编写一个模拟抛硬币的程序。 首先,要求用户“打电话”或预测折腾。 接下来,让用户知道您正在抛硬币。 然后报告用户是否正确。
例:
Please call the coin toss (h or t): h Tossing... The coin came up heads. You win!
这是关于我应该做的事情。 这是我到目前为止的代码:
package inClassCh4Sec8to9;
import java.util.Random;
import java.util.Scanner;
public class ClassCh4Sec8to9 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true) {
System.out.print("Enter you guess (1 for heads, 0 for tails, 2 to quit):");
int call = input.nextInt();
int heads = 1;
int quit = 2;
int tails = 0;
if (call == quit) {
break;
} else if (call == heads) {
} else if (call == tails) {
} else {
System.out.println("invalid");
continue;
}
Random random = new Random();
int coinflip = random.nextInt(2);
if(call == coinflip){
System.out.println("Correct!");
}else{
System.out.println("Sorry, incorrect.");
}
}
}
}
我的问题:
我可以得到一个随机数,没问题,但是它允许将h和t用作1和0。
我希望h或head等于1作为输入。