虽然反击不起作用

时间:2015-10-09 09:02:47

标签: java loops while-loop

我是 Java新手并正在制作猜谜游戏,其中用户必须猜测0到50之间的随机整数。

我试图在游戏中添加一个猜测计数器,让用户最多7 猜测,然后再#34;游戏结束"然而,尽管我的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);

    }}

4 个答案:

答案 0 :(得分:2)

如果你希望在7次猜测后终止,你应该有一个循环:

do {
    ....
} while(randomInt != NumberGuess) && (i < 8);

当您仅在外部循环中测试i时,您会为用户提供无限数量的猜测,因为一旦用户猜出正确的数字,您就会离开内部循环。只有在离开内循环后,才会测试猜测次数。

答案 1 :(得分:0)

您也可以检查存在状态,如果没有猜到并且给定机会超出i值(例如,例如:7):

 }while(i < 8);

注意:删除了不必要的条件,请参阅@Rehman评论 如果用户猜到了正确的号码,您可以randomInt != NumberGuess删除System.exit(0)

答案 2 :(得分:0)

我认为它可能会解决您的疑问,

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;
            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(i < 8);

            System.out.println("Game over!");
        System.out.println("All 7 Guesses used!");
        System.exit(0);

    }}

答案 3 :(得分:0)

public class GuessingGame {
    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 = 1;
        Scanner UserGuess = new Scanner(System.in);

        while (i <= 7) {

            NumberGuess = UserGuess.nextInt();
            if (NumberGuess == randomInt) {
                System.out.println("Correct! You win!");
                System.out.println("It took you " + i + " guesses.");
                System.exit(0);
            } else 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.");
            } else if (NumberGuess > randomInt) {
                System.out.println("Guess is too big.");
            }
            System.out.println("You have made " + i + " guesses out of 7");
            i++;
        }

        System.out.println("Game over!");
        System.out.println("All 7 Guesses used!");
        System.exit(0);
    }
}

此外,您不应Scanner UserGuess = new Scanner(System.in);在循环内使用此语句。 只能在循环外创建一个扫描仪副本。