[英]Java clarification on += assignment operator
I'm a bit confused about how += assignment operator works. 我对+ =赋值运算符的工作方式有些困惑。 I know that x += 1 is x = x+1.
我知道x + = 1是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.
我的困惑是变量“输出”有5个不同的输出,但我看不到它的存储位置。 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);
}
}
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 Badshah给出的答案对于您的程序是很有意义的,如果您想进一步了解操作员的可用性,请首先查看我遇到的这个问题
+ operator for String in Java + Java中String的运算符
The answers posted have very good reasoning of the operator 发布的答案对操作员具有很好的推理能力
Its Add AND assignment operator . 其Add AND赋值运算符 。
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.
` `
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. 在Java中,字符串是不可变的。 So
output += somethingNew
makes something like this: 所以
output += somethingNew
会产生这样的结果:
String temp = output;
output = temp + somethingNew;
At the end, it becomes something like concat/merge 最后,它变得像concat / merge
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.