[英]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: 我得到的错误是:
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.