我有使用Java并在IDE中工作的经验,但是最近我决定切换到Linux。我下载并安装了openjdk-14。我可以编译一些基本程序,但是不能编译从标准库导入的程序。
代码:
/* This program is meant to teach students to take
user input into their programs */
//First, we need to import our Scanner from a library
import java.util.Scanner;
public class ScannerPractice {
public static void main(String[] args){
//Next, we declare and create our Scanner object
Scanner sc = Scanner(System.in);
//We declare some empty variables for future input
String name = "";
int age = 0;
//We create a prompt
System.out.print("Enter your name: ");
//Notice that we did not use println. This just makes it look nicer
//Next, we assign specific values to our variables.
name = sc.next();
System.out.print("Enter your age: ");
age = sc.nextInt();
//Finally, we can print our final results
System.out.println("Hello! My name is " + name + " and I'm " + age + " years old!");
}
}
终端命令:
javac ScannerPractice.java
输出:
ScannerPractice.java:10: error: cannot find symbol
Scanner sc = Scanner(System.in);
^
symbol: method Scanner(InputStream)
location: class ScannerPractice
1 error
尝试此代码//在扫描仪之前添加新的代码
Scanner scanner = new Scanner(System.in);