我第一次尝试在我的 Mac (OS X 10.6.8) 上使用 Java。一个简单的“Hello World”示例可以正常工作。但是,现在我正在尝试通过命令提示符输入数据,并且编译在语法上失败,需要一个分号。代码来自 DS Malik 所著的书:“Java Programming: From Problem Analysis to Program Design”,第 5 版。
编译错误是:
javac Example2_17.java
Example2_17.java:18: ';' expected
firstName console.next();
^
1 error
java版本是:
java -version
java version "1.6.0_51"
Java(TM) SE Runtime Environment (build 1.6.0_51-b11-457-10M4509)
Java HotSpot(TM) 64-Bit Server VM (build 20.51-b01-457, mixed mode)
java代码是:
import java.util.*;
public class Example2_17
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
String firstName;
String lastName;
int age;
double weight;
System.out.println("Enter first name, last name, age, and weight separated by spaces.");
firstName console.next(); // <----------- ERROR HERE!
//lastName console.next();
//age console.nextIng();
//weight console.nextDouble();
//System.out.println("Name: " + firstName + " " + lastName);
//System.out.println("Age: " + age);
//System.out.println("Weight: " + weight);
}
}
任何帮助是极大的赞赏。
谢谢!