我如何修复Inner While循环

时间:2018-06-18 23:48:20

标签: java while-loop

HELP!

内部循环(askAgain)无法正常工作。我想要它,以便当用户按下' y'要开始一个新的游戏,它将退出内循环并返回外部whileloop(keepPlaying)并按下' n'退出游戏。

如果用户既没有按下' y'或者' n',任何其他按键,它将循环回:askAgain,直到用户做出有效选择。

该计划未按预期运作。请帮忙。

import java.util.Random;
import java.util.Scanner;

public class Game {


    private  int       playerGuess;

    private  boolean   keepPlaying   = true;
    private  boolean   askAgain      = true;


    Random    randomNumber;   
    int       generatedYear; 
    int       generatedMonth; 
    int       generatedDay;   


    /**
     * 
     */
    public Date getRandomDate() {       

        randomNumber   = new Random();
        generatedYear  = randomNumber.nextInt(600) + 1600;
        generatedMonth = randomNumber.nextInt(12) + 1;
        generatedDay   = randomNumber.nextInt(31) + 1;  

        Date generatedDate = new Date (generatedYear, generatedMonth, generatedDay);

        return generatedDate;
    }   

    /**
     * 
     */
    public void guessTheDate() { 


        Scanner userInput   = new Scanner(System.in);

        while(keepPlaying) { 

            int ranDateYear= getRandomDate().getYear();

            System.out.println("New Game!!");
            System.out.println();
            System.out.print("Please guess the year. (0 - 2199): ");

            while(userInput.hasNext()) { 

                if(userInput.hasNextInt()) { 

                    playerGuess = userInput.nextInt();

                    if(playerGuess > ranDateYear) { 

                        System.out.println("Out of bounds, too high: " + playerGuess + " (Randomized Number: " +  ranDateYear+ ")");
                        System.out.println();
                        System.out.println("Try again!");
                        System.out.print("Please guess the year (0 - 2199): ");

                    }else if(playerGuess < ranDateYear) {

                        System.out.println("Out of bounds, too low:  " + playerGuess + " (Randomized Number: " + ranDateYear + ")");
                        System.out.println();
                        System.out.println("Try again!");
                        System.out.print("Please guess the year (0 - 2199): "); 

                    }else{      

                        if (playerGuess == ranDateYear) {

                            System.out.println("Correct! It is a match:  " + playerGuess + " (Randomized Number: " + ranDateYear + ")");
                            System.out.println();
                            System.out.print("Play again? (Y/N): ");

                            while (askAgain) {

                                while (userInput.hasNext()) {

                                    if (userInput.hasNext()) {

                                        String input = userInput.next();

                                        if (input.equalsIgnoreCase("y")) {

                                            keepPlaying = true;

                                        } else if (input.equalsIgnoreCase("n")){

                                            keepPlaying = false;

                                        } else {

                                            askAgain = true;
                                        }

                                        break;                              
                                    }


                                }

                            }

                        }                           
                    }   

                } else {

                    System.out.println("Invalid input, not a number.");
                    System.out.print("Enter another number: ");
                    userInput.next();

                }
            }   
        }

        System.out.println("The end. Thank you for playing!");
    }
}

3 个答案:

答案 0 :(得分:0)

在你的内部(askAgain)循环中,你永远不会将askAgain的值更改为false,导致它始终保持为真,这会导致无限循环。而是在你的内循环中尝试这个:

if(input.equalsIgnoreCase("y")) {
    keepPlaying = true;
    askAgain = false;
} else if(input.equalsIgnoreCase("n") {
    keepPlaying = false;
    askAgain = false;
} else {
    askAgain = true;

答案 1 :(得分:0)

  

替换

while (askAgain) {

                                while (userInput.hasNext()) {

                                    if (userInput.hasNext()) {

                                        String input = userInput.next();

                                        if (input.equalsIgnoreCase("y")) {

                                            keepPlaying = true;

                                        } else if (input.equalsIgnoreCase("n")){

                                            keepPlaying = false;

                                        } else {

                                            askAgain = true;
                                        }

                                        break;                              
                                    }


                                }

                            }
  

使用

   while (true) {
       String input = userInput.next();
        if (input.equalsIgnoreCase("y")) {
            keepPlaying = true;break;
         } 
        else if (input.equalsIgnoreCase("n")){
             keepPlaying = false;break;
        } 
}

它将解决当前的问题,但它无法解决主要问题,您将在SO上再次提出新问题。实际上你实现代码的方式并不是很好。

<强> Now use this code for a working of your project but mind you this is also not the best way to implement the code

import java.sql.Date;
import java.util.Random;
import java.util.Scanner;

public class JavaAZ {
    public static void main(String[] args) {
        Game myGame = new Game();
        boolean loop = false;
        do {
            loop = myGame.guessTheDate();
            if(!loop) System.out.println("The end. Thank you for playing!");
        }while(loop);

    }
}


 class Game{
     private  int       playerGuess;

        private  boolean   keepPlaying   = true;
        private  boolean   askAgain      = true;


        Random    randomNumber;   
        int       generatedYear; 
        int       generatedMonth; 
        int       generatedDay;   


        /**
         * 
         */
        public Date getRandomDate() {       

            randomNumber   = new Random();
            generatedYear  = randomNumber.nextInt(600) + 1600;
            generatedMonth = randomNumber.nextInt(12) + 1;
            generatedDay   = randomNumber.nextInt(31) + 1;  

            Date generatedDate = new Date (generatedYear, generatedMonth, generatedDay);

            return generatedDate;
        }   

        /**
         * 
         */
        public boolean guessTheDate() { 
            Scanner userInput   = new Scanner(System.in);
                int ranDateYear= getRandomDate().getYear();
                System.out.println("New Game!!");
                System.out.println();
                System.out.print("Please guess the year. (0 - 2199): "+ranDateYear);

                while(userInput.hasNext()) { 
                    if(userInput.hasNextInt()) { 
                        playerGuess = userInput.nextInt();
                        if(playerGuess > ranDateYear) { 
                            System.out.println("Out of bounds, too high: " + playerGuess + " (Randomized Number: " +  ranDateYear+ ")");
                            System.out.println();
                            System.out.println("Try again!");
                            System.out.print("Please guess the year (0 - 2199): ");

                        }else if(playerGuess < ranDateYear) {
                            System.out.println("Out of bounds, too low:  " + playerGuess + " (Randomized Number: " + ranDateYear + ")");
                            System.out.println();
                            System.out.println("Try again!");
                            System.out.print("Please guess the year (0 - 2199): "); 

                        }else{      
                            if (playerGuess == ranDateYear) {
                                System.out.println("Correct! It is a match:  " + playerGuess + " (Randomized Number: " + ranDateYear + ")");
                                System.out.println();
                                    while (true) {
                                         System.out.print("Play again? (Y/N): ");
                                            String input = userInput.next();
                                            if (input.equalsIgnoreCase("y")) {
                                                return true;

                                            } else if (input.equalsIgnoreCase("n")){
                                                return false;
                                            }
                                    }
                            }                           
                        }   

                    } else {

                        System.out.println("Invalid input, not a number.");
                        System.out.print("Enter another number: ");
                        userInput.next();

                    }
                }   
                return false;
        }
 }
  

这是以你的方式运作你的代码

import java.sql.Date;
import java.util.Random;
import java.util.Scanner;

public class Game {


    private  int       playerGuess;

    private  boolean   keepPlaying   = true;
    private  boolean   askAgain      = true;


    Random    randomNumber;   
    int       generatedYear; 
    int       generatedMonth; 
    int       generatedDay;   

    public static void main(String[] args) {

        Game g = new Game();
        g.guessTheDate();
    }
    /**
     * 
     */
    public Date getRandomDate() {       

        randomNumber   = new Random();
        generatedYear  = randomNumber.nextInt(600) + 1600;
        generatedMonth = randomNumber.nextInt(12) + 1;
        generatedDay   = randomNumber.nextInt(31) + 1;  

        Date generatedDate = new Date (generatedYear, generatedMonth, generatedDay);

        return generatedDate;
    }   

    /**
     * 
     */
    public void guessTheDate() { 


        Scanner userInput   = new Scanner(System.in);

        while(keepPlaying) { 

            int ranDateYear= getRandomDate().getYear();

            System.out.println("New Game!!");
            System.out.println();
            System.out.print("Please guess the year. (0 - 2199): "+ranDateYear);

            while(userInput.hasNext()) { 

                if(userInput.hasNextInt()) { 

                    playerGuess = userInput.nextInt();

                    if(playerGuess > ranDateYear) { 

                        System.out.println("Out of bounds, too high: " + playerGuess + " (Randomized Number: " +  ranDateYear+ ")");
                        System.out.println();
                        System.out.println("Try again!");
                        System.out.print("Please guess the year (0 - 2199): ");

                    }else if(playerGuess < ranDateYear) {

                        System.out.println("Out of bounds, too low:  " + playerGuess + " (Randomized Number: " + ranDateYear + ")");
                        System.out.println();
                        System.out.println("Try again!");
                        System.out.print("Please guess the year (0 - 2199): "); 

                    }else{      

                        if (playerGuess == ranDateYear) {

                            System.out.println("Correct! It is a match:  " + playerGuess + " (Randomized Number: " + ranDateYear + ")");
                            System.out.println();
                            System.out.print("Play again? (Y/N): ");

                            while (true) {
                                        String input = userInput.next();
                                        if (input.equalsIgnoreCase("y")) {
                                           keepPlaying = true;break;
                                        } 
                                        else if (input.equalsIgnoreCase("n")){
                                            keepPlaying = false;break;
                                        } 
                            }
break;
                        }                           
                    }   

                } else {

                    System.out.println("Invalid input, not a number.");
                    System.out.print("Enter another number: ");
                    userInput.next();

                }
            }   
        }

        System.out.println("The end. Thank you for playing!");
    }
}

答案 2 :(得分:0)

if (userInput.hasNext()) {

    String input = userInput.next();    
    if (input.equalsIgnoreCase("y")) {    
          keepPlaying = true;   
          askAgain = false;
    } else if (input.equalsIgnoreCase("n")){    
          keepPlaying = false;  
          askAgain = false;  
    } else {    
          askAgain = true;
    }    
    break;                              
}

请参阅此http://api.dataatwork.org/v1/spec/

修改

while(userInput.hasNext()){替换为告诉用户输入的变量,因此在askAgain循环中,此变量将变为false。

试试这个:

    while(enteringInput) {

        try{
                playerGuess = userInput.nextInt();

                if(playerGuess > ranDateYear) { 

                    System.out.println("Out of bounds, too high: " + playerGuess + " (Randomized Number: " +  ranDateYear+ ")");
                    System.out.println();
                    System.out.println("Try again!");
                    System.out.print("Please guess the year (0 - 2199): ");

                }else if(playerGuess < ranDateYear) {

                    System.out.println("Out of bounds, too low:  " + playerGuess + " (Randomized Number: " + ranDateYear + ")");
                    System.out.println();
                    System.out.println("Try again!");
                    System.out.print("Please guess the year (0 - 2199): "); 

                }else{      

                    if (playerGuess == ranDateYear) {

                        System.out.println("Correct! It is a match:  " + playerGuess + " (Randomized Number: " + ranDateYear +")");


 System.out.println();
                        System.out.print("Play again? (Y/N): ");

                        while (askAgain) {           

                            System.out.print(askAgain);
                            while (userInput.hasNext()) {

                    String input = userInput.nextLine();    
                    if (input.equalsIgnoreCase("y")) {    
                        keepPlaying = true;   
                        askAgain = false;
                    } else if (input.equalsIgnoreCase("n")){                
                    keepPlaying = false;  
                        askAgain = false;
                                            enteringInput = false;  
                    } else {    
                        askAgain = true;
                    }    
                        break;   
                            }
                        }
                    }                           
                }  
                }catch(Exception e){
                   System.out.println("Invalid input");
                   userInput.nextLine();
                }                    
        }