Int++ 运算符第一次运行时不会增加整数。

huangapple go评论67阅读模式
英文:

Int++ operator isn't increasing the int the first time it's ran

问题

import java.util.Scanner;

class U1_L4_Activity_One{
  public static void main(String[] args){
      
      int num;
      Scanner startNum = new Scanner(System.in);
      
      //Enter an int (num)
      System.out.println("请输入起始数字(必须为整数)");
      num = startNum.nextInt();
      
      //Increases num 3 times
      System.out.println("数字现在是:" + num++);
      System.out.println("数字现在是:" + num++);
      System.out.println("数字现在是:" + num++);
      //Decreases num 3 times, back to original number
      System.out.println("数字现在是:" + num--);
      System.out.println("数字现在是:" + num--);
      System.out.println("数字现在是:" + num--);
  }
}
英文:

Basically the goal of the program is to have the user input a number, increase is 3 times, then decrease it 3 times using unary operators. The issue is that when it's ran, the first "number is now ___" line ends up just showing the same number the user inputed rather than increasing it by one. New to Java, Don't really know why

import java.util.Scanner;

class U1_L4_Activity_One{
  public static void main(String[] args){
      
      int num;
      Scanner startNum = new Scanner(System.in);
      
      //Enter an int (num)
      System.out.println("Enter starting number(must be an integer)");
      num = startNum.nextInt();
      
      //Increases num 3 times
      System.out.println("number is now " + num++);
      System.out.println("number is now " + num++);
      System.out.println("number is now " + num++);
      //Decreases num 3 times, back to original number
      System.out.println("number is now " + num--);
      System.out.println("number is now " + num--);
      System.out.println("number is now " + num--);
  }
}

答案1

得分: 1

++一元运算符可用作后增量或前增量运算符。
在这种情况下,由于您在变量num之后添加了++,它执行的是后增量操作。
这意味着首先显示num变量的值,然后将其增加1。
这就是数字先被打印出来,然后增加,导致再次显示相同的数字。

要修复这个问题,您可以使用前增量操作。

  System.out.println("number is now " + ++num); // <-- 首先增加num,然后显示。
  System.out.println("number is now " + ++num);
  System.out.println("number is now " + ++num);
  // 将num减少3次,恢复到原始数字
  System.out.println("number is now " + --num); // <-- 前减量操作
  System.out.println("number is now " + --num);
  System.out.println("number is now " + --num);
英文:

The ++ unary operator can be used as a post-increment or pre-increment operator.
In this case, since you have added the ++ after the num variable, it is performing a post-increment operation.
This means that the num variable value is displayed first and then will be incremented by 1.
This is the reason the number is printed first and then incremented causing the same number to display again.

To fix this you can make use of the pre-increment operation.

  System.out.println(&quot;number is now &quot; + ++num); // &lt;-- the num is incremented first and then displayed.
  System.out.println(&quot;number is now &quot; + ++num);
  System.out.println(&quot;number is now &quot; + ++num);
  //Decreases num 3 times, back to original number
  System.out.println(&quot;number is now &quot; + --num); // &lt;-- pre decrement operation
  System.out.println(&quot;number is now &quot; + --num);
  System.out.println(&quot;number is now &quot; + --num);

答案2

得分: 0

因为 num++ 在“后台”增加其值然后返回它。如果你想增加 num 并立即返回值,你应该使用 ++num

英文:

It's because num++ increments its value in the "background" and then returns it. If you want to increment num and immediatly return the value, you should use ++num.

huangapple
  • 本文由 发表于 2020年8月25日 13:17:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63572466.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定