• Post Reply Bookmark Topic Watch Topic
  • New Topic

Substituting the value to the equation(loop)  RSS feed

 
beu curt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

So basically this program will stop at 15 iterations. My problem is I am not able to substitute the value of the answer to the equation.

Here is the code



The output should be



But the one I am getting is



I am really getting frustrated, I can't figure it out. Any help would be very helpful.

Thank you
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what your problem is. Is it that it only asks for input once? In that case that is because you're only asking for input once, it is not in the for loop. If it's regarding the calculation then you need to provide us with more information.

By the way: look at the difference between System.out.print and System.out.printf.
 
beu curt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:I'm not sure what your problem is. Is it that it only asks for input once? In that case that is because you're only asking for input once, it is not in the for loop. If it's regarding the calculation then you need to provide us with more information.

By the way: look at the difference between System.out.print and System.out.printf.


Yep it only asks for one input. The answer for the first input should be the the next value to substituted.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then just assign the result to the input variable at the end of an iteration.

example:
equation = x + 2
input=1

1+2=3
x = 3

x+2
3+2=5
x = 5

x+2
5+2=7

and so on
 
beu curt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Then just assign the result to the input variable at the end of an iteration.

example:
equation = x + 2
input=1

1+2=3
x = 3

x+2
3+2=5
x = 5

x+2
5+2=7

and so on


you meant like this?
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. More like this:

variable x = input()
loop
variable result = calculation(x)
print result
x = result
end loop
 
beu curt
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:No. More like this:

variable x = input()
loop
variable result = calculation(x)
print result
x = result
end loop


You made it work I owe you my life.

Thank very much.
 
Wouter Oet
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome but actually you made it work. I just gave hints.

<edit>typo</edit>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!