Some Notes:
1- in.nextInt(); reads an integer from the user, blocks until the user enters an integer into the console and presses ENTER. The result integer has to be saved in order to use it later on, and to do so save it into a variable, something like this:
int value = in.nextInt();
In your code, you need to assign the 3 integers that the user enters to the corresponding variables:
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
2- You are implementing the loop very well, but you just need to use the user's inputs rather than using explicit integers:
for(int i = min; i <= max; i += increment)
{
System.out.println(i);
}
Answer from Eng.Fouad on Stack OverflowSome Notes:
1- in.nextInt(); reads an integer from the user, blocks until the user enters an integer into the console and presses ENTER. The result integer has to be saved in order to use it later on, and to do so save it into a variable, something like this:
int value = in.nextInt();
In your code, you need to assign the 3 integers that the user enters to the corresponding variables:
System.out.println("Enter min value: ");
min = in.nextInt();
System.out.println("Enter max value: ");
max = in.nextInt();
System.out.println("Enter increment value: ");
increment = in.nextInt();
2- You are implementing the loop very well, but you just need to use the user's inputs rather than using explicit integers:
for(int i = min; i <= max; i += increment)
{
System.out.println(i);
}
System.out.println("Enter min value: ");
int minVal = in.nextInt();
System.out.println("Enter max value: ");
int maxVal = in.nextInt();
System.out.println("Enter increment value: ");
int increment = in.nextInt();
for(i=minVal; i<=maxVal; i+=incremement){
System.out.println(i);
}
operator keyword - Decrement operation in Java - Stack Overflow
loops - Decrement by a certain amount on java - Stack Overflow
oop - Java Increment / Decrement Operators - How they behave, what's the functionality? - Stack Overflow
explain this simple increment/decrement operation please
When you put operators ++ or -- in front of variable like ++m or --m, it calculates result and returns new value. When you put them after the variable, it returns the value and then calculates the result.
So, in line 2 it will return 19, but the value in m is 20. Then in line 3 it returns that value of 20, and decreses m to 19. Finally in line 4 it first decrease the value and then returns 18.
More on reddit.comVideos
--a
is similar to
a = a - 1;
which means at first it calculates value of a-1 and then with a = ... it assigns that value back to a.
But in case of -a you are just calculating negative value, but it doesn't doesn't reassign it back to a. So since you are not doing anything with that calculated value it will be lost so compiler informs you that your code doesn't do what you may think it does.
Try explicitly assign that result back to a with
a = -a;
After this instruction a will hold new value which you can use anywhere.
This problem disappears when you are using
System.out.println("Your number is: " + (-a) );
since now compiler sees that calculated value -a is being used (as part of value passed to println method).
Because you didn't assign the result of the unary minus. The pre-decrement includes an assignment.
a = -a; // <-- like this.
In the second use (printing), you are using the value in the print routine (and not updating a).
These are called Pre and Post Increment / Decrement Operators.
x++;
is the same as x = x + 1;
x--;
is the same as x = x - 1;
Putting the operator before the variable ++x; means, first increment x by 1, and then use this new value of x
int x = 0;
int z = ++x; // produce x is 1, z is 1
int x = 0;
int z = x++; // produce x is 1, but z is 0 ,
//z gets the value of x and then x is incremented.
++ and -- are called increment and decrement operators.
They are shortcuts for writing x = x+1 (x+=1) / x = x-1 (x-=1). (assumed that x is a numeric variable)
In rare cases you could worry about the precedence of the incrementation/decrementation and the value the expression returns: Writing ++x it means "increment first, then return", whereas x++ means "return first, then increment". Here we can distinguish between pre- and post increment/decrement operators.
int m = 20; System.out.println(--m); //19 System.out.println(m++); //19 System.out.println(m--); //20 why 20? System.out.println(--m); //18 why 18?
First line prints 19 because -- sign comes before the variable name, and in the second line it remains same because the ++ is after the variable name, right?
Then in the third line, how does it get 20? shouldn't it remain 19? and then in the last line, if previous value of m was 20, then how did it get 18?
When you put operators ++ or -- in front of variable like ++m or --m, it calculates result and returns new value. When you put them after the variable, it returns the value and then calculates the result.
So, in line 2 it will return 19, but the value in m is 20. Then in line 3 it returns that value of 20, and decreses m to 19. Finally in line 4 it first decrease the value and then returns 18.
int m = 20;
System.out.println(--m); // Prints (20-1) = 19 Value of m after expression = 19
System.out.println(m++); // Prints 19 Value of m after expression = (19+1) = 20
System.out.println(m--); // Prints 20 Value of m after expression = (20-1) = 19
System.out.println(--m); // Prints (19-1) = 18 Value of m after expression = 18
You are using Integer which is an immutable object.
Basically your code is
y = new Integer(y.intValue() + 1);
and
y = new Integer(y.intValue() - 1);
Therefore you're creating two new Integer objects that are not the same (==) as the previous objects.
This behaviour is called autoboxing in Java.
Change your
Integer y = 567;
Integer x = y;
to
int y = 567;
int x = y;
and the suprprising behavior will be gone. My guess is that you have stumbled upon Java's implicit autoboxing of primitive values into wrapper objects, and are lead to believe that you are directly manipulating the numbers.