I am trying to make a game where you unscramble words. But I cannot seem to get it to properly check if the user's guess is equal to the actual answer. It seems to no matter what have the actual word not equal to the users guess, also while your here.
Is there any other way to pause my code and wait for a users input other than having a while loop?
import javax.swing.JOptionPane;
public class Loop extends CreateWindow{
private static final long serialVersionUID = 1L;
public static boolean running = true;
public static void GameStart(){
if(running){
int wordNum = 0;
for (int i=0;i<=9;i++){
field.setText("");
setScrambledWord(i);
while(enter == false){
System.out.println(userAnswer);
System.out.println(enter);
System.out.println(score);
}
userAnswer = area.getText();
if(userAnswer == normalWords[wordNum]){
JOptionPane.showMessageDialog(null, "legos", userAnswer, JOptionPane.OK_OPTION);
}else if(userAnswer != normalWords[wordNum]){
JOptionPane.showMessageDialog(null, "sogel", userAnswer, JOptionPane.OK_OPTION);
}
area.setText("");
enter = false;
wordNum++;
}
}
}
public static void main(String[] args){
new CreateWindow();
running = true;
GameStart();
}
}
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class CreateWindow extends JFrame {
private static final long serialVersionUID = 1L;
JPanel panel = new JPanel();
public static int score = 0;
static JTextArea area = new JTextArea(10,40);
static JTextField field = new JTextField(40);
JButton button = new JButton("Press to Enter Answer");
public static boolean enter;
public static String userAnswer;
public static String[] normalWords = {
"yellow",
"alphabet",
"tangent",
"elephant",
"amusement",
"cat",
"eerie",
"wonder",
"shadows"
};
public static String[] scrambledWords = {
"lloeyw",
"lphaabte",
"natgnte",
"leehpnta",
"mauseenmt",
"tca",
"reeei",
"dowenr",
"shawdos"
};
CreateWindow(){
setSize(500,300);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
field.setEditable(false);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
enter = true;
System.out.println("You clicked the button");
System.out.println(enter);
}
});
add(panel);
panel.add(field, BorderLayout.PAGE_START);
panel.add(area, BorderLayout.CENTER);
panel.add(button, BorderLayout.PAGE_END);
setVisible(true);
}
public static void setScrambledWord(int i){
field.setText(scrambledWords[i]);
}
public void clearArea(){
area.setText("");
}
}