简体   繁体   English

为什么我的猜数字游戏没有向控制台输出任何信息?

[英]Why doesn't my number guessing game output anything to the console?

My program doesn't output anything to the console. 我的程序没有任何输出到控制台。 I'm not sure what I did wrong. 我不确定自己做错了什么。 I'm required to use System.in.read() and can't use a Scanner . 我需要使用System.in.read()而不能使用Scanner I'm also required to use any loop of my choice. 我还需要使用我选择的任何循环。

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."); 
            }
        } 
    }
}

This line: 这行:

while(correct == false);

needs to lose the semicolon at the end. 最后需要输掉分号。 As it stands now, it's an infinite empty loop and your program won't proceed past that statement. 就目前而言,这是一个无限的空循环,您的程序不会继续执行该语句。

To add to Greg's answer, System.in.read() returns a char . 为了增加Greg的答案, System.in.read()返回一个char If you store it in an int you will have the ASCII representation of the value. 如果将其存储在int ,则将具有该值的ASCII表示形式。

For example 56 is the ASCII for '8'. 例如56是ASCII的'8'。 The offset of the numbers in the ASCII table in HEX is 0x30. HEX的ASCII表中数字的偏移量为0x30。 So, for the code to actually work, you should subtract 0x30 to the received value. 因此,为使代码真正起作用,应将接收值减去0x30。

So, you should change the input line to the following one: 因此,您应该将输入行更改为以下一行:

guess = System.in.read() - 0x30;

First, when you have a boolean type, there is no need for you to check (correct == false) , you can simply use the operator ! 首先,当您具有boolean类型时,不需要检查(correct == false) ,您可以简单地使用运算符! like this (!correct) 像这样(!correct)

The problem that you have is that you are using the semicolon after the while, while loop has no semicolon. 您遇到的问题是,在while,while循环后没有分号的情况下使用了分号。 So you have this while(!correct); 所以你有这个while(!correct); , so it stays on an infinite loop without executing the inner block of code ,因此它不执行内部代码块就处于无限循环

    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."); 
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM