0

对于作业,我必须编写以下代码:

import java.util.Scanner;

public class Words {
    public static void main(String[] args)  {
        //Display welcome message
        System.out.println("Welcome to the String Function Event!");
        Scanner userInput = new Scanner(System.in);

        String result1;
        System.out.print("Please input the first word:");
        result1=userInput.next();

        String result2;
        System.out.print("Please input the second word:");
        result2=userInput.next();

        System.out.println("The words you chose are " +result1+ " and " +result2+ ".");
    }
}   

当我尝试编译它时,它在命令提示符中给出了 3 个错误,显示“无法解析符号,符号:类 Scanner,位置:类 Words,Scanner userInput=new Scanner(System.in)”。我不确定错误在哪里。我应该使用 BufferedReader 代替输入吗?

4

4 回答 4

2

你的代码绝对没问题。您需要确保安装了最新版本的 JDK。为此,请从此处获取最新的 SDK:http ://www.oracle.com/technetwork/java/javase/downloads/index.html

于 2013-08-17T13:05:45.953 回答
1

You're code is great, it just has a minor error. All you needed to do was enter Line after the userInput.next and it should work correctly. It is corrected in the bold.

String result1;
    System.out.print("Please input the first word:");
    result1 = userInput.next**Line**();

    String result2;
    System.out.print("Please input the second word:");
    result2 = userInput.next**Line**();
于 2015-10-01T06:21:59.390 回答
0

如果您继续收到此错误,您可以跳过使用扫描仪。

你可以试试

using java.io.Console;

public class Words{

  public static void main(String[] args){
     Console con = new System.Console();
     System.out.println("Enter some text");
     String input = con.readLine();

  }

}

这是一个示例 http://www.javapractices.com/topic/TopicAction.do?Id=79

和 Java 文档http://docs.oracle.com/javase/6/docs/api/java/io/Console.html

于 2013-08-17T13:20:11.807 回答
0

你的代码是绝对正确的。请安装最新版本的 JDK 并重试。

于 2013-08-17T13:25:24.820 回答