java中控制台输入

永远的菜鸟 2011-11-16 02:12:15
java中怎么用system.in方法能够从控制台不停的输入...
怎么判断年份是否为闰年....要求从控制台输入,当输入不为正整数时,提示请输入错误,请输入正整数,然后能继续从控制台输入,知道输入为正整数为止...
...全文
506 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
ningmenggogo 2012-06-06
  • 打赏
  • 举报
回复
this.buf = new BufferedReader(new InputStreamReader(System.in));
和Scanner sc = new Scanner(System.in);
的区别是??
龙腾冰 2011-11-16
  • 打赏
  • 举报
回复
for example

package Char12;

import java.io.*;
import java.text.*;
import java.util.*;

class InputData{
private BufferedReader buf = null;
public int count =1;
public InputData(){
this.buf = new BufferedReader(new InputStreamReader(System.in));
}
public String getInfo(){
String temp = null;
if(this.count>1){
this.count =1;
System.out.print("请你输入第"+this.count+++"个日期:");
}else{
System.out.print("请你输入第"+this.count+++"个数字:");
}
try {
temp = this.buf.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return temp;
}
public int getInt(){
int temp = 0;
String str = null;
boolean flag = true;
while(flag){
str = this.getInfo();
if(str.matches("^\\d+{1}quot;)){
temp = Integer.parseInt(str);
flag = false;
}else{
System.out.println("输入的数据必须是数据,请重新输入!");
this.count = count-1;
}
}
return temp;
}
public Date getDate(){
Date d = null;
boolean flag = true;
String str = null;
while(flag){
str = this.getInfo();
if(str.matches("^\\d{4}-\\d{2}-\\d{2}{1}quot;)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
d = sdf.parse(str);
} catch (ParseException e) {
e.printStackTrace();
}
flag = false;
}else{
System.out.println("输入的数据必须是日期,请重新输入!");
}
}
return d;
}
};
public class BufferedReaderDemo02 {
public static void main(String args[]){
int i = 0 ;
int j = 0 ;
InputData input = new InputData();
i = input.getInt();
j = input.getInt();
System.out.println(i+"+"+j+"="+(i+j));
Date d = null;
d = input.getDate();
DateFormat sdf = DateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.ERA_FIELD,new Locale("zh","Cn"));
System.out.println(sdf.format(d));
}
}
qybao 2011-11-16
  • 打赏
  • 举报
回复
for example

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("输入错误,请重输");
}
}
}
}
小绵羊 2011-11-16
  • 打赏
  • 举报
回复
while(读入的数不是整数){
//应该是继续读
}
小绵羊 2011-11-16
  • 打赏
  • 举报
回复
while(读入的数不是整数){
//执行你的逻辑
}
旷逍遥 2011-11-16
  • 打赏
  • 举报
回复
BufferedReader br=new XX(system.in) ;
br.readline()
simimasaiya 2011-11-16
  • 打赏
  • 举报
回复
楼主,这些问题,把任何一本java教材的前两章看看都能写,除非楼主没有任何基础

62,615

社区成员

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

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