JavaLearing: JAVA IO BufferedReader

来源:互联网 发布:linux 查看外网ip地址 编辑:程序博客网 时间:2024/04/25 15:31
package org.fun.io;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class BufferedReaderDemo {public static void main(String[] args) throws IOException {BufferedReader br = null;br = new BufferedReader(new InputStreamReader(System.in));System.out.print("input:");String str = br.readLine();System.out.println("msg:" + str);}}

package org.fun.io;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class AddDemo {public static void main(String[] args) throws IOException {BufferedReader buf = null;buf = new BufferedReader(new InputStreamReader(System.in));int i = 0;int j = 0;String str = null;boolean flag = true;while (flag) {System.out.print("first number:");str = buf.readLine();if (str.matches("\\d+")) {i = Integer.parseInt(str);flag = false;} else {System.out.println("input again:");}}flag = true;while (flag) {System.out.print("Second number:");str = buf.readLine();if (str.matches("\\d+")) {j = Integer.parseInt(str);flag=false;} else {System.out.println("input again:");}}System.out.println(i + "+" + j + "=" + (i + j));}}

0 0
原创粉丝点击