有什么不能解决的?
Something can't be resolved?
正在尝试测试我的 java "skills" 并制作一个基于文本的游戏——但我无法获得用户输入。我已经导入了扫描仪 class,它可以很好地使用整数,所以坦率地说,我知道问题出在哪里。每当我尝试编译它时,包含 "String name = scanner.next();" 的行都会显示 'Scanner cannot be resolved' 错误。
import java.util.Scanner;
public class CH1 {
public static void main (String args[]) {
Scanner s= new Scanner( System.in);
int answer;
System.out.println ("You're in grave danger, but first, I must know your name. Will you tell me? ");
answer = s.nextInt();
if (answer == 1) {
System.out.println ("I respect your decision, but I'll need to know your name
if you turn up dead, unless you want to have a one man funeral.");
System.out.println ("What's your name?");
String name = scanner.next();
}
else if (answer == 2) {
System.out.println("Great, now what's your name?");
String name = scanner.next();
}
else {
System.out.println(" Huh? I didn't really get that. (1 for no, 2 for yes.)");
}
}
}
您首先将扫描仪命名为 s!
您以后不能只使用其他名称!
因此只需将扫描器变量名称更改为 "scanner" 并继续使用该名称即可。
除此之外:您几乎从不使用单字符变量名(for 循环中的索引值除外)。关键是:变量名应该说明它们所表示的事物。 "s"什么都没说!
正在尝试测试我的 java "skills" 并制作一个基于文本的游戏——但我无法获得用户输入。我已经导入了扫描仪 class,它可以很好地使用整数,所以坦率地说,我知道问题出在哪里。每当我尝试编译它时,包含 "String name = scanner.next();" 的行都会显示 'Scanner cannot be resolved' 错误。
import java.util.Scanner;
public class CH1 {
public static void main (String args[]) {
Scanner s= new Scanner( System.in);
int answer;
System.out.println ("You're in grave danger, but first, I must know your name. Will you tell me? ");
answer = s.nextInt();
if (answer == 1) {
System.out.println ("I respect your decision, but I'll need to know your name
if you turn up dead, unless you want to have a one man funeral.");
System.out.println ("What's your name?");
String name = scanner.next();
}
else if (answer == 2) {
System.out.println("Great, now what's your name?");
String name = scanner.next();
}
else {
System.out.println(" Huh? I didn't really get that. (1 for no, 2 for yes.)");
}
}
}
您首先将扫描仪命名为 s!
您以后不能只使用其他名称!
因此只需将扫描器变量名称更改为 "scanner" 并继续使用该名称即可。
除此之外:您几乎从不使用单字符变量名(for 循环中的索引值除外)。关键是:变量名应该说明它们所表示的事物。 "s"什么都没说!