2015-09-28 33 views
0

我有問題想要在我的「猜測」遊戲中保持分數。我必須使用for循環或while循環。我擁有它,因此在一個名爲mystery.txt的文本文件中創建了10個隨機數字,文件讀取器從文本文件中讀取這些數字。Java-在遊戲中節省分數

您的分數從0開始。如果用戶從文本文件中猜出正確的數字,他們會得到-10分。如果他們得到的數字是錯誤的,他們會添加他們從文件中的數字中猜出的數字的絕對值差異。最後得分越低越好。

當我只運行我的if else語句一次,它的工作正常。一旦我循環它不止一次,它開始行動起來。我不得不使用if else語句和for或while循環。謝謝!

編輯 - 原來我不得不使用for循環而不是while循環,現在我完全失去了。

它應該是如何工作的: 當你運行一個文本文件被10倍不同的數字生成的程序(我已經爲準備代碼)用戶被要求輸入一個號碼,用戶輸入的數量得到較到文本文件中的第一個文件。如果相同,永遠不會得到-10分。如果他們弄錯了,他們會得到被猜測的數字與添加到分數中的文本文件中的數字的差異。這是假設重複十次。

import java.io.BufferedReader; 
    import java.io.FileNotFoundException; 
    import java.io.FileReader; 
    import java.io.IOException; 
    import java.io.PrintWriter; 
    import java.util.Scanner; 
    import java.util.Random; 
    import java.lang.Math; 

    public class lab4Fall15 { 


public static void numberGuessingGame() throws IOException { 


    Scanner myscnr = new Scanner (System.in); 
    PrintWriter mywriter = new PrintWriter("mysteryNumber.txt"); 


    int randomNumber; 


    int i = 0; 
    i=1; 
    while (i<=10) { 
    Random randomGen = new Random(); 
     randomNumber= randomGen.nextInt(11); 
    // then store (write) it in the file 
    mywriter.println(randomNumber); 
     i = i + 1; 
    } 

      //Decided to use a loop to generate the numbers------- 

    mywriter.close();  



    FileReader fr = new FileReader("./mysteryNumber.txt"); 
    BufferedReader textReader = new BufferedReader(fr); 
    int numberInFile; 
    // the number in your file is as follows: 
    numberInFile = Integer.valueOf(textReader.readLine()); 


    int score= 0; 


    int a = 1; 

    while (a<=10) { 
    a=a+1; 
    System.out.print ("Please enter a number between 0 and 10: "); 
    int userNumber= myscnr.nextInt(); 
    if (userNumber==numberInFile){ 
     score = score-10; 
     } 
    else{ 
     score = score + Math.abs(userNumber-numberInFile); 

    } 
    System.out.println ("current score is: "+score); 
    } 




    System.out.println ("your score is "+score);  


    textReader.close(); 
    } 







public static void main(String[] args) throws IOException { 
    // ... 
    numberGuessingGame(); 

} 

}

+0

你是什麼意思「行動起來?」你有某種錯誤(崩潰或錯誤答案)。是*具體*關於你在找什麼。請問 –

+0

,你可以把電流輸出和你預期的舉一個例子嗎? – uma

+0

只需添加它們以及之前的文件讀取器代碼即可。 – ObiJay

回答

-1

您實際上並未捕獲用戶輸入的號碼。試試這個:

int userNumber = Integer.parseInt(KeyIn.readLine()); 
1
if (userNumber==numberInFile){ 
    score = score-10; 
    } 

我不明白你要的意思。但我可以猜到這一點。你的上面的代碼不會顯示任何錯誤。通常情況下,你檢查你的代碼以上部分。你需要變量'numberInFile'。有時候,你的文件閱讀器會用'空白或字符串或e.t.c'。首先你檢查這個輸出。你把手動數據放到這個變量中並檢出。如果它工作正常,你糾正該功能。

1

好的,首先我們來看看for循環,因爲這就是您的問題所在。從您提供的代碼看來,您似乎已經瞭解了while循環,這很好,因爲在Java中,for循環(通常)只是僞裝成while循環。在一般情況下,如果你有這個while循環,

int a = 0; 
while (a < 10) { 
    // do stuff with a 
    a = a + 1; // or ++a or a++ 
} 

你總是可以把它改寫這樣的:

for (int a = 0; a < 10; a = a + 1) { 
    // do stuff with a 
} 

按照慣例(當你學習數組和Collection類型本公約是有用的),你」我想從0而不是1索引你的循環。既然你只是在學習,現在就聽我說。從0循環到n-1,而不是從1n

這樣一來,讓我們來解決爲什麼你得到錯誤的答案(順便說一句,與循環完全沒有關係)。重寫爲for循環,程序的問答部分看起來像這樣。

for (int a = 0; a < 10; ++a) { 
    System.out.print ("Please enter a number between 0 and 10: "); 
    int userNumber = myscnr.nextInt(); 
    if (userNumber == numberInFile){ 
     score = score - 10; 
    } else { 
     score = score + Math.abs(userNumber - numberInFile); 
    } 
    System.out.println ("current score is: "+score); 
} 

你會注意到,無處本節你更新的numberInFile值。這意味着這個循環的每次運行仍然在查看該變量在循環開始時的任何值。該值來自以下行:

// the number in your file is as follows: 
numberInFile = Integer.valueOf(textReader.readLine()); 

該行在循環運行前僅執行一次。如果你想每次用戶猜測一個數字時加載下一個數字,你需要在循環中移動它。我會把這個作爲練習留給讀者。