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.");
}
}
}
Asked
Active
Viewed 224 times
-4

azro
- 53,056
- 7
- 34
- 70

Antu Islam
- 3
- 1
-
1I have downvoted this question because "i am having an error" is not a useful problem statement. Please [edit] your question to include the error that you are getting, and then I will consider retracting my downvote. – Joe C Oct 15 '17 at 19:37
-
2You are comparing an INT with a STRING ... – azro Oct 15 '17 at 19:37
-
/CoinTossGame.java:19: error: incomparable types: String and int if (Guess==i) { ^ 1 error – Antu Islam Oct 15 '17 at 19:38
2 Answers
0
You compare an Integer with a String. That obviously can not work. Convert either Guess
into int
or i
into String
.

JT 1114
- 73
- 1
- 11
0
You are comparing an int
with a String
: if (Guess==i)
What you need is 2 ints :
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!!");
}
Or with 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!!");
}

azro
- 53,056
- 7
- 34
- 70