0

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

} 

}
}
4

2 回答 2

1

有几件事需要改变。

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

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

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

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

现在,这样做并点击Cancel会抛出一个NullPointerException,所以你需要检查是否entry停止null

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

你也忘记了,当有人进入时,e改变最后一个eLoose!

于 2013-10-02T18:30:25.520 回答
0

你永远不会修改答案变量

代替

if(answer == guessAnswer)

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

另外,我认为它仍然行不通,但由于其他原因。把它放在最后找出原因:

System.out.println(sb.toString());
于 2013-10-02T18:25:01.713 回答