林新到Java並想提出一個猜謎遊戲在用戶有猜測0和50當計數器不能正常工作
之間的隨機整數我試圖猜測添加計數器在「遊戲結束」之前給用戶高達7的遊戲猜測然而,即使我的while循環,櫃檯仍然保持在7以上。
我提前道歉,如果這是一個重複的問題,但是我也有一個簡單的介紹一下,但沒有找到一個很好的答案對我的代碼
繼承人的代碼:
package guessinggame;
import java.util.Random;
import java.util.Scanner;
public class GuessingGame {
//Author TC13551
public static void main(String[] args) {
int NumberGuess = 0;
Random randomNumber = new Random();
int randomInt = randomNumber.nextInt(51);
System.out.println("Guess the number between 0 -50:");
int i = 0;
while(i < 8){
do{
Scanner UserGuess = new Scanner(System.in);
NumberGuess = UserGuess.nextInt();
if(NumberGuess < 0 || NumberGuess > 50){
System.out.println("Invalid Input, please enter numbers between 0 and 50");
}
else if(NumberGuess < randomInt){
System.out.println("Guess is too small.");
i ++;
System.out. println("You have made "+i +" guesses out of 7");
}
else if(NumberGuess > randomInt) {
System.out.println("Guess is too big.");
i ++;
System.out. println("You have made "+i +" guesses out of 7");
}
else if(NumberGuess == randomInt){
System.out.println("Correct! You win!");
System.out.println("It took you "+ i + " guesses.");
System.exit(0);
}
}while(randomInt != NumberGuess);
}System.out.println("Game over!");
System.out.println("All 7 Guesses used!");
System.exit(0);
}}
ATLEAST提供你所面對的問題...... – afzalex
此外,如果你得到的隨機數'0',你將永遠無法創建,因爲'randomInt輸入== 0'和'猜數字== 0' – SomeJavaGuy