• Post Reply Bookmark Topic Watch Topic
  • New Topic

variable confusion  RSS feed

 
Tom Sinclair
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm very new to this forum and I'm taking an online java class which is slaying me. I am having an issue with this program. It is a program that I had previously written but this time we are to use "if else" statements to do error checking. That idea behind the program is that you are buying an item from a vending machine (25<=cost<=100) in cents and that the items cost must be in 5 cent incriments or multiples of 5 cents. i'm not sure why but at like 40 I'm getting an error saying that the variable "amount" has not been initialized. I can't see any problems with the code but I'm new. Any help would be much appreciated. here's the code:

Simulates a vending machine operation through the use of the remainder operator and keyboard input.
Uses error checking to make sure that the input is greater than or equal to 25 cents or less than or
equal to 100 cents. The input must also be in incriments of 5.


*/




 
Tom Sinclair
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear Bibeault. That is much easier to read now. I might have gotten a reply if I had posted it like this originally.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hint: always use braces when writing if/else statements or loops. How does that changed things?
 
Tom Sinclair
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Got it! I just found out that when you have more than a single line within an else if statement you need to include the multiline body into braces! Thanks for the hint!!! Did you learn on a forum like this? I can normally pick things up like math and physics by just reading the book and doing problems but this is a different story. I spent a hours looking at that exact code and couldn't figure out my mistake.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Sinclair wrote:I just found out that when you have more than a single line within an else if statement you need to include the multiline body into braces!

It's considered a good practice to always use the braces even for single-statement bodies.

Did you learn on a forum like this?

I've been doing this for 35 years. There wasn't even a net back then.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming is hard to learn by just reading. It's takes lots and lots of practice, so don't feel bad. Just keep plugging away. It becomes easier with practice.
 
Tom Sinclair
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BAUCE-MODE!!!
Thanks again for your help!


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!