ObiJay
ObiJay

Reputation: 3

Java- Saving score in a game

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.

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.

When I only run my if else statement once, it works correctly. Once I loop it more than once it starts to act up.

I have to use an if else statement and a for or while loop. Thanks!

Edit- Turns out I have to use a for loop not a while loop, I'm completely lost now.

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.

    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();

}

}

Upvotes: 0

Views: 2090

Answers (3)

Ian McLaird
Ian McLaird

Reputation: 5585

OK, first, let's just go over for loops, since that's what your question was asking about. From the code you provided, it seems that you already understand while loops, and that's good, because in Java, for loops are (usually) just while loops in disguise. In general, if you have this while loop,

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

You can always rewrite it like this:

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

By convention (and this convention is useful when you study arrays and Collection types) you'll want to index your loops from 0 rather than 1. Since you're just learning, take my word for it for now. Loop from 0 to n-1, not from 1 to n.

With that out of the way, let's tackle why you're getting the wrong answer (which, incidentally, has nothing at all to do with loops). Rewritten as a for loop, the ask-and-score part of your program looks like this.

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);
}

You will note that nowhere in this section do you update the value of numberInFile. That means that every run of this loop is still looking at whatever value that variable had at the beginning of the loop. That value came from this line:

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

That line is executed exactly once, before the loop runs. If you want to load the next number every time the user guesses a number, you'll need to move it inside the loop. I'll leave that as an exercise to the reader.

Upvotes: 1

Chris
Chris

Reputation: 478

You are not actually capturing the number the user is entering. Try this:

int userNumber = Integer.parseInt(KeyIn.readLine());

Upvotes: -1

uma
uma

Reputation: 1575

if (userNumber==numberInFile){
    score = score-10;
    }

I don't understand what you going to mean. but I can guess this. your above code not show any error. normally , you check your , above part of code. you take variable 'numberInFile'. sometime , your file reader take this with 'whitespace or String or e.t.c' . first you check this out put .you put manual data to this variable and check out put. if it work fine , you correct that function.

Upvotes: 1

Related Questions