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);
}
How do I use Increment operators (++) to increase a value more than 1?
How to make for loops in Java increase by increments other than 1 - Stack Overflow
Post and Pre Incrementing?
Is there a way to pre increment by more than 1 in Java? - Stack Overflow
Videos
Just to preface, this isnt homework - Im self learning. Ive used Stack overflow but I dont think im searching the right thing, so I cant find what Im looking for
For my study book (Learn Java the Hard Way) , i'm on an exercise where I have to increase
i = 5;
to 10 by only using ++ , i know I could do
i++; on repeated lines but my study drill says I can do it on one. Which I would like to figure out
"Add code below the other Study Drill that resets i’s value to 5, then using only ++, change i’s value to 10 and display it again. You may change the value using several lines of code or with just one line if you can figure it out. "
Perhaps ive read it wrong but I cant find a way of using only ++ to do this in one line.
Ty for the help :)
That’s because j+3 doesn’t change the value of j. You need to replace that with j = j + 3 or j += 3 so that the value of j is increased by 3:
for (j = 0; j <= 90; j += 3) { }
Since nobody else has actually tackled Could someone please explain this to me? I believe I will:
j++ is shorthand, it's not an actual operation (ok it really IS, but bear with me for the explanation)
j++ is really equal to the operation j = j + 1; except it's not a macro or something that does inline replacement. There are a lot of discussions on here about the operations of i+++++i and what that means (because it could be intepreted as i++ + ++i OR (i++)++ + i
Which brings us to: i++ versus ++i. They are called the post-increment and pre-increment operators. Can you guess why they are so named? The important part is how they're used in assignments. For instance, you could do: j=i++; or j=++i; We shall now do an example experiment:
// declare them all with the same value, for clarity and debug flow purposes ;)
int i = 0;
int j = 0;
int k = 0;
// yes we could have already set the value to 5 before, but I chose not to.
i = 5;
j = i++;
k = ++i;
print(i, j, k);
//pretend this command prints them out nicely
//to the console screen or something, it's an example
What are the values of i, j, and k?
I'll give you the answers and let you work it out ;)
i = 7, j = 5, k = 7; That's the power of the pre and post increment operators, and the hazards of using them wrong. But here's the alternate way of writing that same order of operations:
// declare them all with the same value, for clarity and debug flow purposes ;)
int i = 0;
int j = 0;
int k = 0;
// yes we could have already set the value to 5 before, but I chose not to.
i = 5;
j = i;
i = i + 1; //post-increment
i = i + 1; //pre-increment
k = i;
print(i, j, k);
//pretend this command prints them out nicely
//to the console screen or something, it's an example
Ok, now that I've shown you how the ++ operator works, let's examine why it doesn't work for j+3 ... Remember how I called it a "shorthand" earlier? That's just it, see the second example, because that's effectively what the compiler does before using the command (it's more complicated than that, but that's not for first explanations). So you'll see that the "expanded shorthand" has i = AND i + 1 which is all that your request has.
This goes back to math. A function is defined where f(x) = mx + b or an equation y = mx + b so what do we call mx + b ... it's certainly not a function or equation. At most it is an expression. Which is all j+3 is, an expression. An expression without assignment does us no good, but it does take up CPU time (assuming the compiler doesn't optimize it out).
I hope that clarifies things for you and gives you some room to ask new questions. Cheers!
Just put the increment statement in parentheses. For example, the following will output pre: 2:
int i = 0;
System.out.println(
((i+=2) == 0)
? "post: " + i : "pre: " + i);
However, writing code like this borders on obfuscation. Splitting up the statement into multiple lines will significantly improve readability.
Not sure if there is a confusion in terminology going on, but += is not a post or pre-increment operator! Java follows the C/C++ definition of post-increment/pre-increment and they are well defined in the standard as unary operators. += is a shortcut for a binary operator. It evaluates to this:
lvalue1 += 5 ;
// is really (almost)
lvalue1 = lvalue1 + 5;
The assembler for the instruction doesn't look exactly like the binary version but at the level your using Java you do not see that.
The post-increment/pre-increment are unary operators that function kind of like this:
i++ ; // is something like _temp = i; i = i + 1; return temp;
++i; // is something like i = i + 1; return i;
This is just an example of how it works, the byte code doesn't translate too multiple statements for the post-increment case.
In your example, you could say a post-increment occurs but really, its just an increment. which is why I believe you have made the (incorrect) leap that it might be possible to have a pre-increment version of the same operation. But such a thing does not exist in C, C++, or Java.