/*
*
* Hangman.java
* The program asks the user to choose a file that is provided.
* The program will read one line from the file and the player will guess the word.
* and then outputs the line, the word will appear using "_".
* The player will guess letters within the word or guess entire word,
* if the player guesses correctly the "_" will replaced with the letter guessed.
* But, if the player guesses incorrectly the a part of the stickman's body will be added,
* then the user will be asked to guess again. The user can also enter "!" to guess the entire word,
* if the guess correctly they win, but if they guess incorrectly they will be asked to guess again.
* Once it has finished reading the file, the program outputs the number of guesses.
*/
import java.awt.*;
import hsa.Console;
//class name
public class Hangman
{
static Console c;
public static void main (String [] args)
{
c = new Console ();
c.println("Hello, and welcome to Hangman!");
PrintWriter output;
String fileName;
//ask user to choose file; file contains words for user to guess
c.println ("The categories are: cartoons.txt, animals.txt, and food.txt. Which category would you like to choose?");
fileName = c.readLine ();
// R:\\HNMRY\\ICS3U10\\Assignments\\Sumbit your work for marks here\\Frank, Tracy\\+fileName
try {
/* Sets up a file reader to read the file passed on the command
line one character at a time */
FileReader input = new FileReader(args[0]);
/* Filter FileReader through a Buffered read to read a line at a
time */
BufferedReader bufRead = new BufferedReader(input);
String line; // String that holds current file line
int count = 0; // Line number of count
// Read first line
line = bufRead.readLine();
count++;
{
lineCount++;
output.println (lineCount + " " + line);
line = input.readLine ();
}
// Read through file one line at time. Print line # and line
while (line != null){
c.println(count+": "+line);
line = bufRead.readLine ();
count++;
}
bufRead.close();
}
catch (FileNotFoundException e)
{
c.println("File does not exist or could not be found.");
c.println("FileNotFoundException: " + e.getMessage());
}
catch (IOException e)
{
c.println("Problem reading file.");
c.println("IOException: " + e.getMessage());
}
final String GUESS_FULL_WORD = "!";
// set integer value for number of letters for the length of the secret word
// set integer value for the number of guesses the user have made. starting at zero.
int numberofletters, numberofguesses;
numberofguesses = 0;
// guessletter indicates the letter that the user is guessing
// guessword indicates the word that the user is guessing after typing "!"
// new screen indicates the change made to the screen
// screen is the game screen that contains all the "_"'s
String guessletter, guessword, newscreen;
String screen = "";
numberofletters = SECRET_WORD.length ();
// for every letter there is in the secretword, add a "_" to screen.
for (int numdashes = 1; numdashes <= numberofletters; numdashes ++)
{
screen += "_";
}
//Array to check letters entered by user
final int LOW = 'A'; //smallest possible value
final int HIGH = 'Z'; //highest possible value
int[] letterCounts = new int[HIGH - LOW + 1];
String SECRET_WORD;
char[] guessletter;
int offset; //array index
// set constants for the secret word and also "!" to guess the full word
final String GUESS_FULL_WORD = "!";
//设置密码长度的字母数的整数值 //设置用户所做猜测次数的整数值。从零开始。 int numberofletters,numberofguesses; numberofguesses = 0;
// guessletter表示用户猜的字母 // guessword表示用户在输入“!”后猜测的单词 //新屏幕表示对屏幕所做的更改 //屏幕是包含所有“_”的游戏屏幕 String guessletter,guessword,newscreen; String screen =“”; numberofletters = SECRET_WORD.length();
/* prompt user for a word */
c.print("Enter a letter: ");
guessletter = c.readLine();
// start guessing!
// keep the user guessing (do while loop) until user types in "!" or the game screen is all filled with letters
// add one to numberofguesses to as a counter for the guesses the user makes
// A string lu(Letters Used) is created to hol the letters the user has already guessed.
String lu;
Boolean canPlay = true;
do
{
c.println ("(TYPE (!) TO GUESS THE WHOLE MYSTERY WORD!)");
c.println (screen);
c.println ("Please guess a letter: ");
guessletter = c.readLine ();
numberofguesses = numberofguesses + 1;
// change user's guessed letter to upper case.
guessletter = guessletter.toUpperCase ();
// make an index for which space the guessed letter is in the secret word.
int indexof = SECRET_WORD.indexOf (guessletter);
// if the guessed letter DOES appear in the secret word
// then replace the guessed letter on the corresponding dash on the newscreen
// the dashes that is in front of the corresponding dash and behind it is not changed to the current newscreen.
// the replace the information of the old screen with the new screen.
if (indexof > -1)
{
newscreen = (screen.substring (0,indexof) + guessletter + screen.substring (indexof+1));
screen = newscreen;
}
}while (GUESS_FULL_WORD.compareTo (guessletter)!= 0 && SECRET_WORD.compareTo (screen) !=0);
// if the screen equals the secret word (the dashes are all replaced with letters)
// then indicate that the user have won.
// also include the secret word and the number of guesses the used have made.
if (screen.equals (SECRET_WORD))
{
c.println ("WOW!! YOU WON!! (:");
c.println ("The scecret word was..." +SECRET_WORD);
c.println ("You've guessed the total of " +numberofguesses+ " times");
}
// if the user enters "!" then
if (guessletter.equals (GUESS_FULL_WORD))
{
c.println (screen);
// allow the user to guess the whole word.
c.println ("What is the secret word? ");
guessword = c.readLine ();
// change the user's guess into upper case
guessword = guessword.toUpperCase ();
if (numberofguesses==6)
{
canPlay = false;
Lose();
}
else
/*
* Put man here
--
o |
/|\|
/ \|
_____
*/
String man[] = new String[7]
man[0] = " --\n |\n |\n |\n_____\n";
man[1] = " --\n o |\n |\n |\n_____\n";
man[2] = " --\n o |\n/ |\n |\n_____\n";
man[3] = " --\n o |\n/| |\n |\n_____\n";
man[4] = " --\n o |\n/|\\|\n |\n_____\n";
man[5] = " --\n o |\n/|\\|\n/ |\n_____\n";
man[6] = " --\n o |\n/|\\|\n/ \\|\n_____\n";
char g1[] = guess.toCharArray();
char w2[] = word.toCharArray();
c.println(man[0]);
for(int x=0;x<g1.length;x++)
{
c.print(g1[x]);
}
c.println();
// inside the if-statement...
// if the user's guess (full word) match with the secret word then
// indicate that the user have won.
// also include the secret word and also the number of guesses the user have made.
if (guessword.compareTo (SECRET_WORD) ==0)
{
c.println ("WOW!! YOU'VE GUESSED IT!! (:");
c.println ("The scecret word was..." +SECRET_WORD);
c.println ("You've guessed the total of " +numberofguesses+ " times");
}
// if the user's guess (full word) does not match with the secret word then
// indicate that the user have lost.
// also include the secret word and also the number of guesses the user have made
else
{
c.println ("OOPS...! YOU LOSE!! ):");
c.println ("The scecret word was..." +SECRET_WORD);
c.println ("You've guessed the total of " +numberofguesses+ " times");
}
}
**7 errors found:**
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: not a statement
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: ';' expected
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: not a statement
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: ';' expected
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: not a statement
File: H:\My Documents\Dr. Java\Hangman.java [line: 166]
Error: H:\My Documents\Dr. Java\Hangman.java:166: ';' expected
File: H:\My Documents\Dr. Java\Hangman.java [line: 203]
Error: H:\My Documents\Dr. Java\Hangman.java:203: reached end of file while parsing
答案 0 :(得分:1)
至少,你需要一个分号:
String man[] = new String[7]
将其更改为:
String man[] = new String[7];
这是第166行的问题。第203行上的问题可能由此修复,或者它可能表示没有右括号}
(或仅仅是不匹配的括号)代码中的某个地方。
答案 1 :(得分:0)
该行
String man[] = new String[7]
最后需要;
。
此前else
语句之前的几行没有大括号。
什么是Lose();
?看起来像是函数调用给我,但是你没有相应的函数。
答案 2 :(得分:0)
每次更改代码时都要编译(并修复这些错误),这样在开始调试之前不必处理十几个语法错误
String man[] = new String[7]
缺少;
在错过{
之前的其他内容,并且文件末尾缺少多个}
btw正确地缩进你的代码这对于找到不匹配的大括号是一个巨大的帮助