Java 为什么';我的数字猜谜游戏没有输出任何东西到控制台吗?
我的程序没有向控制台输出任何内容。我不确定我做错了什么。我必须使用Java 为什么';我的数字猜谜游戏没有输出任何东西到控制台吗?,java,loops,Java,Loops,我的程序没有向控制台输出任何内容。我不确定我做错了什么。我必须使用System.in.read(),不能使用扫描仪。我还需要使用我选择的任何循环 package MyGuessingGame; import java.io.IOException; /** * * @author Anthony */ public class MyGuessingGame { /** * @param args the command line arguments * @thro
System.in.read()
,不能使用扫描仪。我还需要使用我选择的任何循环
package MyGuessingGame;
import java.io.IOException;
/**
*
* @author Anthony
*/
public class MyGuessingGame {
/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
int guess = 0;
int answer = 8;
//Todo change to random
boolean correct = false;
while (correct == false); {
System.out.println("Please enter your guess!");
guess = System.in.read();
System.in.read();
if (guess == answer) {
correct = true;
System.out.println("Congradulations You have won!");
} else if (guess != answer) {
correct = false;
System.out.println("Sorry try again.");
}
}
}
}
这一行:
while(correct == false);
需要在结尾处丢失分号。现在,它是一个无限的空循环,您的程序将无法通过该语句继续运行。要添加到Greg的答案中,System.in.read()
返回一个char
。如果将其存储在int
中,则将具有该值的ASCII表示形式
例如,56是“8”的ASCII码。ASCII表格中十六进制数字的偏移量为0x30。因此,为了使代码实际工作,您应该将接收到的值减去0x30
因此,您应该将输入行更改为以下行:
guess = System.in.read() - 0x30;
首先,当您有一个布尔
类型时,您无需检查(correct==false)
,只需使用运算符代码>像这样(!correct)
问题是在while之后使用分号,而循环没有分号。所以你有了这个而(!correct)代码>,因此它停留在无限循环中,而不执行内部代码块
int guess = 0;
int answer = 8;
//Todo change to random
boolean correct = false;
while(!correct)
{
System.out.println("Please enter your guess!");
guess = System.in.read();
if (guess == answer) {
correct = true;
System.out.println("Congradulations You have won!");
} else {
System.out.println("Sorry try again.");
}
}
不用担心,每个人都曾在某个时候这样做过。那些说他们还没做过的人。要么他们是卑鄙的说谎者。谢谢,我也需要。不,不是。Java使用Unicode/UTF-16char
包含一个UTF-16代码单元,其中一个或两个编码一个码点,一个基本码点和零个或多个组合码点构成一个语义/视觉单元,称为grapheme(可以称为字符)。例如,“\u0061\u0301”构成了graphemeá和“\uD834\uDD60”构成了