【发布时间】:2014-02-22 13:43:56
【问题描述】:
为什么Number lower than 9 在我的控制台中打印了两次,在下面的代码示例中?
public static void main(String[] args) throws java.io.IOException {
System.out.println("Input from keyboard should be '49'");
char e;
for (int a = 0; ; a++) {
e = (char) System.in.read();
if (e == 49)
break;
else
System.out.println("Use number lower than 9");
}
}
这是当您键入除数字 1 之外的任何内容时的输出:
run:
Input from keyboard should be '49'
2
Number lower than 9
Number lower than 9
编辑
基本上,我正在尝试从控制台获取输入并将其存储在char e 中。我输入1 后得到的是值49。这就是我在上面的代码中使用e == 49 的原因。我不知道如何只检索字符1。接下来是我使用无限循环来强制输入要求特定字符,直到它正确为止。
【问题讨论】:
-
您永远不会在代码中打印“数字低于 9”...