2014-08-28 6 views
-1

Я начал изучать Java в начале этой неделе (понедельник), и теперь в то время как раз проходя от упражнений http://programmingbydoing.com/ (Specific физических упражнений я делаю это 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

Сообщите, пожалуйста, код ошибки. –

+1

Какая ошибка? –

ответ

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: не явно часть вопроса, но полезный совет предложил Сэм: для сравнения строк, если вы заинтересованы в их содержании, используйте .equals(), например:

if (ready.equals("Y")) 

UPDATE 2: одна ошибка предложила в комментариях, которые будут бросать другой вид ошибки компиляции является

answer2 = next.keyboardInt(); 

, которые должны быть

answer2 = keyboard.nextInt(); 
+2

также используют '.equals (...)' для сравнения строк вместо '==' – SamYonnou

+0

также, это должно быть 'keyboard.nextInt()', а не 'next.keyboardInt()' –

+1

Кроме того, если вы переверните свой Строковое сравнение вокруг и сделать что-то вроде «Y» .equals (ready) вместо ready.equals («Y»), вы можете получить один и тот же логический результат, не беспокоясь о получении исключения NullPointerException, если оно равно null. – McGlone

0

готов == Y -> неправильно готов = = "Y" или ready.equals ("Y") -> правильный

answer2 = next.keyboardInt(); -> неправильный answer2 = keyboard.nextInt(); -> correct

Смежные вопросы