Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java对+;=赋值运算符_Java_Variable Assignment_Operator Keyword - Fatal编程技术网

Java对+;=赋值运算符

Java对+;=赋值运算符,java,variable-assignment,operator-keyword,Java,Variable Assignment,Operator Keyword,我对+=赋值运算符的工作原理有点困惑。我知道x+=1就是x=x+1。但是,在这段代码中有一个名为“string output”的字符串变量,并用空字符串初始化。我的困惑是变量“output”有5种不同的输出,但我不知道它存储在哪里。帮助澄清我的误解。我似乎不明白 import java.util.Scanner; public class SubtractionQuiz { public static void main(String[] args) { final int NUMBE

我对+=赋值运算符的工作原理有点困惑。我知道x+=1就是x=x+1。但是,在这段代码中有一个名为“string output”的字符串变量,并用空字符串初始化。我的困惑是变量“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);

    }


 }
import java.util.Scanner;
公开课减法测验{
公共静态void main(字符串[]args){
最终整数问题数=5;//问题数
int correctCount=0;//计算正确答案的数目
int count=0;//计算问题的数量
long startTime=System.currentTimeMillis();
String output=”“;//输出字符串最初为空
扫描仪输入=新扫描仪(System.in);
while(计数<问题数量){
//1.生成两个随机的单位数整数
int number1=(int)(Math.random()*10);
int number2=(int)(Math.random()*10);
//2.如果number1
Its添加和赋值运算符

它将右操作数与左操作数相加,并将结果赋给左操作数

就你而言

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

`

Its添加和赋值运算符

它将右操作数与左操作数相加,并将结果赋给左操作数

就你而言

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

也许正确的答案已经写好了,但如果我正确理解了你的问题,你希望得到一些澄清,而不是+=

更改代码

    // 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的东西,Badsah给出的答案对您的程序来说是值得赞赏的,如果您想了解更多关于操作员可用性的信息,请查看我遇到的这个问题


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

Badsah给出的答案对您的程序很有用,如果您想更多地了解操作员的可用性,请查看我遇到的这个问题


发布的答案对运算符

string1+string2
连接字符串有很好的推理。因此,
output+=someString
someString
的内容附加到
output
中。“5个不同的输出”实际上是一个使用换行符
\n
@damo分隔成5行的输出字符串,感谢您的响应。我刚想出来,花了一段时间。
string1+string2
连接字符串。因此,
output+=someString
someString
的内容附加到
output
中。“5个不同的输出”实际上是一个使用换行符
\n
@damo分隔成5行的输出字符串,感谢您的响应。我刚想出来,花了一段时间。谢谢,巴德萨,我花了一段时间才看到。谢谢,巴德萨,我花了一段时间才看到。