Computer Science Canada What's wrong with my code? (Java) |
Author: | GDoughtUpInIt [ Mon Jul 05, 2004 9:32 am ] |
Post subject: | What's wrong with my code? (Java) |
Eventually, I'm supposed to display all 7 days of the week with what this guy is going to be doing. So far I only got 1 day done, but I'm still trying to display all the days on the screen, just to get myself started. But I can't get it to display: MONDAY: (all the stuff i put there so far too) TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY in a loop. Can somebody tell me what is wrong with my code below. I am a beginner in this, but I'm really trying to learn so any help will be appreciated. Thanks Quote: class joesWeek{
public static void main (String[] args){ int x = 0; int money = 200; int shoeCost = 30; double shoeTax = 1.15; System.out.println("Java Joe has $200."); while (x<7) { x=x+1; if (x==1) { System.out.println("MONDAY: Joe pays $" + (shoeCost*shoeTax) + " for shoes. He now has $" + (money-(shoeCost*shoeTax)) + " left."); if (x==2) { System.out.println("TUESDAY:"); if (x==3) { System.out.println("WEDNESDAY:"); if (x==4) { System.out.println("THURSDAY:"); if (x==5) { System.out.println("FRIDAY:"); if (x==6) { System.out.println("SATURDAY:"); if (x==7) { System.out.println("SUNDAY:"); }else{ System.out.println(x); } } } } } } } } } } |
Author: | zylum [ Mon Jul 05, 2004 11:33 am ] | ||
Post subject: | |||
you have all youre ifs inside each other thats why only the first one shows up... it should look like this:
btw, you might want to look up the "switch" syntax... if you know turing, its like "case" |
Author: | McKenzie [ Mon Jul 05, 2004 11:34 am ] | ||
Post subject: | Re: What's wrong with my code? (Java) | ||
GDoughtUpInIt wrote: if (x==1) {
System.out.println("MONDAY: Joe pays $" + (shoeCost*shoeTax) + " for shoes. He now has $" + (money-(shoeCost*shoeTax)) + " left."); if (x==2) { ... } } you have all of the branches of your if nested inside x==1. Try:
|
Author: | wtd [ Tue Jul 06, 2004 1:18 pm ] | ||
Post subject: | |||
Now tested and shown to work.
|