游戏的JOption Panel

时间:2017-05-25 14:06:32

标签: java swing

我正在尝试制作游戏,在开始时,第一部分很好,但是,我无法使第二个问题正常工作,我希望它能显示:rules_yes如果输入是(不区分大小写),则rules_no如果还有其他内容,则显示目前,无论我为规则输入什么,它只运行rules_yes。我可以获得一些关于如何使其工作的反馈吗?

numpy array

3 个答案:

答案 0 :(得分:0)

因为您将(Y / N)问题值添加到“yes_no”参数,但是您的'if-else'条件使用'input',因此输入未初始化,这意味着它等于0.这就是为什么您的问题总是返回YES。

更改您的代码:

public static void main(String[] args) {

    String user_name;
    String name_answer;
    String yes_no;
    String rules_yes;
    String rules_no;
    char[] input;
    char Yes = 0;

    {
        user_name = JOptionPane.showInputDialog("Enter Your Name");

        name_answer = ("Hello " + user_name + " Welcome to Tic-Tac-Toe, Click OK to Start");

        JOptionPane.showMessageDialog(null, name_answer);
    }

    {
        yes_no = JOptionPane.showInputDialog("Would you like the rules (Y/N)");

        input = yes_no.toCharArray();
        if (input[0] == Yes) {

            rules_yes = ("Yes? The Rules: X goes first, each player takes turns to put their symbol in one of nine boxes, you cannot put your symbol in a box which already contains a symbol, the first one to make a row of three wins");

            JOptionPane.showMessageDialog(null, rules_yes);
        } else {

            rules_no = ("No? Well too bad, here are the rules, The Rules: X goes first, each player takes turns to put their symbol in one of nine boxes, you cannot put your symbol in a box which already contains a symbol, the first one to make a row of three wins");

            JOptionPane.showMessageDialog(null, rules_no);
        }
    }
}

答案 1 :(得分:0)

为什么使用"输入"对话框?

更简单的解决方案是使用"消息"对话框"是","否"用户点击的按钮。

阅读How to Make Dialogs上Swing教程中的部分,了解更多信息和示例。

答案 2 :(得分:0)

您的代码存在许多问题,您可以采取许多措施来简化代码。

  1. yesYes未初始化,导致您的程序失败。
  2. 您可以将yes_no声明为String,然后使用if (yes_no.equalsIgnoreCase("y");(而不是使用char yeschar Yes
  3. 这不会影响你的程序,但你的行之间有很多间距,这使它看起来比它更多。
  4. input是不必要的,因此您可以将其删除。
  5. 所以你的最终代码可能如下所示:

    import javax.swing.JOptionPane;
    
    public class ScratchPaper {
        public static void main(String[]args) {
            String userName;
            String nameAnswer;
            String rulesYes;
            String rulesNo;
            String yesNo;
    
            userName = JOptionPane.showInputDialog("Enter Your Name");
            nameAnswer = ("Hello " + userName + " Welcome to Tic-Tac-Toe, Click OK to Start");
            JOptionPane.showMessageDialog( null, nameAnswer );
            yesNo = JOptionPane.showInputDialog("Would you like the rules (Y/N)");
    
    
            if (yesNo.equalsIgnoreCase("y")) 
            {
                rulesYes = ("Yes? The Rules: X goes first, each player takes turns to put their symbol in one of nine boxes, you cannot put your symbol in a box which already contains a symbol, the first one to make a row of three wins");
                JOptionPane.showMessageDialog( null, rulesYes );
            }
            else {
                rulesNo = ("No? Well too bad, here are the rules, The Rules: X goes first, each player takes turns to put their symbol in one of nine boxes, you cannot put your symbol in a box which already contains a symbol, the first one to make a row of three wins");
                JOptionPane.showMessageDialog( null, rulesNo );
            }
        }
    }
    

    如果您有任何疑问,请在下面发表评论,我会尽快回答。谢谢!