我遇到了這個任務的麻煩,我已經給了(高中)。這是一個猜數字的遊戲,我已經把它的大部分內容都放下了,但他希望我們在控制檯上放一些數字。一個例子輸出爲:猜數程序問題Java
輸入下限:4
輸入上限:10
等,基本上選擇你想要的計算機以供選擇號碼的特定範圍。我只能在一個設定的範圍內編碼(1-1000),因爲我無法弄清楚如何去做他想做的事情。這裏是我的代碼:
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
int randomNumber;
randomNumber = (int) (Math.random() * 999 + 1);
Scanner keyboard = new Scanner(System.in);
int guess;
do {
System.out.print("Enter a guess (1-1000): ");
guess = keyboard.nextInt();
if (guess == randomNumber)
System.out.println("Your guess is correct. Congratulations!");
else if (guess < randomNumber)
System.out.println("Your guess is smaller than the secret number.");
else if (guess > randomNumber)
System.out.println("Your guess is greater than the secret number.");
} while (guess != randomNumber);
}
}
如果你嘗試它,它也真的很難反正玩。我會感謝一些幫助,謝謝!
你覺得這種表達是這樣做的:'(INT)(的Math.random()* 999 + 1)'?你會如何改變它來獲得另一個範圍? – Henry