为什么编译时会使用扫描仪找到“找不到符号”?

时间:2014-04-16 03:02:56

标签: java

我在这里学习java,所以当我编写一个简单的程序时,每次尝试编译时都会出错。这是我得到的错误:

Trivia.java:14: error: cannot find symbol
ret = sc.nextline();
        ^

该计划如下:

import java.util.Scanner;

class GameStart {
  public static void main(String[] args){
   Scanner sc = new Scanner(System.in);
   String ret;
   byte qnum;
   String ans;

 String correct = "Awesomely correct!";
 String wrong = "Darn it! Almost got it!";

 System.out.println("Do you think you know your stuff?");
 ret = sc.nextline;

 if (ret.equals("yes") || ret.equals("Yes"))
 {
 System.out.println("Well, then let's test what you know! Choose a number from 1 to 5!");
qnum = sc.nextByte();
  switch (qnum)
  {
    case 1:
    System.out.println("In what year did the French Revolution start?");
    ans = sc.nextline();
      if (ans.equals("1789") || ans.equals("Seventeen eighty nine"))
      {
      System.out.println(correct);
      }
      else
      {
      System.out.println(wrong);
      }
     break;

    case 2:
    System.out.println("How many protons does a sodium atom have?");
    ans = sc.nextline();
      if (ans.equals("11") || ans.equals("Eleven"))
      {
      System.out.println(correct);
      }
      else
      {
      System.out.println(wrong);
      }
     break;

    case 3:
    System.out.println("What is 2^6*0.5-12?");
    ans = sc.nextline();
      if (ans.equals("20") || ans.equals("Twenty"))
      {
      System.out.println(correct);
      }
      else
      {
      System.out.println(wrong);
      }
     break;

    case 4:
    System.out.println("Which is the lowest numbered element in the periodic table?");
    ans = sc.nextline();
      if (ans.equals("Hydrogen") || ans.equals("hydrogen"))
      {
      System.out.println(correct);
      }
      else
      {
      System.out.println(wrong);
      }
     break;

    case 5:
    System.out.println("Which is the unit that measures Coulombs per second?");
    ans = sc.nextline();
      if (ans.equals("Ampere") || ans.equals("ampere"))
      {
      System.out.println(correct);
      }
      else
      {
      System.out.println(wrong);
      }
     break;
    default:
    System.out.println("Stick to the rules! 1-5!");
  }

 }
 else
 {
 System.out.println("Not liking that attitude, I want to hear a big yes!");
 }

}
}

正如我所提到的,我是Java的新手,所以你肯定会发现更多的错误,而且我的风格也是#34;可能很糟糕,所以欢迎所有的建议! :)

4 个答案:

答案 0 :(得分:0)

您忘记在此行中添加()括号

ret = sc.nextline;

也是nextLine而不是nextline

更改ret = sc.nextline;

 ret = sc.nextLine();

答案 1 :(得分:0)

使用以下

 ret = sc.nextLine();

而不是

 ret = sc.nextline;

答案 2 :(得分:0)

应该是这样的:

  

rc.next的()

案例非常重要。

括号也是。

答案 3 :(得分:0)

你的case错了,你忘记了括号(你应该先调用hasNextLine()) -

// something like this (with a ternary)
ret = (sc.hasNextLine()) ? sc.nextLine() : null;

if (sc.hasNextLine()) {
  ret = sc.nextLine();
} else {
  ret = null;
}