title: Java- Saving score in a game
body: <p>I'm having problems tyring to keep score in my "guessing" game. I have to use a for loop or while loop. I have it so 10 random numbers are created in a text file called mystery.txt and a file reader reads these numbers from the text file.</p>
  
  <p>Your score starts at 0. If the user guesses the correct number from the text file they get -10 points. If they get the number wrong they add the the absolute value difference of the number they guessed from a number in the file. The lower the score in the end the better.</p>
  
  <p>When I only run my if else statement once, it works correctly. Once I loop it more than once it starts to act up. </p>
  
  <p>I have to use an if else statement and a for or while loop. Thanks!</p>
  
  <p>Edit- Turns out I have to use a for loop not a while loop, I'm completely lost now.</p>
  
  <p>How it should work:
  When you run the program a text file gets generated with 10 different numbers (I already have the code for that ready) The user gets asked to enter a number, the number the user enters gets compared to the first file on the text file. If it is the same never they get -10 points to their score. If they get it wrong they get the difference of the number the guessed and the number in the text file added to the score. This is suppose to repeat ten times.</p>
  
  <pre><code>    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&lt;=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&lt;=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();
  
  }
  </code></pre>
  
  <p>}</p>