【问题标题】:Java clarification on += assignment operator关于 += 赋值运算符的 Java 说明
【发布时间】:2013-06-15 03:23:48
【问题描述】:

我对 += 赋值运算符的工作方式有点困惑。我知道 x += 1 是 x = x+1。但是,在此代码中有一个名为“String output”的字符串变量,并使用空字符串进行了初始化。我的困惑是变量“输出”有 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);

    }


 }

【问题讨论】:

  • string1 + string2 连接字符串。所以output += someStringsomeString的内容追加到output
  • “5 个不同的输出”实际上是一个输出字符串,使用换行符\n 分隔成 5 行@
  • @damo 感谢您的回复。我只是想通了,花了一段时间。

标签: java variable-assignment operator-keyword


【解决方案1】:

Badshah 给出的答案对您的程序来说是可观的,如果您想了解更多关于操作员的可用性,请查看我遇到的这个问题

+ operator for String in Java

发布的答案对运营商有很好的推理

【讨论】:

    【解决方案2】:

    它的加和赋值运算符

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

    你的情况

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

    `

    【讨论】:

    • 谢谢,Baadshah,我花了一段时间才看到。
    【解决方案3】:

    也许写了正确的答案,但如果我正确理解你的问题,你需要一些澄清而不是 += 的含义

    修改代码;

        // 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 的东西

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-09
      • 2012-04-01
      • 1970-01-01
      • 2019-05-14
      • 2011-11-16
      • 2015-05-02
      • 1970-01-01
      • 2015-03-19
      相关资源
      最近更新 更多