弄了一下午,还有点问题,谁帮忙看看

timepassby 2008-05-13 05:27:44
题目: 编写一个程序,接受用户输入的1~12之间的整数,若不符合则重输入,,利用swich语句输出对应月份的天数.

弄了一下午,现在可以运行,但是当输入的整不符合要求时,按说应该继续输入才对啊,但是如果输入不符合条件时就停止了,怎么回事,谁告诉下,现在不知道心情怎么样,这个程序应该是很简单的了,我竟然弄了一下午,而且现在还有问题


import java.util.*;
public class test1 {

/**
* @param args
*/
public static void main(String[] args) {
int month;
System.out.println("Input the month");
do{
Scanner sc = new Scanner(System.in);
month = sc.nextInt();
if(month < 1||month > 12)
System.out.println("Error,Input again");
}while(month > 12&&month < 1);// TODO Auto-generated method stub
switch (month){
case 1:System.out.println("31");break;
case 3:System.out.println("31");break;
case 5:System.out.println("31");break;
case 7:System.out.println("31");break;
case 8:System.out.println("31");break;
case 10:System.out.println("31");break;
case 12:System.out.println("31");break;
case 2:System.out.println("28");break;
case 4:System.out.println("30");break;
case 6:System.out.println("30");break;
case 9:System.out.println("30");break;
case 11:System.out.println("30");break;
}
}
}
...全文
157 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
evoloyeu 2008-05-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 l6443305 的回复:]
import java.util.*;
public class test1 {

/**
* @param args
*/
public static void main(String[] args) {
int month;
System.out.println("Input the month");
do{
Scanner sc = new Scanner(System.in);
month = sc.nextInt();
if(month < 1 ¦ ¦month > 12)
System.out.println("Error,Input again");
}while(month < 12&&month > 1);// TODO Auto-generated method stub //应该是在1~12之间
switch …
[/Quote]
可爱的萝卜 2008-05-13
  • 打赏
  • 举报
回复
import java.util.*;
public class test1 {

/**
* @param args
*/
public static void main(String[] args) {
int month;
System.out.println("Input the month");
do{
Scanner sc = new Scanner(System.in);
month = sc.nextInt();
if(month < 1 ¦ ¦month > 12)
System.out.println("Error,Input again");
}while(month < 12&&month > 1);// TODO Auto-generated method stub //应该是在1~12之间
switch (month){
case 1:System.out.println("31");break;
case 3:System.out.println("31");break;
case 5:System.out.println("31");break;
case 7:System.out.println("31");break;
case 8:System.out.println("31");break;
case 10:System.out.println("31");break;
case 12:System.out.println("31");break;
case 2:System.out.println("28");break;
case 4:System.out.println("30");break;
case 6:System.out.println("30");break;
case 9:System.out.println("30");break;
case 11:System.out.println("30");break;
}
}
}
Y382904800 2008-05-13
  • 打赏
  • 举报
回复
while这里改为:
while(month >12 ¦ ¦ month   <1); 
okchenpeng 2008-05-13
  • 打赏
  • 举报
回复
mport java.util.*;
public class month {

/**
* @param args
*/
public static void main(String[] args) {
int month=0;
System.out.println("Input the month");



do{
Scanner sc =new Scanner (System.in);
month = Integer.parseInt(sc.nextLine());


if(month>12 || month<1)
{
System.out.println("Error,Input again");

}
}while(month >12|| month <1);


switch (month)
{
case 1:System.out.println("31");break;
case 3:System.out.println("31");break;
case 5:System.out.println("31");break;
case 7:System.out.println("31");break;
case 8:System.out.println("31");break;
case 10:System.out.println("31");break;
case 12:System.out.println("31");break;
case 2:System.out.println("28");break;
case 4:System.out.println("30");break;
case 6:System.out.println("30");break;
case 9:System.out.println("30");break;
case 11:System.out.println("30");break;
}
}
}
skyqiang 2008-05-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cyberpeng 的回复:]
Java codewhile(month > 12 && month < 1);


什么数能够即大于12又小于1呢?
[/Quote]
lovingprince 2008-05-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 anqini 的回复:]
Java code
public static void main(String[] args) {
int month=0;
System.out.println("Input the month");
do{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();//这里你要读整行!不是读一位!
try {
month = Integer.valueOf(s);
}catch(Exception e) {
Syste…
[/Quote]

楼上正解,楼主要是输入其他的非数字就会出现异常。
anqini 2008-05-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 anqini 的回复:]
Java code
public static void main(String[] args) {
int month=0;
System.out.println("Input the month");
do{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();//这里你要读整行!不是读一位!
try {
month = Integer.valueOf(s);
}catch(Exception e) {
Syste…
[/Quote]
我理解錯了nextInt,主要是邏輯判斷!if(month < 1 ||month > 12)
anqini 2008-05-13
  • 打赏
  • 举报
回复

public static void main(String[] args) {
int month=0;
System.out.println("Input the month");
do{
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();//这里你要读整行!不是读一位!
try {
month = Integer.valueOf(s);
}catch(Exception e) {
System.out.println("输入错误!请重新输入!");
System.exit(1);
}
if(month < 1 ||month > 12)
System.out.println("Error,Input again");
}while(month > 12||month < 1);
switch (month){
case 1:System.out.println("31");break;
case 3:System.out.println("31");break;
case 5:System.out.println("31");break;
case 7:System.out.println("31");break;
case 8:System.out.println("31");break;
case 10:System.out.println("31");break;
case 12:System.out.println("31");break;
case 2:System.out.println("28");break;
case 4:System.out.println("30");break;
case 6:System.out.println("30");break;
case 9:System.out.println("30");break;
case 11:System.out.println("30");break;
}
}

cyberpeng 2008-05-13
  • 打赏
  • 举报
回复
while(month > 12 && month < 1);

什么数能够即大于12又小于1呢?

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧