我正在尝试创建一种方法,用户可以预测模拟抛硬币是正面还是反面。我无法将随机数分配给 1 或 2(对于尾部的头部)。我相信我的尝试(下面的代码)给了我 0、1 和 2。
public static void coinToss(String call){
//setting up random
Random random = new Random();
//set user guess to numeric value
int guessNo=0;
if (call.equalsIgnoreCase("heads")){
guessNo=1;
}else if (call.equalsIgnoreCase("tails")){
guessNo=2;
}else System.out.println("Invalid. Enter either heads or tails");
//setting result for coin toss
**int result = random.nextInt(2);**
System.out.println("Trace: numberic result is:" + result);
//if...else statement to print results of coin toss
if (result==1){
System.out.println("Result: heads");
}else if (result==2){
System.out.println("Result: Tails");
}else System.out.println("Invalid result");
if (guessNo==result){
System.out.println("Guess Correct");
}else System.out.println("Guess incorrect");
}//method end
我是一个相对的java新手(显然!)所以非常感谢您的帮助!