2014-01-23 40 views
-1

我對Java非常陌生,試圖從這本書中學習一門課程,並且我堅持製作一個程序。如果是錯誤,並且它不能找到標記爲guessdigit1的符號,請不要告訴我。否則,如果在程序中出現錯誤,不知道哪裏出了什麼問題?

當我將鼠標懸停在符號旁邊的錯誤對話框上時,它總是說guessdigit1和2是它們自己的類。任何想法? 謝謝!

package loterry; 

import java.util.Scanner; 

public class Loterry { 
// This program creates two random numbers, and checks to see if your guess 
    // makes the lottery win 

    public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter in your guess for loterry, two digits please"); 
     int lottery = (int)(Math.random()*100 /50); 
     int guess = input.nextInt(); 
     int lotterydigit1= lottery /10; 
     int lotterydigit2= lottery %10; 

     // Get digits from guess 
     int guessdigit1 = guess/10; 
     int guessdigit2 = guess % 10; 

     System.out.println("The lottery number is " + lottery); 

     if (guess == lottery) 
      System.out.println("Exact Match: you win 10,000"); 
     else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2); 
      System.out.println("Match all digits: you win 3,000"); 
     else if (guessdigit1 == lotterydigit1 
        || guessdigit1 == lotterydigit2 
        || guessdigit2 == lotterydigit2 
        || guessdigit2 == lotterydigit2) 
        System.out.println("match one digit: you win 1,000"); 
       else 
        System.out.println("sorry no match"); 




    } 
} 

回答

11

這裏是你的問題:

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2); 

刪除分號(;

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2) 

注:建議總是寫你的if else語句支架{}以避免將來發生任何可能的錯誤,當然它也在Java code conventions中。 (見this answer澄清在牙齒矯正問題)

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){ 
    System.out.println("Match all digits: you win 3,000"); 
} 
+1

我不知道我怎麼錯過了...我想肯定的IntelliJ告訴我,有那裏不是分號。 +1。 – Makoto

+0

使用大括號是避免這種錯誤 – 2014-01-23 05:55:59

+0

@RC的好方法。肯定。我加了一張關於那個 – Baby

2

更換
else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2);

else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2)

雖然寫if else你應該使用{}。它使得它clean codeclean code很容易調試。

3

刪除semicolon (;)避免編譯錯誤

使用curly braces "{ }"塊在if else語句,以避免混亂

if (guess == lottery) 
      System.out.println("Exact Match: you win 10,000"); 
     else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2); //->here 
+0

用分號詳細說明,你基本上是在說別的,如果塊已經完成。你的下一行是'System.out ...'是一個新的語句,如果else else(guessdigit1 == lotterydigit1 ...是編譯器正在抱怨的話)。 – ansible

0

只需刪除分號;您已放在最後()你的第一個elseif。

public static void main(String[] args) { 
    Scanner input = new Scanner(System.in); 
    System.out.println("Enter in your guess for loterry, two digits please"); 
     int lottery = (int)(Math.random()*100 /50); 
     int guess = input.nextInt(); 
     int lotterydigit1= lottery /10; 
     int lotterydigit2= lottery %10; 

     // Get digits from guess 
     int guessdigit1 = guess/10; 
     int guessdigit2 = guess % 10; 

     System.out.println("The lottery number is " + lottery); 

     if (guess == lottery) 
      System.out.println("Exact Match: you win 10,000"); 
     else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2) 
      System.out.println("Match all digits: you win 3,000"); 
     else if (guessdigit1 == lotterydigit1 
        || guessdigit1 == lotterydigit2 
        || guessdigit2 == lotterydigit2 
        || guessdigit2 == lotterydigit2) 
        System.out.println("match one digit: you win 1,000"); 
       else 
        System.out.println("sorry no match"); 




    } 
0

呀之後其他事物分號if語句是什麼搞亂您:在其結束

工作守則;

只有語句應該有分號(;)。但是,儘管我會建議你在未來使用括號......這使得代碼看起來更整齊,從而更容易跟蹤事物......相信我,它在處理更大的程序或在一個組中工作時確實有幫助。

0

只是remome ;如果條件和使用{}每個語句後

看到這裏docs

 if (guess == lottery){ 
//code here 

} 

     else if (guessdigit2 == lotterydigit1 && guessdigit1 == lotterydigit2){ 
//code here 

} 

     else if (guessdigit1 == lotterydigit1 
        || guessdigit1 == lotterydigit2 
        || guessdigit2 == lotterydigit2 
        || guessdigit2 == lotterydigit2){ 
//code here 

} 

       else{ 
//code here 

} 
相關問題