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

0

回答変数を変更することはありません

交換

if(answer == guessAnswer)

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

また、まだうまくいかないと思いますが、他の理由があります。これを最後に置いて、理由を確認してください。

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