我正在尝试创建一个类似于刽子手的猜谜游戏。我已经完成了大部分代码,并且可以正常编译。但是,当我运行我的代码时,它似乎并没有完全通过程序运行。我猜这与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);
}
}
}