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

时间:2008-05-13 23:47:00   来源:论坛整理  作者:  编辑:chinaitzhe
题目: 编写一个程序,接受用户输入的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;
}
}
}
网友回复:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



while(month > 12 && month < 1);


什么数能够即大于12又小于1呢?
网友回复:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





 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; 

           } 

       } 






网友回复:
引用 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…

我理解錯了nextInt,主要是邏輯判斷!if(month < 1 ¦ ¦month > 12)

网友回复:
引用 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…


楼上正解,楼主要是输入其他的非数字就会出现异常。
网友回复:
引用 1 楼 cyberpeng 的回复:
Java codewhile(month > 12 && month < 1);


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

网友回复: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;
}
}
}
网友回复:while这里改为:
Java code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/



while(month >12 ¦ ¦ month   <1); 




网友回复: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;
}
}
}
网友回复:
引用 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 …
关键字:下午,有点问题,帮忙,看看,

相关文章

文章评论

共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面