/**Toss coin test that allow the you guess the upside as many times as you wish. When exit, the system would give you a list of information about your guesses.*/
import java.io.*;
public class TossCoin {
public static void main(String[]args)
{
String input=null;
long seed=System.currentTimeMillis();
BufferedReader read=new BufferedReader(new InputStreamReader(System.in));
GenericCoin coin=new GenericCoin();
int tossCount=0;
int frontCount=0;
int backCount=0;
int rightCount=0;
try{
coin.throwCoin(seed);
System.out.print("Input your guess: ");
input=read.readLine();
while(!input.equals("exit"))
{
tossCount++;
if(coin.getFront()==1)
/**Assertion: Upside is front.*/
{
frontCount++;
if(input.equals("front"))
{
rightCount++;
System.out.println("The upside is front.You are right!");
}
else
System.out.println("The upside is front.You are wrong!");
}
else
{
backCount++;
if(input.equals("back"))
{
rightCount++;
System.out.println("The upside is back.You are right!");
}
else
System.out.println("The upside is back.You are wrong!");
}
seed=seed+500;
coin.throwCoin(seed);
System.out.print("Input your guess: ");
input=read.readLine();
}//end while
}//end try
catch(IOException e){
e.printStackTrace();
}
System.out.println("Toss "+tossCount+" times " +
"front: "+frontCount+" times back: "+backCount+" times");
System.out.println("Right guess: "+rightCount);
System.out.println("Right percentage: "+(double)rightCount/(double)tossCount*100+"%");
}
}
result:
Input your guess: back
The upside is front.You are wrong!
Input your guess: front
The upside is back.You are wrong!
Input your guess: back
The upside is back.You are right!
Input your guess: back
The upside is back.You are right!
Input your guess: exit
Toss 4 times front: 1 times back: 3 times
Right guess: 2
Right percentage: 50.0%
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}