【发布时间】:2013-02-03 14:48:51
【问题描述】:
我的“如果”语句没有给我写答案,例如,如果我输入“5”,它会打印得太高,这意味着随机数较低,但如果我输入像“4”这样的较低数字,它也会打印低意味着随机数更高。它非常令人困惑,我只是想知道我在代码中的逻辑是否正确?
导入 java.util.Scanner; 导入 java.lang.Math;
公开课 GuessTest {
public static void main(String[] args)
{
System.out.println("Welcome to the gussing game, Try to guess the number am thinking of to win!");
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println();
System.out.println("Am thinking of a number between 0 and 10. Try to guess it.");
System.out.println();
Scanner sc = new Scanner(System.in);
String choice = "y";
while (!choice.equalsIgnoreCase("n"))
{
System.out.println("Enter the Number:");
int guess = sc.nextInt();
double rightNum = Math.random() *10;
int randomNum = (int) rightNum;
if (guess == randomNum)
{
System.out.println("Correct you've got it! The Number Was " + randomNum );
System.out.println();
System.out.println("Would you like to play again (y/n):");
choice = sc.next();
}
else if (guess < randomNum)
{
System.out.println("your too low!");
}
else if (guess > randomNum)
{
System.out.println("Your too high!");
}
}
}
}
【问题讨论】:
标签: java