2014-08-28 82 views
-1

我開始學習本週早些時候(星期一)java和現在,而只是通過excercises打算從http://programmingbydoing.com/ (具體鍛鍊; Tibial我做的是http://programmingbydoing.com/a/a-little-quiz.html找不到符號。 java編譯器錯誤

,現在我碰到一個錯誤我似乎無法修復。

import java.util.Scanner; 

    public class Quiz 
    { 

     public static void main (String[] args) 
     { 

      Scanner keyboard = new Scanner(System.in); 

      String ready,answer3; 
      int answer1,answer2, score; 

      score = 0; 

      System.out.println ("Are you ready for a quis?(Y/N) "); 
      ready = keyboard.next(); 

      if (ready == Y) 
      { 
       System.out.println ("Great, let's get to it then!"); 
      } 
      else if (ready == N) 
      { 
       System.out.println ("Well since you did start this program of your own volition I assume you are ready and you're simply having a go at me."); 
      } 

      else 
      { 
       System.out.println ("Error, wrong input!"); 
      } 

      System.out.println(); 

      System.out.println ("Q1) What is the capital of Australia?"); 
      System.out.println (" 1) Brisbane"); 
      System.out.println (" 2) Sydney"); 
      System.out.println (" 3) Canberra"); 
      answer1 = keyboard.nextInt(); 

      if (answer1 == 1) 
      { 
       System.out.println ("Sorry, Canberra is the capital of Australia"); 
      } 

      else if (answer1 == 2) 
      { 
       System.out.println ("Sorry, Canberra is the capital of Australia"); 
      } 

      else if (answer1 == 3) 
      { 
       System.out.println ("Correct!"); 
       score = score+1; 
      } 
      else 
      { 
       System.out.println ("Error, wrong input!"); 
      } 

      System.out.println(); 

      System.out.println ("Q2) Can you store the value 'cat' in a varible of the int type? "); 
      System.out.println (" 1) Yes"); 
      System.out.println (" 2) No"); 
      answer2 = next.keyboardInt(); 


      System.out.println(); 

      if (answer2 == 1) 
      { 
       System.out.println ("Sorry, 'cat' is a string, ints can only store numbers."); 
      } 
      else if (answer2 == 2) 
      { 
       System.out.println ("That's right!"); 
       score = score+1; 
      } 

      else 
      { 
       System.out.println("Error, wrong input"); 
      } 

     System.out.println(); 

     System.out.println ("Did vikings wear horned helmets in combat?(Y/N)"); 
     System.out.println (" 1) Yes"); 
     System.out.println (" 2) No"); 
     answer3= keyboard.next(); 

     System.out.println(); 

     if (answer3 == Y) 
     { 
      System.out.print (" That's wrong, the only times a viking would ever have..... nevermind let's proceed with the quiz"); 
     } 

     else if (answer3 == N) 
     { 
      System.out.println ("Correct!"); 
      score = score+1; 
     } 

     else 
     { 
      System.out.println ("Error, wrong input!"); 
     } 

     System.out.println ("Overall, you got a total score of " + score + " out of 3 possible."); 
     System.out.println ("Thanks for playing"); 

    } 

} 

我很感謝任何人都可以提供的幫助!

+0

請提供實際的錯誤代碼。 – 2014-08-28 12:59:38

+1

什麼是錯誤? – 2014-08-28 13:02:15

回答

1

也許定義符號串

... 
if (ready == "Y") // just Y without quotes is interpreted as a variable or keyword. You have to make it a string 
{ 
    System.out.println ("Great, let's get to it then!"); 
} 
else if (ready == "N") 
{ 
    System.out.println ("Well since you did start this program of your own volition I assume you are ready and you're simply having a go at me."); 
} 
... 

同以下物質:

if (answer3 == "Y") 
{ 
    System.out.print (" That's wrong, the only times a viking would ever have..... nevermind let's proceed with the quiz"); 
} 

else if (answer3 == "N") 
{ 
    System.out.println ("Correct!"); 
    score = score+1; 
} 

UPDATE:問題的不明確地一部分,但它是由Sam提出了一個有用的技巧:比較字符串,如果您對其內容感興趣,請使用.equals(),例如:

if (ready.equals("Y")) 

更新2:另一個錯誤在於將引發另一種編譯錯誤的意見建議是

answer2 = next.keyboardInt(); 

,應該是

answer2 = keyboard.nextInt(); 
+2

也使用'.equals(...)'進行字符串比較而不是'==' – SamYonnou 2014-08-28 13:04:28

+0

也應該是'keyboard.nextInt()'而不是'next.keyboardInt()' – 2014-08-28 13:07:36

+1

此外,如果您翻轉字符串比較並做類似「Y」.equals(ready)而不是ready.equals(「Y」)的操作,如果ready爲null,則可以獲得相同的邏輯結果,而不必擔心會發生NullPointerException。 – McGlone 2014-08-28 13:11:21

0

準備==Ÿ - >錯 準備= =「Y」或ready.equals(「Y」) - >正確

answer2 = next.keyboardInt(); - >錯誤 answer2 = keyboard.nextInt(); - >正確