Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';我的数字猜谜游戏没有输出任何东西到控制台吗?_Java_Loops - Fatal编程技术网

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-16
char
包含一个UTF-16代码单元,其中一个或两个编码一个码点,一个基本码点和零个或多个组合码点构成一个语义/视觉单元,称为grapheme(可以称为字符)。例如,“\u0061\u0301”构成了graphemeá和“\uD834\uDD60”构成了