繁体   English   中英

关于+ =赋值运算符的Java说明

[英]Java clarification on += assignment operator

我对+ =赋值运算符的工作方式有些困惑。 我知道x + = 1是x = x + 1。 但是,此代码中有一个字符串变量,称为“字符串输出”,并使用空字符串初始化。 我的困惑是变量“输出”有5个不同的输出,但我看不到它的存储位置。 帮助澄清我的误会。 我似乎无法弄清楚。

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

    }


 }

Badshah给出的答案对于您的程序是很有意义的,如果您想进一步了解操作员的可用性,请首先查看我遇到的这个问题

+ Java中String的运算符

发布的答案对操作员具有很好的推理能力

Add AND赋值运算符

它将右操作数添加到左操作数,并将结果分配给左操作数。

就你而言

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

`

也许写了正确的答案,但是如果我正确理解了您的问题,那么您需要澄清一下,而不是+ =的意思。

更改代码;

    // Increase the question count
    count++;

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

这样:

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

这样您就可以同时看到行和计数。 然后按照您的意愿增加。

在Java中,字符串是不可变的。 所以output += somethingNew会产生这样的结果:

String temp = output;
output = temp + somethingNew;

最后,它变得像concat / merge

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM