我正在嘗試創建一個使用文件io /文件輸入的hangman程序。我希望用戶選擇一個包含4行的類別(文件);每個都有一個詞。然後該程序將讀取該行並將其轉換爲_
,這是用戶將看到的內容。 我在哪裏可以插入 - > { lineCount ++; output.println(lineCount +「」+ line); line = input.readLine(); }我如何將文件中讀取的行轉換爲「_」?
/*
- Hangman.java
- 程序會要求用戶選擇提供了一個文件。
- 該程序將從文件中讀取一行,玩家將猜測該單詞。
- 然後輸出該行,該單詞將使用「_」出現。
- 玩家猜測字中的字母或猜測整個字,
- 如果玩家正確猜測「_」將替換爲字母猜測。
- 但是,如果玩家猜錯了火柴人身體的一部分將被添加,那麼用戶將被要求再次猜測。用戶也可以輸入「!」猜測整個單詞
- 如果猜測他們正確的贏了,但如果他們猜錯了,他們會被要求再次猜測。
- 讀完文件後,程序輸出猜測次數。
*/
進口java.awt中的*。 import hsa.Console;
//班級名稱 public class Hangman { static Console c;
public static void main(String [] args) { c = new Console();
PrintWriter output;
String fileName;
//要求用戶選擇文件;文件包含供用戶猜測的詞
c.println(「分類是:cartoons.txt,animals.txt和food.txt。您想選擇哪一類?」);
fileName = c.readLine();
// E:\\ICS\\ICS 3U1\\Assignments\\JavaFiles\\+fileName
嘗試{
/* 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
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());
}
它看起來像你沒有CATCH的TRY。這是真的,還是出於空間原因離開? – 2011-06-01 01:51:23
,但即使與捕捉它不會工作 – owo 2011-06-01 02:08:02
你有沒有嘗試通過與調試程序的代碼呢?它在什麼時候失敗? – Alex 2011-06-01 02:11:45