JAVAIO中BufferedReader的应用1

import java.io.*;
import java.util.regex.*;
class CheckDigit
{
	public boolean check(String str)
	{
		if(str.matches("\\d+"))
		{
			return true;
		}
		else
		{	
			System.err.println("必须是数字!请重新输入");
			return false;
		}
	}
}
class InputData
{
	BufferedReader br =null;
	String success=null;
	String tips=null;
	String str = null;
	public InputData(String tips,String success)
	{
		br=new BufferedReader(new InputStreamReader(System.in));
		this.success=success;
		this.tips=tips;
		
	}
	public String getStr()
	{
		System.out.println(tips);
		try{
			str=br.readLine();
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return str;
	}
	public Integer getInteger()
	{
		System.out.println(success);
		return Integer.parseInt(str);
	}
}
public class override
{
	public static void main(String args[])
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int i=0;
		int j=0;
		boolean flag=true;
		CheckDigit cd = new CheckDigit();
		InputData id1 = new InputData("请输入第一个数字","输入成功!");
		while(flag)
		{
			if(cd.check(id1.getStr()))
			{
				i=id1.getInteger();
				flag =false;
			}
		}
		InputData id2 = new InputData("请输入第二个数字","输入成功!");
		if(cd.check(id2.getStr()))
		{
			j=id2.getInteger();
		}
		System.out.println(i+"+"+j+"="+(i+j));
		
	}
}

要求:通过键盘输入数据完成加法操作

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值