import java.util.Scanner;public class myBirthday
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
System.out.println ("Enter your name (pinyin):");
String name = scan.nextLine();
System.out.println ("Your name is:" +name);
System.out.println (" The length of your name is:" +name.length());
System.out.println("Enter the day of your birth (1 to 31):");
int day= scan.nextInt();
System.out.println ("You entered:" + day);
if (day %2==0)
System.out.println( "The day is an even-numbered day.");
else
System.out.println("The day is an odd-numbered day.");
System.out.println("Enter the month of your birth (1 to 12):");
int month= scan.nextInt();
System.out.println(" The month of your birth is:");
switch (month)
{
case 1:
System.out.println ("January");
break;
case 2:
System.out.println ("February");
break;
case 3:
System.out.println ("March");
break;
case 4:
System.out.println ("April");
break;
case 5:
System.out.println ("May");
break;
case 6:
System.out.println ("June");
break;
case 7:
System.out.println ("July");
break;
case 8:
System.out.println ("Augest");
break;
case 9:
System.out.println ("September");
break;
case 10:
System.out.println ("October");
break;
case 11:
System.out.println ("November");
break;
case 12:
System.out.println ("December");
break;
default:
System.out.println ("Please enter again.");
break;
}
}
}
default判断完后,如果输入大于12,怎么让程序返回做再次判断?
{
public static void main (String[] args)
{
Scanner scan = new Scanner (System.in);
System.out.println ("Enter your name (pinyin):");
String name = scan.nextLine();
System.out.println ("Your name is:" +name);
System.out.println (" The length of your name is:" +name.length());
System.out.println("Enter the day of your birth (1 to 31):");
int day= scan.nextInt();
System.out.println ("You entered:" + day);
if (day %2==0)
System.out.println( "The day is an even-numbered day.");
else
System.out.println("The day is an odd-numbered day.");
System.out.println("Enter the month of your birth (1 to 12):");
int month= scan.nextInt();
System.out.println(" The month of your birth is:");
switch (month)
{
case 1:
System.out.println ("January");
break;
case 2:
System.out.println ("February");
break;
case 3:
System.out.println ("March");
break;
case 4:
System.out.println ("April");
break;
case 5:
System.out.println ("May");
break;
case 6:
System.out.println ("June");
break;
case 7:
System.out.println ("July");
break;
case 8:
System.out.println ("Augest");
break;
case 9:
System.out.println ("September");
break;
case 10:
System.out.println ("October");
break;
case 11:
System.out.println ("November");
break;
case 12:
System.out.println ("December");
break;
default:
System.out.println ("Please enter again.");
break;
}
}
}
default判断完后,如果输入大于12,怎么让程序返回做再次判断?
不过建议改下程序结构,尽量不用GOTO语法
用一个while的死循环,当用户输入小于13的时候,才跳出去继续往下运行,不然就提示输入的值不对,重新输入
while(month > 12) {
month = scan.netInt()
}
这样行了吧?
int month= scan.nextInt();
while(month > 12) {
month = scan.netInt()
}
while(month > 12) {
month = scan.netInt();
if(month == 13){
break;
}
}
你得给个跳出来的条件啊
int month= scan.nextInt();
while(month > 12) {
month = scan.netInt();
if(month <=12{
break;
}
}
switch
....}while(写判断,若条件不满足,继续);
static void getMonths(int m){
switch(m):{
case 1: System.out.println("JAN");break
.....
default: System.out.println("your input is not between 1 to 12");
myBirthday.getMonth(scan.nextInt());
}
}
while(month > 12) {
month = scan.netInt();
if(month <=12){
System.out.println(" The month of your birth is:"+ month);
}
switch(month){
.......
}