日期:2014-05-20 浏览次数:20647 次
import java.util.*; public class Test { public static void main(String[] args) { int a=0; Scanner input = new Scanner(System.in); System.out.println("请输入月份:"); do { a = input.nextInt(); if(a>12||a<1){ System.out.println("输入错误,请重新输入:"); } else{ break; } } while (true); switch (a) { case 1: case 2: case 3: System.out.println("春天"); break; case 4: case 5: case 6: System.out.println("夏天"); break; case 7: case 8: case 9: System.out.println("秋天"); break; case 10: case 11: case 12: System.out.println("冬天"); break; } } }
------解决方案--------------------
if(a>0&&a<13)
{
if(a<=6)
{
if(a<=3) System.out.println("春天");
else System.out.println("夏天");
}
else
{
if(a<=9) System.out.println("秋天");
else System.out.println("冬天");
}
}
else System.out.println("输入有误!");
循环这样的话,会比较简洁点
------解决方案--------------------
public class Suilian4 {
public static void main(String[] args) {
int a;
Scanner input = new Scanner(System.in);
System.out.println("请输入月份:");
a = input.nextInt();
while(a<0||a>13){//加一个while循环判断输入的月份是否在范围之内,不在范围之内循环再次输入
System.out.println("输入错误,请重新输入:");
System.out.println("请输入月份:");
a = input.nextInt();
break;
}
switch (a) {
case 1:
case 2:
case 3:
System.out.println("春天");
break;
case 4:
case 5:
case 6:
System.out.println("夏天");
break;
case 7:
case 8:
case 9:
System.out.println("秋天");
break;
case 10:
case 11:
case 12:
System.out.println("冬天");
break;
}
}
}