Search code examples
javaif-statementcoin-flippingjava-package

I am doing a coin toss game program. But i am having an error in this portion [ (if Guess==i) {. How do I solve this?


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.");
   }

}
}

Solution

  • You compare an Integer with a String. That obviously can not work. Convert either Guess into int or i into String.