• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Loops

 
benito kamelas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody, so here is a bit of code for a homework I have for tomorrow. The problem I am having is that when I pick 9 products from the menu (Scenario 1), it gives me the total of what it cost. But when I pick only one product from the list (Check Scenario 2 & 3), it STILL gives me the total cost as if I had still picked the 9 products (Check last screenshot). Check images below to understand better.

Any help would be appreciated.

Thanks!







 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see the logic error, but I can give you some suggestions...

1) put in more System.out.println(); statements. You will take these out later, but these are GREAT for debugging. It will let you see what your code is really doing.

2) Simplify. Comment out all but (say) 2-3 choices. no need to clutter up your non-working code with more stuff that doesn't work. Get it working with 2-3, and then you can add in the remaining 7-8.

3) Consider using an array instead of 10 variables named nProductX. This kind of solution doesn't scale well. Imagine if you had 100 or 10,000 products.

4) Consider starting over. writing almost 200 lines of code only to find out it doesn't work is only going to lead to frustration. I NEVER write more than 2-3 before I recompile and TEST TEST TEST.
 
benito kamelas
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply,

Appreciate your idea of putting arrays in it. Makes it more easier instead of putting it one by one.

Could you be more specific on the system print and how does it find the bugs? I mean, I know what its for but didn't know it worked as a debugger.

Have re done this homework and always tested it one by one, but the error looks like its coming from the first IF statement. Have tried to incorporate another IF statement within it or an ELSE IF to see if that works. Even I tried to put constant variable and it also didn't work. The reason I put the whole work as if it was done is to show and seek guiding on where might be the mistake.
 
Henry Wong
author
Marshal
Pie
Posts: 20883
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Two obvious issues...

First, you ask for the item at the end of the loop -- and by the time you reach there, you processed every item on the list. So, you start off with every item purchased.

Second, the choice item number is incorrect. Choosing 1, or 2, or 3, or any number of the items on the list doesn't work. The check is by it's price, not by it's choice. So, if you want a gMac, you have to enter 1299, and not 6.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12084
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
benito kamelas wrote:Could you be more specific on the system print and how does it find the bugs? I mean, I know what its for but didn't know it worked as a debugger.

System.out.println (S.o.p) doesn't find bugs. Your BRAIN finds them. S.o.p just helps you see what is really happening so you can understand.

What I will often do is put in a statement just before each loop starts, and print out the value I'm about to test (this doesn't work on a for-loop, but will for a while).

Then, the first statement in the loop will again be the value. So just before your line 72, i'd do something like:



Before each if-statement in your loop, print nNumber and nProductX (which will be easier one you have an array). Then you can see what the values are, and why it is or isn't entering the if-statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic