| | |
Confused about loops and switch
![]() |
•
•
Join Date: Jan 2005
Posts: 22
Reputation:
Solved Threads: 0
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,");
}
}
}
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,");
}
}
}
•
•
•
•
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.
:?: Sometimes i wonder if i'm on the right planet :!:
•
•
•
•
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.
Java Syntax (Toggle Plain Text)
for (int x = 1; x < 13; x++) { // Could also use the while loop you have System.out.print("On the " + x); Switch(x) { case 1 : System.out.print("st"); break; case 2 : System.out.print("nd"); break; ......... // Etc. you get the drift. } System.out.println(" day of ....."); Switch (x) { case 12 : System.out.println("the thing you get on the 12th day,"); case 11 : System.out.println("whatever you get on the 11th day,"); ....... //Etc. Etc. case 1 : System.out.println("the first thing you get"); break; } }
This should get you what you want.
Have fun,:cheesy:
Guido
:?: Sometimes i wonder if i'm on the right planet :!:
![]() |
Similar Threads
- jComboBox getSelectedIndex (Java)
- confused loops (C++)
- help for program involving switch loops and file (C++)
- Cannot get switch to execute (C++)
Other Threads in the Java Forum
- Previous Thread: Creating Graphs
- Next Thread: A question on java script?(Please answer ASAP)
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide image int j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux mac main map method mobile myregfun netbeans nonstatic notdisplaying number online pearl printf problem program project qt researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields thread threads time title tree tutorial-sample update variablebinding windows working xor





