输入正确的输入后需要退出循环

时间:2016-09-20 19:43:45

标签: java

请参阅下面的测验游戏代码。我一直试图弄清楚如何在选项菜单中正确编码循环2天。一旦我知道如何正确编码它,我就可以在其他问题的代码中编写代码。

我现在编码的方式现在停留在“选择无效,请再试一次”。无限。

有人请帮我正确的代码。 :(

我能找到的与此相关的一切都利用了扫描仪输入而不是JOptionPane。

package team1;

//this is required for JOptionPane to work
import javax.swing.JOptionPane; 

//this allows for exception throwing
import java.io.*;

public class GameV2 {

    public static void main(String[] args) throws IOException {

/**
* Version 2 Updates:
* - added 2 more sets of questions and answers
* - created a decision structure to determine the path of the game based on +
    the user's menu choice
* - created a decision structure to indicate whether to the user whether an +
    answer is correct or incorrect.
* - added point values to each question and a totalScore accumulator which +
    displays at the end of the game.
*/

        // display an introduction to the game.
        JOptionPane.showMessageDialog(null, "Welcome to Team 1's Game Version 2!");

        // prompt the user for his/her first name
        String firstname;
        firstname = JOptionPane.showInputDialog("What is your first name?");

        // prompt the user for his/her last name
        String lastname;
        lastname = JOptionPane.showInputDialog("What is your last name?");

        // display a customized hello message to the user
        JOptionPane.showMessageDialog(null, "Hi, " + firstname + " " + lastname + "!");

        **// create a menu and display it to the user
        // then ask the user to choose an option
        String menu = "1) See Rules and Play the Game\n"
                    + "2) Play the Game\n"
                    + "3) Exit\n"
                    + "Please enter your choice: (1 or 2 or 3) ";

        String userChoice = JOptionPane.showInputDialog(menu);

        JOptionPane.showMessageDialog(null, "You chose option " + userChoice); 

        // display the rules
        String rules = "Rules:\n"

                + "The game will display total 3 multiple choice questions," +
                " with 4 possible answers per question.\n"
                + "Once you answer the question, the correct answer will be displayed" +
                " and the game will advance to the next question.\n"
                + "If you answer the question correctly, you will gain a point.\n"
                + "Each point is added to a total score that will be displayed at the" + 
                "end of the game.\n";

    // declare an integer that reads the user input
    int numericChoice = Integer.parseInt(userChoice);
    boolean valid = (numericChoice == 1 || numericChoice == 2 || numericChoice == 3);

        if (numericChoice == 1){
         // display the rules then start the game
        JOptionPane.showMessageDialog(null, rules);
        }
        else if (numericChoice == 2){
         // start the game
        JOptionPane.showMessageDialog(null, "Let's play the game.\n");
        }
        else if (numericChoice == 3){
         // exit the game
        System.exit(0);
        }
        while (!valid){
        JOptionPane.showMessageDialog(null, "Ivalid selection, please try again");
        JOptionPane.showInputDialog(menu);
        continue;
        }

1 个答案:

答案 0 :(得分:1)

为了让你的实现像你想要的那样工作,你应该重写你的循环,例如:如下:

    boolean valid = false;
    do {
        final String userChoice = JOptionPane.showInputDialog(menu);
        final int numericChoice = Integer.parseInt(userChoice);
        JOptionPane.showMessageDialog(null, "You chose option " + userChoice);
        if (numericChoice == 1) {
            valid = true;
            // display the rules then start the game
            JOptionPane.showMessageDialog(null, rules);
        } else if (numericChoice == 2) {
            valid = true;
            // start the game
            JOptionPane.showMessageDialog(null, "Let's play the game.\n");
        } else if (numericChoice == 3) {
            valid = true;
            // exit the game
            System.exit(0);
        } else {
            JOptionPane.showMessageDialog(null, "Ivalid selection, please try again");
        }
    } while (!valid);

您还应该删除

    String userChoice = JOptionPane.showInputDialog(menu);

    JOptionPane.showMessageDialog(null, "You chose option " + userChoice); 
在循环之前

因为它们将在其中执行。

如果你这样做,那么你的循环有以下周期:

  1. 获取并存储用户输入(userChoicenumericChoice *)
  2. 向用户显示他们的输入
  3. 处理输入
    • 如果1或2或3设置为有效,则根据选择继续
    • 显示错误并从步骤1开始重复
  4. *当userChoice无法解析为数字时,您应该考虑处理案例