2016-09-20 77 views
0

请参阅我的代码以获取问答游戏。我一直试图弄清楚如何在选项菜单正确编码循环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

你永远不会重新分配'valid'一次为假,所以它永远是错误的。 – Compass

回答

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

*您应该想想办案时userChoice不能被解析成多个

+0

非常感谢,现在我很开心。 –