Java——键盘输入字符

2019年2月23日18:18:22 发表评论 1,308 views
package filetest;
import java.io.*;
public class bufferdemo {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        BufferedReader buf = null;
        buf = new BufferedReader(new InputStreamReader(System.in));
//对BufferedReader对象实例化,因为现在需要从键盘输入数据,因此需要使用System.in进行
//实例化,但System.in是属于InputStream类型,所以使用InputStreamReader类将字节流转换
//成字符流,之后将字符流放入到BufferedReader中
String str =null; while(true){ System.out.print("请输入数字:");; try{ str = buf.readLine(); } catch(IOException e){ e.printStackTrace(); } int i = -1; try{ i = Integer.parseInt(str); i++; System.out.println("输入的数字修改后为:"+i); break; } catch(Exception e){ System.out.println("输入的内容不正确,请重新输入!"); } } } }

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: