1

我大约一个月前开始编程,我对java中的运算符++a,a++有些困难。有人可以逐行解释这个程序吗?

import java.util.Scanner; 
public class Number { 
 public static void main(String[] args) { 
 Scanner keyboard = new Scanner(System.in); 
 int number = keyboard.nextInt(); 
 int division1 = (number++) % 10; 
 number = number / 10; 
 System.out.println(number % 10+division1); 
 } 
} 
4

5 回答 5

14
int division1 = (number++) % 10;

相当于:

int division1 = number % 10; 
number += 1;

尽管

int division1 = (++number) % 10;

相当于:

number += 1;
int division1 = number % 10; 

基本上,您的代码相当于:

int number = new java.util.Scanner(System.in).nextInt(); 
System.out.println( ((number + 1) / 10) % 10 + number % 10 ); 
于 2013-10-10T09:50:57.710 回答
3

++a是一个预增量。这意味着在返回 的值之前a递增。a

a++是后增量。这意味着返回 的值后a递增。a

换句话说,a++给出 的当前值,a然后将其递增。而++a直接递增a。如果a=42thenSystem.out.println(a++)给出42whileSystem.out.println(++a)给出43and 在这两种情况下,a=43现在。

OP 还要求对该代码进行逐行解释:

import java.util.Scanner;

public class Number { 
 public static void main(String[] args) { 
   Scanner keyboard = new Scanner(System.in); 
   int number = keyboard.nextInt(); 
   int division1 = (number++) % 10; 
   number = number / 10; 
   System.out.println(number % 10+division1); 
 } 
}

我想,只有函数内部的代码main需要一些解释:

   // Create a Scanner object that read from the standard input.
   Scanner keyboard = new Scanner(System.in);

   // Read an integer.
   int number = keyboard.nextInt(); 

   // put (number % 10) into variable division1 and then increment `number`.
   int division1 = (number++) % 10;

   // Divide number by 10.
   number = number / 10; 

   // Print that expression :
   System.out.println(number % 10+division1);

这条线int division1 = (number++) % 10;可能不是很清楚。像这样阅读会更简单:

int division1 = number % 10;
number += 1;

现在,解释函数的作用:

如果number = 142,我们将 2 放入变量division1,则 number 递增并除以 10。因此 number 得到值 14 ((142+1) / 10)。现在我们打印数字 % 10 + 除法 1,即 4 + 2 = 6。

这里有一些结果示例(我自己编译了代码):

3 => 3
9 => 10
10 => 1
248 => 12
于 2013-10-10T09:52:57.637 回答
1

逐行解释:

import java.util.Scanner; // Import Scanner
public class Number {     // Create Java class called Number
    public static void main(String[] args) {  // Define the main function
        Scanner keyboard = new Scanner(System.in); // Initialize the scanner
        int number = keyboard.nextInt();   // Read an integer from standard input
        int division1 = (number++) % 10;   // Take the unit digit of number, then increase number by 1
        number = number / 10; // Divide number by 10
        // Take the tens digit of the original number (or tens digit + 1, if the unit was 9), sum with the unit digit previously stored in division1
        System.out.println(number % 10+division1);
        // The result would sum the last two digit of number if it doesn't end with 9, or sum the last two digit +1 if the unit was 9.
    } 
}

一些案例:

f(1) = 1
f(124) = 2+4 = 6
f(39) = 4+9 = 13
f(5248) = 4+8 = 12
于 2013-10-10T09:55:41.657 回答
0

我认为,对您来说有趣的部分是以下几行:

int number = keyboard.nextInt(); 

在这里,您可以从用户那里获得输入。说,他进入12

int division1 = (number++) % 10; 

division1将变为 2,因为 12 mod 10 等于 2。 此之后,number将增加 1,所以现在是 13

现在让我们假设会有 a++number而不是number++. 在这种情况下,我们将在模运算number 之前递增。因此,首先,number变为 13,然后我们将 3 分配给division1

这就是关于a++和的基本内容++a- 时间,当值增加时。a++意味着,所有操作按原样使用a,作为最后一步,a递增。++a按另一个顺序工作。

于 2013-10-10T10:00:39.050 回答
0

++a is a pre-incrementation. Which means that a is incremented before the expression is evaluated.

a++ is a post-incrementation. Which means that a is incremented after the expression is evaluated.

One thing where beginners might be confused is what is this 'expression' after which the increment takes place It is actually the ; When you use a++, it is after the ";" when the increment actually takes place and when you use ++a; it is just as you inserted a statement before this one saying a=a+1 and then your expression comes.

于 2013-10-10T09:58:34.450 回答