gpt4 book ai didi

java - 游戏的 JOption 面板

转载 作者:太空宇宙 更新时间:2023-11-04 11:27:28 25 4
gpt4 key购买 nike

我正在尝试制作一个游戏,一开始,第一部分很好,但是,我无法解决第二个问题,我希望它显示:如果输入"is"(不区分大小写),则显示规则_yes,如果写入其他内容,则显示规则_no。目前,无论我输入什么规则,它都只运行rules_yes。我可以获得一些关于如何实现这项工作的反馈吗?

{
String user_name;
String name_answer;
String yes_no;
String rules_yes;
String rules_no;
char input;
private char yes;
private char Yes;

{
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)");


if (input == Yes || input == 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. yesYes 未初始化,这导致您的程序失败。
  2. 您可以将 yes_no 声明为 String,然后使用 if (yes_no.equalsIgnoreCase("y");(而不是使用 char yeschar Yes)
  3. 这不会影响您的程序,但行之间的间距很大,这使得它看起来比实际要多得多。
  4. input是不必要的,所以你可以删除它。

所以你的最终代码可能如下所示:

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 );
}
}
}

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

关于java - 游戏的 JOption 面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44182596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com