2011-06-01 28 views
-1

我正在嘗試創建一個使用文件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()); 
} 
+1

它看起來像你沒有CATCH的TRY。這是真的,還是出於空間原因離開? – 2011-06-01 01:51:23

+0

,但即使與捕捉它不會工作 – owo 2011-06-01 02:08:02

+0

你有沒有嘗試通過與調試程序的代碼呢?它在什麼時候失敗? – Alex 2011-06-01 02:11:45

回答

3

退一步,看看你的代碼從高位:

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 ... 

可以肯定的感覺,就像你坐了下來,編碼53奇沒有測試的行任何東西,複製粘貼的代碼不知道爲什麼你在第一個地方,並不明白你在杉木瞄準什麼st地方。(對不起,這是鈍的,但你沒有徵求建議,我不擅長糖衣。)

我建議你先用在一張紙上先用鉛筆寫完。你會希望它看起來更像是這樣的:

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 

一旦你完成了所有的情況下,所有的成敗得失,等想,然後開始編碼。 從小開始。即使只是通常的Java噪聲,也可以立即編譯並運行。添加幾行,重新運行,重新測試。切勿一次添加超過四條或五條線。並且不要猶豫,要添加大量的System.out.println(...)調用來顯示你的程序內部狀態。

隨着您的經驗越來越豐富,您可以更好地識別錯誤信息,並且可能有足夠的信心一次添加十到二十行。不要急於到達那裏,但這需要時間。

0

這將作爲一個數組來檢查程序的用戶輸入的字母?用於保密字 //設置整數值的長度爲猜測的用戶的數量的字母數

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"; 

//設置整數值取得。從零開始。 int numberofletters,numberofguesses; numberofguesses = 0;

// guessletter指示用戶猜測的字母 // guessword指示用戶在輸入「!」後猜測的單詞。 //新屏幕表示對屏幕所做的更改 //屏幕是包含所有「_」的遊戲屏幕 字符串猜字母,guessword,newscreen; String screen =「」; numberofletters = SECRET_WORD.length();

/* prompt user for a word */ 
c.print("Enter a letter: "); 
guessletter = c.readLine(); 
相關問題