2014-08-28 85 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(); - >正确