Итак, у меня эта программа несколько функциональна до сих пор. Я использовал инструкции switch. Я не уверен, что это правильная логика для этого, но у меня есть работа. Когда я ввожу слово, которое я хочу, он работает и начинает рисовать палача после первого ввода, но затем запускает ошибку.Программирование Hangman в Java, исключение в ошибке основного потока (наряду с другими)
public class Hangman {
public static void main (String[] args){
JFrame frame = new JFrame ("Hangman");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HangmanPanel Panel = new HangmanPanel();
frame.getContentPane().add(Panel);
frame.pack();
frame.setVisible(true);
}
}
вот мой другой класс
import javax.swing.JPanel;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
import java.util.Scanner;
import java.io.*;
import java.util.Scanner;
public class HangmanPanel extends JPanel {
int lives;
String guessletter;
String inputw;
char[] hiddenWord;
char[] aOfWord;
String[] words = { "ADA", "COBOL", "LOGO", "BASIC", "PROLOG", "UBUNTU",
"UHURU" };
Scanner scan = new Scanner(System.in);
boolean isFound;
int a;
public HangmanPanel() {
System.out.println("Enter the word to be searched: ");
inputw = scan.nextLine();
aOfWord = inputw.toCharArray();
this.setLives(inputw.length());
// this.output();
hiddenWord = new char[aOfWord.length];
for (int j = 0; j < hiddenWord.length; j++)
hiddenWord[j] = '*';
this.output();
while (lives > 0)
System.out.println("Please choose a letter: ");
guessletter = scan.nextLine();
this.checkForMatch(guessletter);
if (isFound == true) {
hiddenWord[a] = guessletter.charAt(0);
} else {
System.out.println("_____");
System.out.println("| |");
System.out.println("| ");
System.out.println("| ");
System.out.println("| ");
this.reduceLives();
while (lives > 0)
System.out.println("Please choose a letter: ");
guessletter = scan.nextLine();
this.checkForMatch(guessletter);
if (isFound == true) {
hiddenWord[a] = guessletter.charAt(1);
} else {
System.out.println("_____");
System.out.println("| |");
System.out.println("| o");
System.out.println("| ");
System.out.println("| ");
this.reduceLives();
}
while (lives > 0)
System.out.println("Please choose a letter: ");
guessletter = scan.nextLine();
this.checkForMatch(guessletter);
if (isFound == true) {
hiddenWord[a] = guessletter.charAt(2);
} else {
System.out.println("_____");
System.out.println("| |");
System.out.println("| o");
System.out.println("| | ");
System.out.println("| ");
this.reduceLives();
}
while (lives > 0)
System.out.println("Please choose a letter: ");
guessletter = scan.nextLine();
this.checkForMatch(guessletter);
if (isFound == true) {
hiddenWord[a] = guessletter.charAt(3);
} else {
System.out.println("_____");
System.out.println("| |");
System.out.println("| o");
System.out.println("| /| ");
System.out.println("| ");
this.reduceLives();
}
while (lives > 0)
System.out.println("Please choose a letter: ");
guessletter = scan.nextLine();
this.checkForMatch(guessletter);
if (isFound == true) {
hiddenWord[a] = guessletter.charAt(4);
} else {
System.out.println("_____");
System.out.println("| |");
System.out.println("| o");
System.out.println("| /|\\ ");
System.out.println("| ");
this.reduceLives();
}
while (lives > 0)
System.out.println("Please choose a letter: ");
guessletter = scan.nextLine();
this.checkForMatch(guessletter);
if (isFound == true) {
hiddenWord[a] = guessletter.charAt(5);
} else {
System.out.println("_____");
System.out.println("| |");
System.out.println("| o");
System.out.println("| /|\\ ");
System.out.println("| \\");
this.reduceLives();
}
while (lives > 0)
System.out.println("Please choose a letter: ");
guessletter = scan.nextLine();
this.checkForMatch(guessletter);
if (isFound == true) {
hiddenWord[a] = guessletter.charAt(6);
} else {
System.out.println("_____");
System.out.println("| |");
System.out.println("| o");
System.out.println("| /|\\");
System.out.println("|/\\");
this.reduceLives();
}
}
this.output();
}
public void setLives(int a) {
this.lives = a;
}
public void reduceLives() {
lives = lives - 1;
System.out.println("Lives remaining: " + this.getLives());
}
public int getLives() {
return lives;
}
public void output() {
System.out.println("Lives remaining: " + this.getLives());
System.out.println("Word found so far ");
for (int i = 0; i < hiddenWord.length; i++) {
System.out.print(hiddenWord[i] + "\n");
}
}
public void checkForMatch(String l) {
for (int i = 0; i < aOfWord.length; i++) {
if (l.charAt(0) == aOfWord[i]) {
isFound = true;
a = i;
break;
} else {
isFound = false;
}
}
}
}
сообщение Ошибка здесь, пожалуйста? – iWumbo
Вы не «ломаете» или «возвращаете» после каждого «случая», поэтому, возможно, ваш код работает процедурно и дает вам неожиданные ошибки. – SnakeDoc
Я не думаю, что перерывы в этом случае будут иметь большое значение, поскольку все случаи являются взаимоисключающими. – MxyL