简体   繁体   English

猜数字游戏不起作用

[英]number guessing game not working

Apparently my code is not working according to an automated test and doesn't give me a pass for my solution, however when I run it it seems to work fine. 显然,根据自动化测试,我的代码无法正常工作,并且无法通过我的解决方案,但是当我运行它时,它似乎可以正常工作。 I'm wondering what's wrong with my code?: 我想知道我的代码怎么了?:

import java.util.Random;
import java.util.Scanner;

public class GuessingNumberGame {

    public static void main(String[] args) {
        Scanner reader = new Scanner(System.in);
        int numberDrawn = drawNumber();

        int i = 1;
        boolean correct = false;

        while (correct == false) {

        System.out.print("Guess a number: ");
        int guess = Integer.parseInt(reader.nextLine());

        if (guess == numberDrawn) {
            System.out.println("Congratulations, your guess is correct!");
            correct = true;

        } else if (guess < numberDrawn) {
                    System.out.println("The number is lesser, guesses made: " + i);
                                i++;
                    } else if (guess > numberDrawn) {
                        System.out.println("The number is greater, guess made: " + i);
                                    i++;
        }
        }
    }

    // DO NOT MODIFY THIS!
    private static int drawNumber() {
        return new Random().nextInt(101);
    }
}

It is exercise 41 on the University of Helsinki online Java course, the exercise details are here: http://mooc.cs.helsinki.fi/programming-part1/material-2013/week-2?noredirect=1#e41 这是赫尔辛基大学在线Java课程的练习41,此处的练习详细信息为: http : //mooc.cs.helsinki.fi/programming-part1/material-2013/week-2?noredirect=1#e41

The errors that I get are: 我得到的错误是:

  • If guess is too small, program should print "The number is greater". 如果猜测太小,程序应打印“数字更大”。
  • If guess is too large, program should print "The number is lesser". 如果猜测太大,程序应打印“数字较小”。
  • If the drawn number is 1 and user input is 0, 1, program should at first print "The number is greater", and after that "Congratulations, your guess is correct!" 如果绘制的数字是1,并且用户输入是0、1,则程序应首先打印“数字更大”,然后打印“恭喜,您的猜测是正确的!”
  • If the drawn number is 1 and user input is 2, 1, program should at first print "The number is lesser", and after that "Congratulations, your guess is correct!" 如果绘制的数字是1,并且用户输入是2、1,则程序应首先打印“数字较小”,然后打印“恭喜,您的猜测是正确的!”

Thank you 谢谢

You have simple logic mistake 你有简单的逻辑错误

When the number is lesser condition should be guess > numberDrawn or the number is greater it should be guess < numberDrawn : 当数字较小时,应guess > numberDrawn或数字较大,应guess < numberDrawn

    if (guess == numberDrawn) {
         System.out.println("Congratulations, your guess is correct!");
         correct = true;
    } else if (guess > numberDrawn) {
                System.out.println("The number is lesser, guesses made: " + i);           
                i++;
    } else if (guess < numberDrawn) {
                System.out.println("The number is greater, guess made: " + i);           
                i++;
    }

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

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