【发布时间】:2014-01-02 14:04:09
【问题描述】:
所以到目前为止,我的这个程序有些功能。我一直在使用 switch 语句。我不确定这是否是正确的逻辑,但我有它的工作。当我输入我想要的单词时,它可以工作,并在第一次输入后开始绘制刽子手,但随后出现错误。
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;
}
}
}
}
【问题讨论】:
-
请在这里发布错误?
-
您没有在每个
case之后使用break或return,因此您的代码可能正在按程序运行并给您带来意外错误。 -
我不认为在这种情况下休息会很重要,因为所有情况都是相互排斥的。
-
抱歉,这是错误“线程“主”java.lang.StringIndexOutOfBoundsException 中的异常:字符串索引超出范围:HangmanPanel 的 java.lang.String.charAt(Unknown Source) 处的 4。
(HangmanPanel.java:115) 在 Hangman.main(Hangman.java:12) -
@MxyL OP 应该删除
switch语句,如果是这样的话,因为它不需要。有break的好形式,这样你就可以理解程序流程,特别是因为他有一个loop迭代案例直到lives