显然我的代码根据自动化测试没有工作,并没有给我一个通过我的解决方案,但是当我运行它似乎工作正常。我想知道我的代码有什么问题?:
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);
}
}
赫尔辛基大学在线Java课程练习41,练习详情如下:http://mooc.cs.helsinki.fi/programming-part1/material-2013/week-2?noredirect=1#e41
我得到的错误是:
谢谢
答案 0 :(得分:2)
你有简单的逻辑错误
当数字较小时,条件应为False
或数字较大,应为guess > numberDrawn
:
guess < numberDrawn