I start programming about one month ago, and i have some difficulties with the operators ++a, a++ in java. Can someone explain me line by line this program?
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);
}
}
int division1 = (number++) % 10;
Is equivalent to :
int division1 = number % 10;
number += 1;
While
int division1 = (++number) % 10;
Would be equivalent to :
number += 1;
int division1 = number % 10;
Basically, your code is equivalent to :
int number = new java.util.Scanner(System.in).nextInt();
System.out.println( ((number + 1) / 10) % 10 + number % 10 );
++a
is a pre-incrementation. Which means that a
is incremented before returning the value of a
.
a++
is a post-incrementation. Which means that a
is incremented after returning the value of a
.
In other words, a++
gives the current value of a
and then increment it. While ++a
directly increment a
. If a=42
then System.out.println(a++)
gives 42
while System.out.println(++a)
gives 43
and in both cases, a=43
now.
OP also asked for a line by line explanation of that code:
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);
}
}
I guess, only the code inside the main
function need some explanations :
// 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);
The line int division1 = (number++) % 10;
might not be very clear. It would be simpler to read like that:
int division1 = number % 10;
number += 1;
Now, the explanation of what the function does:
If number = 142
, we put 2 into variable division1
, then number is incremented and divided by 10. So number gets the value 14 ((142+1) / 10). And now we print number % 10 + division1 which is 4 + 2 = 6.
Here some examples of results (I've compiled the code myself):
3 => 3
9 => 10
10 => 1
248 => 12
Literally line-by-line explanation:
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.
}
}
Some cases:
f(1) = 1 f(124) = 2+4 = 6 f(39) = 4+9 = 13 f(5248) = 4+8 = 12
++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.
I think, the interesting part for you are the following lines:
int number = keyboard.nextInt();
Here, you get an input from the user. Say, he enters 12
int division1 = (number++) % 10;
division1
will become 2, because 12 mod 10 equals 2. After this, number
will be incremented by 1, so it is now 13
Let's now assume there would be a ++number
instead of number++
. In this case, we would increment number
before the modulo operation. So, first, number
becomes 13, then we assign 3 to division1
.
And that's the basic thing about a++
and ++a
- the time, when the value is incremented. a++
means, all operations use a
as it is and as a last step, a
is incremented. ++a
works in the other order.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.