我正在处理的作业是为了显示圣诞节的十二天的经文,具体取决于您输入的数字(1-12)。
涉及两个while
循环。第一种是验证输入的数字是否在该范围内。这很好。第二个while循环使用两个switch
语句。第一个将正确的后缀放在你键入的数字的末尾。第二个开关确定与当天相关的礼物,然后它显示如下:
圣诞节的第三天,我的真爱给了我
三只法国母鸡,
两只乌龟鸽子,
梨树中的鹧
这是我到目前为止所做的事情:
import java.util.Scanner;
public class TwelveDays
{
public static void main (String[] args)
{
int lastDay = 0; // last day
final int MAX = 12;
Scanner scan = new Scanner(System.in);
//Get the last day and use input validation
System.out.println("How many days (1 to 12)? ");
lastDay = scan.nextInt();
//Begin 1st while
while (lastDay <= 0 || lastDay > MAX)
{
System.out.println("How many days (1 to 12)? ");
lastDay = scan.nextInt();
}
int day = 1; //loop control variable for song verses
//Begin 2nd while
while ( )
{
System.out.print("On the " + lastDay);
//Output the suffix for the day
String suffix = " "; // temporary suffix value
//Begin 1st switch
switch(lastDay)
{
case 1:
suffix = ("st");
System.out.print(suffix);
break;
case 2:
suffix = ("nd");
System.out.print(suffix);
break;
case 3:
suffix = ("rd");
System.out.print(suffix);
break;
default:
suffix =("th");
System.out.print(suffix);
break;
}
System.out.println(" day of Christmas my true love gave to me");
//Begin 2nd switch
switch(lastDay)
{
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,");
break;
}
}
}
}
很抱歉,如果我的格式不佳。我是java的新手。看起来好像无论我在第二次使用什么条件时,它总是以无限循环结束,或者它只输出我放入12次的任何数量的礼物。
答案 0 :(得分:0)
据我了解您的要求,第一个switch
应该在while循环之外,因为每个输入只需要一次。
现在,你的主要问题是第二个while循环将无限次迭代,因为没有条件或break语句。
所以你可以做的是,在第二个while循环结束时递减变量lastDay
,并在它为0时断开循环。
以下代码必须提供帮助:
import java.util.Scanner;
public class TwelveDays {
public static void main(final String[] args)
{
int lastDay = 0; // last day
final int MAX = 12;
Scanner scan = new Scanner(System.in);
// Get the last day and use input validation
System.out.println("How many days (1 to 12)? ");
lastDay = scan.nextInt();
// Begin 1st while
while (lastDay <= 0 || lastDay > MAX)
{
System.out.println("How many days (1 to 12)? ");
lastDay = scan.nextInt();
}
int day = 1; // loop control variable for song verses
System.out.print("On the " + lastDay);
// Output the suffix for the day
String suffix = " "; // temporary suffix value
// Begin 1st switch
switch (lastDay) {
case 1:
suffix = ("st");
System.out.print(suffix);
break;
case 2:
suffix = ("nd");
System.out.print(suffix);
break;
case 3:
suffix = ("rd");
System.out.print(suffix);
break;
default:
suffix = ("th");
System.out.print(suffix);
break;
}
System.out.println(" day of Christmas my true love gave to me");
// Begin 2nd while
while (true) {
// Begin 2nd switch
switch (lastDay) {
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,");
break;
}
lastDay--;
if (lastDay == 0) {
break;
}
}
}
}
我建议你阅读更多有关循环和逻辑的内容,因为我可以看到你是编程新手。 从技术上讲,你根本不需要第二个while循环,我试着尽量减少代码中的变化并给出答案。