gpt4 book ai didi

java - 循环和字符串生成器

转载 作者:行者123 更新时间:2023-12-01 14:03:24 24 4
gpt4 key购买 nike

我正在尝试创建一个类似于刽子手的猜谜游戏。我已经完成了大部分代码并且编译得很好。然而,当我运行我的代码时,它似乎并没有完全运行该程序。我猜这与 StringBuilder 有关。欢迎任何反馈。谢谢!

import java.lang.String;
import javax.swing.JOptionPane;
import java.lang.Character;
import java.lang.String;
import java.lang.StringBuilder;

public class GuessGame {
public static void main(String[] args) {
String answer = "G1123 I4 L1123!";
String guessAnswer = "Goose Is Loose!";
StringBuilder sb = new StringBuilder(15);

sb.append("G**** I* L****!");
/*
sb.append("012345678901234");
*/
//create string array and split into
String entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
//sorting loop method to find if letter exists

for(int i = 0; i < 26;i++){
if (entry.equals("e")){
sb.replace(4,5,"e");
sb.replace(13,14,"e");
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
//sorting loop method to find if letter exists

}else if (entry.equals("o")){
sb.replace(1,3,"oo");
sb.replace(10,12,"oo");
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
//sorting loop method to find if letter exists

}else if(entry.equals("s")){
sb.replace(3,4,"s");
sb.replace(12,13,"s");
sb.replace(7,8,"s");
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
//sorting loop method to find if letter exists

}else {
JOptionPane.showMessageDialog(null, "Incorrect letter guess! Try again. ");
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
//sorting loop method to find if letter exists

}
}


if(answer == guessAnswer){
JOptionPane.showMessageDialog(null, "You win!!! Answer is: " + sb);
}else{
JOptionPane.showMessageDialog(null, "You Lose: Answer is: " + sb);
}
}
}

//根据汤姆的建议进行编辑:

import java.lang.String;
import javax.swing.JOptionPane;
import java.lang.Character;
import java.lang.String;
import java.lang.StringBuilder;

public class GuessGame {
public static void main(String[] args) {
String answer = "G1123 I4 L1123!";
String guessAnswer = "Goose Is Loose!";
StringBuilder sb = new StringBuilder(15);

sb.append("G**** I* L****!");
/*
sb.append("012345678901234");
*/
//create string array and split into
String entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
//sorting loop method to find if letter exists

for(int i = 0; i < 26;i++){
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);

if (entry.equals("e")){
sb.replace(4,5,"e");
sb.replace(13,14,"e");

}else if (entry.equals("o")){
sb.replace(1,3,"oo");
sb.replace(10,12,"oo");


}else if(entry.equals("s")){
sb.replace(3,4,"s");
sb.replace(12,13,"s");
sb.replace(7,8,"s");


}else {
JOptionPane.showMessageDialog(null, "Incorrect letter guess! Try again. ");
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);


}
}


if (sb.toString().equals(guessAnswer)){
JOptionPane.showMessageDialog(null, "You win!!! Answer is: " + sb);
}else{
JOptionPane.showMessageDialog(null, "You Lose: Answer is: " + sb);
}
}
}

//最终代码

import java.lang.String;
import javax.swing.JOptionPane;
import java.lang.Character;
import java.lang.String;
import java.lang.StringBuilder;

public class GuessGame {
public static void main(String[] args) {
String answer = "G1123 I4 L1123!";
String guessAnswer = "Goose Is Loose!";
StringBuilder sb = new StringBuilder(15);

sb.append("G**** I* L****!");
/*
sb.append("012345678901234");
*/
//create string array and split into
String entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
//sorting loop method to find if letter exists

for(int i = 0; i < 26;i++){
entry = JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);
if(sb.toString().equals(guessAnswer)){
JOptionPane.showMessageDialog(null, "You win!!! Answer is: " + sb);
break;

}else if (entry.equals("e")){
sb.replace(4,5,"e");
sb.replace(13,14,"e");

}else if (entry.equals("o")){
sb.replace(1,3,"oo");
sb.replace(10,12,"oo");


}else if(entry.equals("s")){
sb.replace(3,4,"s");
sb.replace(12,13,"s");
sb.replace(7,8,"s");


}else{
JOptionPane.showMessageDialog(null, "Incorrect letter guess! Try again. ");
JOptionPane.showInputDialog(null,"You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);


}
}
if ((sb.toString().equals(guessAnswer)) == false){
JOptionPane.showMessageDialog(null, "You Lose: Answer is: " + sb);

}

}
}

最佳答案

有一些事情需要改变。

首先,如前所述,您的结束条件应检查 StringBuilder sb 值。

    if (sb.toString().equals(guessAnswer)) {

此外,每次显示 JOptionPane 时,您都需要更改 entry 包含的内容,如下所示

entry = JOptionPane.showInputDialog(null, "You have 25 chances to guess the phrase! Guess one letter at a time!\n" + sb);

现在,执行此操作并单击取消将引发NullPointerException,因此您需要检查entry是否为null 停止。

应该更频繁地检查某人是否获胜,这样您就不必等待 25 个回合才收到“获胜”消息。在有人输入有效字母后添加 isWin(sb) 方法是一个好方法。

您还忘记了,当有人输入 e 时,更改 Loose! 中的最后一个 e

关于java - 循环和字符串生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19143903/

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