• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with for statement  RSS feed

 
North West
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not looking for an answer.

I am trying to write a code that allows a user to input the number of stones in a basket,
then put the stones in my pocket, after the stones are in my pocket, the stones must be taken out, one by one, and thrown in a pond.

I get stuck at this part because my for statement has to subtract the number of stones in my pocket (one at a time) and add a stone to the pond (one by one) at the same time.

I can get the pond stones to increase until the max amount of stones with the following:

for (int i = 0; i < stones; i++) {

System.out.println("Pocket:" ? " " + "Pond:" + i);

I can't figure out how to reduce one and add to other. Is it possible to do this in one for statement? I tried to put stones-- in place of the question mark, but that is not working. Again, I am not looking for an answer. I am trying to learn.
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can initialize, assign, and modify multiple variables in a for statement.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
North West wrote:I can't figure out how to reduce one and add to other. Is it possible to do this in one for statement?

Sure, but do you really need to? It isn't that complicated to have two variables is it? just add to one and subtract from the other.

Tip: Don't try to be "clever" until you've got a piece of code that WORKS.

I tried to put stones-- in place of the question mark, but that is not working. Again, I am not looking for an answer. I am trying to learn.

It looks like you're trying to use the ternary operator, but you're not exactly sure how.

Again: write some code that WORKS - even if it's a bit more verbose - then see if you can get it to work another way.

---

Just going back to your original question: Suppose that, instead of your 'stones' variable, you had a constant called 'NUMBER_OF_STONES', viz:

private final int NUMBER_OF_STONES = 10;   (or whatever number you want)
private int pond = 0;

How do you think that might help you to "subtract and add" at the same time? Note: you might need to write a method to get the "number of stones in your pocket".

PS: Using "all caps" for constants is just a convention; but its quite widely used.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski never wrote: . . .
private int stones = 10;   (or whatever number you want)
private int pond = 0;
private int pocket = 10;
. . .
Compare that version with what Winston actually wrote. See if you can work out why that version with three variables is more error‑prone than what Winston did write and why you should avoid that version.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!