日期:2014-05-20  浏览次数:20661 次

java中控制台输入
java中怎么用system.in方法能够从控制台不停的输入...
怎么判断年份是否为闰年....要求从控制台输入,当输入不为正整数时,提示请输入错误,请输入正整数,然后能继续从控制台输入,知道输入为正整数为止...

------解决方案--------------------
for example
Java code

import java.util.*;
public class Test {
    public static void main(String[] args) throws Throwable {
        Scanner sc = new Scanner(System.in);
        String buf;
        int year;
        GregorianCalendar c = new GregorianCalendar();
        while (true) {
            System.out.print("请输入年([yyyy]格式):");
            buf = sc.nextLine();
            try {
                year = Integer.parseInt(buf);
                if (year < 0) {throw new Exception("负数");}
                System.out.println(c.isLeapYear(year) ? "闰年" : "不是闰年");
                System.out.print("是否继续?(y/n):");
                buf = sc.nextLine();
                if ("y".equalsIgnoreCase(buf)) break;
            } catch (Exception e) {
                System.out.println("输入错误,请重输");
            }
        }
    }
}