2017-10-15 20 views
-4
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."); 
    } 

} 
} 
+1

我已經downvoted這個問題因爲「我有錯誤」不是有用的問題狀態換貨。請[編輯]您的問題,以包括您收到的錯誤,然後我會考慮收回我的downvote。 –

+2

您正在比較INT與STRING ... – azro

+0

/CoinTossGame.java:19:錯誤:無法比較的類型:字符串和整數 if(Guess == i){ ^ 1 error –

回答

0

您將整數與字符串進行比較。這顯然是行不通的。將Guess轉換爲inti轉換爲String

0

你在比較的intStringif (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!!"); 
}