1

sorry if this is a repeat question or sounds pretty stupid, but I'm really new to c# and looked throughout the forum and couldn't find anything that I could actually understand.

So I'm trying to write a simple program where the user tries to guess a number between 1 and 25. Everything works except that each run of the loop instead of updating the score from the last run of the loop, like 0+1=1, 1+1=2, 2+1=3, each time it adds 1 to 0. Here is my code. How do I fix this? Thank you!

int score = 0;
int add = 1;

while (add == 1)
{
    Console.WriteLine("Guess A Number Between 1 and 25");
    string input = Console.ReadLine();

    if (input == "18")
    {
        Console.WriteLine("You Did It!");
        Console.WriteLine("Not Bad! Your Score was " + score + add);
        break;
    }
    else
    {
        Console.WriteLine("Try Again. Score: " + score + add);
    }
}

1 Answer 1

5

You need to actually add add to score. Try something like this:

int score = 0;
int add = 1;

while (add == 1)
{
    Console.WriteLine("Guess A Number Between 1 and 25");
    string input = Console.ReadLine();

    score += add; // add `add` to `score`. This is the same as `score = score + add;`

    if (input == "18")
    {
        Console.WriteLine("You Did It!");
        Console.WriteLine("Not Bad! Your Score was " + score);
        break;
    }
    else
    {
        Console.WriteLine("Try Again. Score: " + score);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

source += add is the same thing as source = source + add, if you are new to C# that might be confusing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.