1

I'm a bit confused about how += assignment operator works. I know that x += 1 is x = x+1. However, in this code there is a string variable called 'String output' and initialized with an empty string. My confusion is that that there are 5 different outputs for the variable 'output' but I don't see where it's being stored. Help clarify my misunderstanding. I can't seem to figure it out.

import java.util.Scanner;

public class SubtractionQuiz {
public static void main(String[] args) {
    final int NUMBER_OF_QUESTIONS = 5; //number of questions
    int correctCount = 0; // Count the number of correct answer
    int count = 0; // Count the number of questions
    long startTime = System.currentTimeMillis();
    String output = " "; // Output string is initially empty
    Scanner input = new Scanner(System.in);

    while (count < NUMBER_OF_QUESTIONS) {
        // 1. Generate two random single-digit integers
        int number1 = (int)(Math.random() * 10);
        int number2 = (int)(Math.random() * 10);

        // 2. if number1 < number2, swap number1 with number2
        if (number1 < number2) {
            int temp = number1;
            number1 = number2;
            number2 = temp;
        }

        // 3. Prompt the student to answer "What is number1 - number2?"
        System.out.print(
          "What is " + number1 + " - " + number2 + "? ");
        int answer = input.nextInt();

        // 4. Grade the answer and display the result
        if (number1 - number2 == answer) {
            System.out.println("You are correct!");
            correctCount++; // Increase the correct answer count
        }
        else
            System.out.println("Your answer is wrong.\n" + number1
                + " - " + number2 + " should be " + (number1 - number2));


        // Increase the question count
        count++;

        output +=  "\n" + number1 + "-" + number2 + "=" + answer +
                ((number1 - number2 == answer) ? " correct" : "        
                                    wrong");

    }

    long endTime = System.currentTimeMillis();
    long testTime = endTime = startTime;

    System.out.println("Correct count is " + correctCount +
      "\nTest time is " + testTime / 1000 + " seconds\n" + output);

    }


 }
3
  • string1 + string2 concatenates strings. So output += someString appends the content of someString to output. Commented Jun 18, 2013 at 19:10
  • The "5 different outputs" is actually one output string separated into 5 lines using the newline character \n Commented Jun 18, 2013 at 19:18
  • @damo thanks for the response. I just figured that out, took a while. Commented Jun 18, 2013 at 20:19

3 Answers 3

1

Answer given by Badshah is appreciable for your program and if you want to know more about operator' usability, jst check out this question i came across

+ operator for String in Java

The answers posted have very good reasoning of the operator

Sign up to request clarification or add additional context in comments.

Comments

0

Its Add AND assignment operator.

It adds right operand to the left operand and assign the result to left operand.

In your case

output += someString // output becomes output content +somestring content.

`

1 Comment

Thanks, Baadshah, took me a while to see that.
0

Maybe the proper answer was written but if I understand your question correctly, you want some clarification instead of meaning of +=

Change the code;

    // Increase the question count
    count++;

    output +=  "\n" + number1 + "-" + number2 + "=" + answer +
            ((number1 - number2 == answer) ? " correct" : "wrong");

as this:

    output +=  "\nCount: " + count + " and the others: " + 
            number1 + "-" + number2 + "=" + answer +
            ((number1 - number2 == answer) ? " correct" : "wrong");
    // Increase the question count
    count++;

So you can see the line and the count together. Then increase as your wish.

In Java, Strings are immutable. So output += somethingNew makes something like this:

String temp = output;
output = temp + somethingNew;

At the end, it becomes something like concat/merge

Comments

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.