I'm trying to create a hangman program that uses file io/ file input. I want the user to choose a category (file) which contains 4 lines; each has one word. The program will then read the line and convert it into _
s , this is what the user will see.
Where would I insert this -->
{
lineCount++;
output.println (lineCount + " " + line);
line = input.readLine ();
}
/*
- 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 ();
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 ();
// E:\\ICS\\ICS 3U1\\Assignments\\JavaFiles\\+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++;
// Read through file one line at time. Print line # and line开发者_JAVA技巧
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());
}
Take a step back and look at your code from a high level:
print "which file?"
filename = readline()
open(filename)
try {
print "which file?"
filename = readline()
open(filename)
create reader (not using the File object?)
create writer (not using the File object, but why a writer??)
while ...
It sure feels like you've sat down, coded 53 odd lines without testing anything, copy-and-pasted code around without understanding why you had it in the first place, and didn't understand what you were aiming for in the first place. (Sorry to be this blunt, but you did ask for advice and I'm not good at sugar coating.)
I suggest writing your program entirely by hand on a sheet of paper with a pencil first. You'll want it to look more like this:
while user still wants to play
ask for category
open file
read file contents into an array
close file
select array element at random
while user still has guesses left
print a _ for each character
ask user to guess a letter
if letter is in the word
replace the _ with the letter in the output
if the word is complete, success!
else
guesses left --
user ran out of guesses, give condolences
Once you've thought through all the cases, all the wins and losses, and so forth, then start coding. Start small. Get something to compile and run immediate, even if it is just the usual Java noise. Add a few lines, re-run, re-test. Never add more than four or five lines at a time. And don't hesitate to add plenty of System.out.println(...)
calls to show you what your program internal state looks like.
As you get more experienced, you'll get better at recognizing the error messages, and maybe feel confident enough to add ten to twenty lines at a time. Don't rush to get there, though, it takes time.
would this work as an array to check the letters entered by the user of the program?
final int LOW = 'A'; //smallest possible value
final int HIGH = 'Z'; //highest possible value
int[] letterCounts = new int[HIGH - LOW + 1];
String guessletter;
char[] guessletter;
int offset; //array index
// set constants for the secret word and also "!" to guess the full word
final String GUESS_FULL_WORD = "!";
final String SECRET_WORD = "APPLE";
// 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 ();
/* prompt user for a word */
c.print("Enter a letter: ");
guessletter = c.readLine();
精彩评论