而循环不让我从标签中输入文字
如果用户输入单词字符与随机生成的单词字符相同,我正在尝试打破循环。如果任何单词的字符与随机生成的单词字符都不匹配,则应在该标签中显示无与伦比的单词字符,然后跳过它并检查其余部分;然后,让用户再次尝试以最多6次猜测正确的单词。如果在上一次尝试循环中达到限制,则爆发。
我试图打破循环,但它不允许我从标签中输入,只是将上一个输入的用户保留在用户中,然后一次又一次地匹配。显示柜台为6次静置,它在显示匹配和无与伦比的字符上效果很好。
这是我的Java代码
try {
BufferedReader reader = new BufferedReader(new FileReader("E:\\WordyGame\\src\\wordygame\\targetWords.txt"));
String line = reader.readLine();
List<String> words = new ArrayList<String> ();
while (line != null) {
String[] wordsLine = line.split(" ");
for (String word: wordsLine) {
words.add(word);
}
line = reader.readLine();
}
Random rand = new Random(System.currentTimeMillis());
//getting random word from txt file
String randomWord = words.get(rand.nextInt(words.size()));
System.out.println("random word is : " + randomWord);
String check_word = randomWord.trim().toLowerCase().toString();
//getting user input from textfield
//tp count limited attempts
int count = 0;
while (count<6) {
String take_word = type_word.getText().toLowerCase().toString();
System.out.println("user inpur is " + take_word);
//dividing word into char and then each char to strings
//user input word
char first = take_word.charAt(0);
String first1 = String.valueOf(first);
char second = take_word.charAt(1);
String second1 = String.valueOf(second);
char third = take_word.charAt(2);
String third1 = String.valueOf(third);
char fourth = take_word.charAt(3);
String fourth1 = String.valueOf(fourth);
char fifth = take_word.charAt(4);
String fifth1 = String.valueOf(fifth);
//random word from file
char first2 = check_word.charAt(0);
String first22 = String.valueOf(first2);
char second2 = check_word.charAt(1);
String second22 = String.valueOf(second2);
char third2 = check_word.charAt(2);
String third22 = String.valueOf(third2);
char fourth2 = check_word.charAt(3);
String fourth22 = String.valueOf(fourth2);
char fifth2 = check_word.charAt(4);
String fifth22 = String.valueOf(fifth2);
if (take_word.length()<7) {
if (first1.trim().equals(first22)) {
word_1.setText(first1);
word_1.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 1 doesnt match");
continue;
}
if (second1.trim().equals(second22)) {
word_2.setText(second1);
word_2.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 2 doesnt match");
continue;
}
if (third1.trim().equals(third22)) {
word_3.setText(third1);
word_3.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 3 doesnt match");
continue;
}
if (fourth1.trim().equals(fourth22)) {
word_4.setText(fourth1);
word_4.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 4 doesnt match");
continue;
}
if (fifth1.trim().equals(fifth22)) {
word_5.setText(fifth1);
word_5.setForeground(Color.red);
//displaying charchter on GUI
break;
} else {
JOptionPane.showMessageDialog(this, "doesnt 5 match");
count++;
continue;
}
} else {
JOptionPane.showMessageDialog(this, "only 5 char word are allowed Number");
}
if (check_word.trim().equals(take_word)) {
JOptionPane.showMessageDialog(this, "YOU guessed the number in first attempt");
break;
} else {
count++;
}
}
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
I am trying to break loop if user input word character are same as random generated word character. In case any char of word isn't matching with random generated word character, then it should display unmatched in that label and skip it and check the rest; then lets user try again to guess the right word with same condition maximum 6 times. If limit is reached in last try loop break out.
I tried to break loop, but its not letting me input from label and just keep the previous entry from user and matches it again and again. Displayed counter as 6 rest it working well on displaying matched and unmatched character.
here is my java code
try {
BufferedReader reader = new BufferedReader(new FileReader("E:\\WordyGame\\src\\wordygame\\targetWords.txt"));
String line = reader.readLine();
List<String> words = new ArrayList<String> ();
while (line != null) {
String[] wordsLine = line.split(" ");
for (String word: wordsLine) {
words.add(word);
}
line = reader.readLine();
}
Random rand = new Random(System.currentTimeMillis());
//getting random word from txt file
String randomWord = words.get(rand.nextInt(words.size()));
System.out.println("random word is : " + randomWord);
String check_word = randomWord.trim().toLowerCase().toString();
//getting user input from textfield
//tp count limited attempts
int count = 0;
while (count<6) {
String take_word = type_word.getText().toLowerCase().toString();
System.out.println("user inpur is " + take_word);
//dividing word into char and then each char to strings
//user input word
char first = take_word.charAt(0);
String first1 = String.valueOf(first);
char second = take_word.charAt(1);
String second1 = String.valueOf(second);
char third = take_word.charAt(2);
String third1 = String.valueOf(third);
char fourth = take_word.charAt(3);
String fourth1 = String.valueOf(fourth);
char fifth = take_word.charAt(4);
String fifth1 = String.valueOf(fifth);
//random word from file
char first2 = check_word.charAt(0);
String first22 = String.valueOf(first2);
char second2 = check_word.charAt(1);
String second22 = String.valueOf(second2);
char third2 = check_word.charAt(2);
String third22 = String.valueOf(third2);
char fourth2 = check_word.charAt(3);
String fourth22 = String.valueOf(fourth2);
char fifth2 = check_word.charAt(4);
String fifth22 = String.valueOf(fifth2);
if (take_word.length()<7) {
if (first1.trim().equals(first22)) {
word_1.setText(first1);
word_1.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 1 doesnt match");
continue;
}
if (second1.trim().equals(second22)) {
word_2.setText(second1);
word_2.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 2 doesnt match");
continue;
}
if (third1.trim().equals(third22)) {
word_3.setText(third1);
word_3.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 3 doesnt match");
continue;
}
if (fourth1.trim().equals(fourth22)) {
word_4.setText(fourth1);
word_4.setForeground(Color.red);
} else {
count++;
JOptionPane.showMessageDialog(this, "Char 4 doesnt match");
continue;
}
if (fifth1.trim().equals(fifth22)) {
word_5.setText(fifth1);
word_5.setForeground(Color.red);
//displaying charchter on GUI
break;
} else {
JOptionPane.showMessageDialog(this, "doesnt 5 match");
count++;
continue;
}
} else {
JOptionPane.showMessageDialog(this, "only 5 char word are allowed Number");
}
if (check_word.trim().equals(take_word)) {
JOptionPane.showMessageDialog(this, "YOU guessed the number in first attempt");
break;
} else {
count++;
}
}
System.out.println(count);
} catch (Exception e) {
System.out.println(e);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论