import java.util.Scanner;
public class CoinTossGame {
public static void main(String[] args) {
System.out.println("A coin is tossed!");
int Heads=0, Tails=1;
Scanner input = new Scanner (System.in);
System.out.println("Enter your guess."); //Starting message
System.out.println("Press 0 for Heads and 1 for Tails."); //prompts user to enter the input
String Guess = input.nextLine( ); //Stored input in variable
int i= (int) (Math.random () * 2); //Store random number
if (Guess==i) {
System.out.println("Nice guess.\nYou are really guenius!!");
}
else {
System.out.println("Opps! wrong guess.");
System.out.println("Try again.");
System.out.println("Thank you.");
}
}
}
问问题
180 次
2 回答
0
您将整数与字符串进行比较。那显然行不通。转换Guess
成int
或i
转换成String
.
于 2017-10-15T19:38:22.723 回答
0
您正在将 aint
与 a进行比较String
:if (Guess==i)
你需要的是 2 个整数:
int guess = Integer.parseInt(input.nextLine()); //or int guess = input.nextInt();
int i = (int) (Math.random () * 2); //Store random number
if (guess==i) {
System.out.println("Nice guess.\nYou are really guenius!!");
}
或与 2 Strings
:
String guess = input.nextLine();
String i = ((int) (Math.random () * 2)) + "";
if (guess.equals(i)) { // for object use equals() and not ==
System.out.println("Nice guess.\nYou are really guenius!!");
}
于 2017-10-15T19:38:30.850 回答