Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 What's wrong with my code? (Java)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
GDoughtUpInIt




PostPosted: 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);
}
}
}
}
}
}
}
}
}
}
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Mon Jul 05, 2004 11:33 am   Post subject: (No subject)

you have all youre ifs inside each other thats why only the first one shows up... it should look like this:

code:
class joesWeek{
public static void main (String[] args){
int money = 200;
int shoeCost = 30;
double shoeTax = 1.15;

System.out.println("Java Joe has $200.");

for (int i = 1; i <= 7; i++) {
if (i == 1) {
println ("whatever");
} else if (i == 2) {
println ("blah")
} else if (i == 3) {
println ("more stuff") {
}
}
}


btw, you might want to look up the "switch" syntax... if you know turing, its like "case"
McKenzie




PostPosted: 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:
code:

if (x==1){
     ...
}
else if (x==2){
     ...
}
else if (x==3){
     ...
}
...
wtd




PostPosted: Tue Jul 06, 2004 1:18 pm   Post subject: (No subject)

Now tested and shown to work.

code:
import java.lang.*;
import java.io.*;

public class MyProgram {
   public static class Day {
      public static class Event {
         private String name;
         private String description;
         private boolean done;

         public Event(String n, String desc) {
            name = n;
            description = desc;
            done = false;
         }

         public String getName() { return name; }
         public String getDescription() { return description; }
      }

      private String name;
      private Event[] events;
      private int eventCount;

      public Day(String n, int numEvents) {
         name = n;
         events = new Event[numEvents];
         eventCount = 0;
      }

      public String getName() { return name; }
      public Event[] getEvents() { return events; }
      public int getEventCount() { return eventCount; }
      public boolean eventCountFull() { return eventCount >= events.length; }
      private void incrementEventCount() { eventCount++; }

      public Event getEventByName(String n) {
         for (int i = 0; i < events.length; i++) {
            if (events[i].getName() == n)
               return events[i];
         }
         return null;
      }

      public void addEvent(String n, String desc) {
         if (!eventCountFull()) {
            events[eventCount] = new Event(n, desc);
            incrementEventCount();
         }
      }
   }

   public static class Week {
      private Day[] days;

      public Week(int numEventsPerDay) {
         days = new Day[7];
         String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

         for (int i = 0; i < 7; i++) {
            days[i] = new Day(dayNames[i], numEventsPerDay);
         }
      }

      public Day[] getDays() { return days; }

      public Day getDay(String n) {
         for (int i = 0; i < 7; i++) {
            if (days[i].getName() == n)
               return days[i];
         }
         return null;
      }
   }

   public static void main(String[] args) {
      Week joesWeek = new Week(8);

      double joesMoney = 200.00;
      double shoeCost  =  30.00;
      double shoeTax   =   1.15;
      double totalShoeCost = shoeCost * shoeTax;
      double remainingMoney = joesMoney - totalShoeCost;

      Day monday = joesWeek.getDay("Monday");
      monday.addEvent("Pay For Shoes", "Joe pays $" + totalShoeCost + " and is left with $" + remainingMoney);

      Day[] thisWeek = joesWeek.getDays();

      for (int i = 0; i < 7; i++) {
         Day today = thisWeek[i];
         Day.Event[] todaysEvents = today.getEvents();
         int numberOfEvents = today.getEventCount();

         System.out.println(today.getName());

         for (int j = 0; j < numberOfEvents; j++) {
            Day.Event thisEvent = todaysEvents[j];
            System.out.println("\t" + thisEvent.getName() + ": " + thisEvent.getDescription());
         }
      }
   }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: