首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在用户输入错误后循环特定区域的代码?(Java)

如何在用户输入错误后循环特定区域的代码?(Java)
EN

Stack Overflow用户
提问于 2016-12-03 09:57:50
回答 8查看 771关注 0票数 18

我一直在用Java开发一个小的石头布剪刀游戏,其中输赢的条件是基于计算机或玩家是否赢得了五场比赛。如果有错误的输入,我不确定如何让程序在用户输入时循环。

下面是我遇到麻烦的代码

我尝试循环的部分是带有"else if (determination.equals("Y")) {“

import java.util.*;

public class Rock_Paper_Scissors {

  public static void main(String arg[]) {
    boolean loopGameStart = true;

    while (loopGameStart) {
      System.out.println("Welcome to Rock Paper Scissors, a game programmed "
                       + "by Daniel Park. Would you like to start? (Y/N)");
      Scanner userInput = new Scanner(System.in);
      String determination = userInput.next();

      if (determination.equals("N")) {
        System.out.println("Please, do reconsider...");
        loopGameStart = true;
      } else if (determination.equals("Y")) {
        Random rand = new Random();
        int n = rand.nextInt(3) + 1;
        // 1 = Rock, 2 = Paper, 3= Scissor
        int humanWinCount = humanWinCount();
        int computerWinCount = computerWinCount();
        System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
        Scanner userRPS = new Scanner(System.in);
        int choiceRPS = userRPS.nextInt();
        while ((humanWinCount < 5) && (computerWinCount < 5)) {
          if (choiceRPS == 0) {
            if (n == 1) {
              System.out.println("TIE!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else if (n == 2) {
              System.out.println("LOSS!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              computerWinCount = computerWinCount + 1;
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else if (n == 3) {
              System.out.println("WIN!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              humanWinCount = humanWinCount + 1;
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else {
              System.out.println("I do not understand... Try Again.");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            }
          } else if (choiceRPS == 1) {
            if (n == 1) {
              System.out.println("WIN!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              humanWinCount = humanWinCount + 1;
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else if (n == 2) {
              System.out.println("TIE!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else if (n == 3) {
              System.out.println("LOSS!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              computerWinCount = computerWinCount + 1;
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else {
              System.out.println("I do not understand... Try again.");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            }
          } else if (choiceRPS == 2) {
            if (n == 1) {
              System.out.println("LOSS");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              computerWinCount = computerWinCount + 1;
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else if (n == 2) {
              System.out.println("WIN!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              humanWinCount = humanWinCount + 1;
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else if (n == 3) {
              System.out.println("TIE!!");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            } else {
              System.out.println("I do not understand... Try again.");
              System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
              System.out.println("Computer: " + computerWinCount + " rounds won");
              System.out.println("You: " + humanWinCount + " rounds won");
              choiceRPS = userRPS.nextInt();
            }
          }
        }

        if (humanWinCount == 5) {
          System.out.println("Congratulations, you win!!");
          System.out.println("Computer: " + computerWinCount + " rounds won");
          System.out.println("You: " + humanWinCount + " rounds won");
          System.out.println("Would you like to try again? (Y/N)");
          Scanner continueOrNot = new Scanner(System.in);
          String contOrNot = continueOrNot.next();
          if (contOrNot.equals("Y")) {
            loopGameStart = true;
          } else if (contOrNot.equals("N")) {
            System.out.println("Okay, goodbye!!");
            loopGameStart = false;
          }
        }

        if (computerWinCount == 5) {
          System.out.println("Boohoo, you lost!!");
          System.out.println("Computer: " + computerWinCount + " rounds won");
          System.out.println("You: " + humanWinCount + " rounds won");
          System.out.println("Would you like to try again? (Y/N)");
          Scanner continueOrNot = new Scanner(System.in);
          String contOrNot = continueOrNot.next();
          if (contOrNot.equals("Y")) {
            loopGameStart = true;
          } else if (contOrNot.equals("N")) {
            System.out.println("Okay, goodbye!!");
            loopGameStart = false;
          }
        }
      } else {
        System.out.println("I do not understand, please try again!");
      }
    }
  }

  public static int humanWinCount() {
      int x = 0;
      return x;
  }

  public static int computerWinCount() {
      int c = 0;
      return c;
  }
}
EN

回答 8

Stack Overflow用户

发布于 2016-12-03 22:28:39

请不要在一个主函数中编写所有代码,或者到处重复相同的代码,等等。

我完全重建了你的代码,也实现了@Austin建议的循环。现在你应该像你一样工作,或者非常接近。享受:

import java.util.*;

public class Rock_Paper_Scissors
{
    public static void main( String arg[] )
    {
        Rock_Paper_Scissors game = new Rock_Paper_Scissors();
        game.startLogic();
    }

    private Random  rand;
    private Scanner inputScanner;
    private boolean loopGameStart;

    private int humanWinCount;
    private int computerWinCount;

    public Rock_Paper_Scissors()
    {
        this.rand          = new Random();
        this.inputScanner  = new Scanner( System.in );
        this.loopGameStart = true;

        humanWinCount    = 0;
        computerWinCount = 0;
    }

    public void startLogic()
    {
        System.out.print( "Welcome to Rock Paper Scissors, a game programmed by Daniel Park. " );
        this.askUntilGetAnAnswer( "N" );
    }

    private int humanWinCount()
    {
        int x = 0;
        return x;
    }

    private int computerWinCount()
    {
        int c = 0;
        return c;
    }

    private void askUntilGetAnAnswer( String determination )
    {
        while( this.loopGameStart )
        {
            if( !determination.equals( "Y" ) )
            {
                System.out.println( "\nWould you like to start? (Y/N)" );
                determination = this.inputScanner.next();
            }

            if( determination.equals( "N" ) )
            {
                System.out.println( "Please, do reconsider..." );
                this.loopGameStart = false;
            }
            else if( determination.equals( "Y" ) )
            {
                this.processCoreGameLogic();
            }
            else
            {
                System.out.print( "I do not understand, please try again!" );
            }
        }
    }

    private void processCoreGameLogic()
    {
        int choiceRPS;
        System.out.println( "Choose 0, 1, or 2 (Rock/Paper/Scissor)" );

        // 1 = Rock, 2 = Paper, 3= Scissor
        this.humanWinCount    = this.humanWinCount();
        this.computerWinCount = this.computerWinCount();

        while( ( this.humanWinCount < 5 ) && ( this.computerWinCount < 5 ) )
        {
            try
            {
                choiceRPS = inputScanner.nextInt();

                if( choiceRPS >= 0 && choiceRPS <= 2 )
                {
                    this.playing( choiceRPS );
                }
            }
            catch( InputMismatchException e )
            {
                System.out.print( "I do not understand, please try again!" );
                inputScanner.next();
            } 

        }

        this.endGame();
    }

    private void endGame()
    {
        String contOrNot = "";

        if( this.humanWinCount == 5 )
        {
            System.out.println( "\nCongratulations, you win!!" );
            System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
            System.out.println( "You: " + this.humanWinCount + " rounds won" );
            System.out.println( "Would you like to try again? (Y/N)" );

            contOrNot = this.inputScanner.next();
        }

        if( this.computerWinCount == 5 )
        {
            System.out.println( "\nBoohoo, you lost!!" );
            System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
            System.out.println( "You: " + this.humanWinCount + " rounds won" );
            System.out.println( "Would you like to try again? (Y/N)" );

            contOrNot = this.inputScanner.next();
        }

        if( contOrNot.equals( "N" ) )
        {
            System.out.println( "Okay, goodbye!!" );
            this.loopGameStart = false;
        }
        else
        {
            askUntilGetAnAnswer( contOrNot );
        }
    }

    private void playing( int choiceRPS )
    {
        int randomInteger = this.rand.nextInt( 3 ) + 1;

        // choiceRPS 0 -> tie  1, loss 2, win  3
        // choiceRPS 1 -> win  3, tie  1, loss 2
        // choiceRPS 2 -> loss 2, win  3, tie  1
        //
        switch( choiceRPS*3 + randomInteger )
        {
            case 1:
            case 5:
            case 9:
            {
                show_tie();
                break;
            }
            case 2:
            case 6:
            case 7:
            {
                show_loss();
                break;
            }
            case 3:
            case 4:
            case 8:
            {
                show_win();
                break;
            }
            default:
            {
                System.out.println( "    I do not understand... Try again." );
                System.out.println( "    Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
                System.out.println( "    Computer: " + this.computerWinCount + " rounds won" );
                System.out.println( "    You: " + this.humanWinCount + " rounds won" );
            }
        }
    }

    private void show_loss()
    {
        System.out.println( "LOSS" );
        System.out.println( "Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
        this.computerWinCount = this.computerWinCount + 1;
        System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
        System.out.println( "You: " + this.humanWinCount + " rounds won" );
    }

    private void show_win()
    {
        System.out.println( "WIN!!" );
        System.out.println( "Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
        this.humanWinCount = this.humanWinCount + 1;
        System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
        System.out.println( "You: " + this.humanWinCount + " rounds won" );
    }

    private void show_tie()
    {
        System.out.println( "TIE!!" );
        System.out.println( "Choose 0, 1, or 2 again (Rock/Paper/Scissor)" );
        System.out.println( "Computer: " + this.computerWinCount + " rounds won" );
        System.out.println( "You: " + this.humanWinCount + " rounds won" );
    }
}
票数 13
EN

Stack Overflow用户

发布于 2016-12-03 10:13:10

您可以使用do while

int choiceRPS;
 do{
    choiceRPS = userRPS.nextInt();
}while(choiceRPS<0||choiceRPS>2);
票数 7
EN

Stack Overflow用户

发布于 2016-12-09 20:00:29

所以很明显,代码不是模块化的,代码中有冗余的语句。它们可能还在那里,因为我所做的就是修复逻辑,并对引入的更改的代码进行注释-

public class Rock_Paper_Scissors {

    public static void main(String arg[]) {
        boolean loopGameStart = true;
        while (loopGameStart) {
            System.out.println(
                    "Welcome to Rock Paper Scissors, a game programmed by Daniel Park. Would you like to start? (Y/N)");
            Scanner userInput = new Scanner(System.in);
            String determination = userInput.next();

            switch (determination) {
                case "N":
                    System.out.println("Please, do reconsider...");
                    loopGameStart = true; // you might not want the player to be trapped here, so change it to `false` rather
                    break;
                case "Y":
                    /** 0 = Rock, 1 = Paper, 2 = Scissor */
                    int humanWinCount = 0;
                    int computerWinCount = 0;

                    System.out.println("Choose 0, 1, or 2 (Rock/Paper/Scissor)");
                    Scanner userRPS = new Scanner(System.in);
                    int userChoice = userRPS.nextInt();

                    while ((humanWinCount < 5) && (computerWinCount < 5)) { // either of them reaches 5
                        int computerChoice = new Random().nextInt(3); // this is fun, no more static computer choice
                        if (userChoice == computerChoice) { // break quick if its a tie
                            System.out.println("TIE!!");
                            displayStats(computerWinCount, humanWinCount);
                            askChoice();
                        } else {
                            switch (userChoice) { //based on userChoice and computerChoice combination
                                case 0:
                                    if (computerChoice == 1) {
                                        System.out.println("LOSS!!");
                                        displayStats(computerWinCount++,
                                                humanWinCount); // incrementing the computerCount as well
                                        askChoice();
                                    } else if (computerChoice == 2) {
                                        System.out.println("WIN!!");
                                        displayStats(computerWinCount,
                                                humanWinCount++); // incrementing the humanCount as well
                                        askChoice();
                                    }
                                    break;
                                case 1:
                                    if (computerChoice == 0) {
                                        System.out.println("WIN!!");
                                        displayStats(computerWinCount, humanWinCount++);
                                        askChoice();
                                    } else if (computerChoice == 2) {
                                        System.out.println("LOSS!!");
                                        displayStats(computerWinCount++, humanWinCount);
                                        askChoice();
                                    }
                                    break;
                                case 2:
                                    if (computerChoice == 0) {
                                        System.out.println("LOSS");
                                        displayStats(computerWinCount++, humanWinCount);
                                        askChoice();
                                    } else if (computerChoice == 1) {
                                        System.out.println("WIN!!");
                                        displayStats(computerWinCount, humanWinCount++);
                                        askChoice();
                                    }
                                    break;
                                default:
                                    System.out.println("I do not understand... Try again.");
                                    displayStats(computerWinCount, humanWinCount);
                                    askChoice();
                                    break;

                            }
                        }

                        userChoice = userRPS.nextInt(); //next iteration input only required once
                    }

                    if (humanWinCount == 5) {
                        System.out.println("Congratulations, you win!!");
                    } else {
                        System.out.println("Boohoo, you lost!!");
                    }
                    displayStats(computerWinCount, humanWinCount);
                    System.out.println("Would you like to try again? (Y/N)");
                    Scanner continueOrNot = new Scanner(System.in);
                    String contOrNot = continueOrNot.next();
                    if (contOrNot.equals("Y")) {
                        loopGameStart = true;
                    } else if (contOrNot.equals("N")) {
                        System.out.println("Okay, goodbye!!");
                        loopGameStart = false;
                    }
                    break;


                default:
                    System.out.println("I do not understand, please try again!");
                    break;
            }
        }
    }

    private static void askChoice() {
        System.out.println("Choose 0, 1, or 2 again (Rock/Paper/Scissor)");
    }

    private static void displayStats(int computerWinCount, int humanWinCount) {
        System.out.println("Computer: " + computerWinCount + " rounds won");
        System.out.println("You: " + humanWinCount + " rounds won");
    }
}

代码解释将是我留下的一些你不会理解的东西。因此,请进一步询问您可能想知道的有关使用它的原因的任何信息。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40943472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档