943,987 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 5370
  • Java RSS
Mar 23rd, 2005
0

Confused about loops and switch

Expand Post »
I'm supposed to write a loop to loop through the days, the write a switch statement for the verses. The program should print out just like the song goes.

For example:

On the 1st day of xmas my true love gave to me,
a partidge in a pear tree
On the 2nd day of xmas my true love gave to me,
Two turtle doves, and
a partidge in a pear tree

Can someone offer me some advice?

public class Xmas
{

public static void main (String[] args)

{

int ID=0;
int verse=0;
int count = 1;
while (count <=12)



{
System.out.println("On the " +count+" day of Xmas, my true love gave to me");
count++;

}

switch(ID)
{
case 1:
System.out.println ("A partridge in a pear tree");
break;

case 2:
System.out.println ("Two turtle doves, and");
break;

case 3:
System.out.println ("Three french hens,");
break;

case 4:
System.out.println ("Four calling birds,");
break;

case 5:
System.out.println ("Five golden rings,");
break;

case 6:
System.out.println ("Six geese a laying,");
break;

case 7:
System.out.println ("Seven swans a swimming,");
break;

case 8:
System.out.println ("Eight maids a milking,");
break;

case 9:
System.out.println ("Nine ladies dancing,");
break;

case 10:
System.out.println ("Ten lords a leaping,");
break;

case 11:
System.out.println ("Eleven pipers piping,");
break;

case 12:
System.out.println ("Twelve drummers drumming,");

}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jengels is offline Offline
22 posts
since Jan 2005
Mar 23rd, 2005
0

Re: Confused about loops and switch

i see no error here, but you should print "a partidge in a pear tree" after the switch....
and you can use "for" loop for these kind of things i think...
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Mar 23rd, 2005
0

Re: Confused about loops and switch

Quote originally posted by jengels ...
I'm supposed to write a loop to loop through the days, the write a switch statement for the verses. The program should print out just like the song goes.

For example:

On the 1st day of xmas my true love gave to me,
a partidge in a pear tree
On the 2nd day of xmas my true love gave to me,
Two turtle doves, and
a partidge in a pear tree
Unless i am missing something, this code will only print :
On the 1 day of xmas my true love gave to me,
On the 2 day of xmas my true love gave to me,
etc... , as the switch statement is outside the while loop.

You can use this structure if you want but you need to include both the 'on the ..' println statement and the switch statement in the while loop block.

Also, the switch statement as it is now will only print one line, and then break for the next loop (Which there isn't now).

Remember that a case in a switchblock does not have to be stopped with a break; so if you set your cases top-down (12,11,10 etc) and let the program fall-through from one case to another you will get the desired result.

You can also use a switch to get the system to print 1st 2nd etc. instead of 1 2 3 .

Hope this gave you a usefull direction to go in.
Reputation Points: 13
Solved Threads: 0
Light Poster
DeepZ is offline Offline
27 posts
since Sep 2004
Mar 23rd, 2005
0

Re: Confused about loops and switch

I'm sorry, the program you see is how far I've got to this pooint. I was confused about hoe to incorporate the switch and the loop together.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jengels is offline Offline
22 posts
since Jan 2005
Mar 23rd, 2005
0

Re: Confused about loops and switch

Quote originally posted by jengels ...
I'm sorry, the program you see is how far I've got to this pooint. I was confused about hoe to incorporate the switch and the loop together.
No problem. I'll try and give you a better idea of the workings (just the code, sketchy, in the main method, i have left out the class defs etc.):

Java Syntax (Toggle Plain Text)
  1. for (int x = 1; x < 13; x++) { // Could also use the while loop you have
  2. System.out.print("On the " + x);
  3. Switch(x) {
  4. case 1 : System.out.print("st");
  5. break;
  6. case 2 : System.out.print("nd");
  7. break;
  8. ......... // Etc. you get the drift.
  9. }
  10. System.out.println(" day of .....");
  11. Switch (x) {
  12. case 12 : System.out.println("the thing you get on the 12th day,");
  13. case 11 : System.out.println("whatever you get on the 11th day,");
  14. ....... //Etc. Etc.
  15. case 1 : System.out.println("the first thing you get");
  16. break;
  17. }
  18. }

This should get you what you want.

Have fun,:cheesy:
Guido
Reputation Points: 13
Solved Threads: 0
Light Poster
DeepZ is offline Offline
27 posts
since Sep 2004
Mar 23rd, 2005
0

Re: Confused about loops and switch

Thanks for the help!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jengels is offline Offline
22 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Creating Graphs
Next Thread in Java Forum Timeline: A question on java script?(Please answer ASAP)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC