-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIO.java
67 lines (59 loc) · 1.62 KB
/
IO.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package 实验一;
import java.util.*;
import java.io.*;
public class IO {
public static void main(String[] args) throws IOException
{
//way1:use Scanner to input
Scanner input=new Scanner(System.in);
System.out.print("输入一个字符串和数字:");
String a=input.next();
int number=input.nextInt();
//way2:use BufferedReader and System.in to input
System.out.print("输入一个字符串和数字:");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String b;
b=br.readLine();
int s;
s =Integer.parseInt(br.readLine());
//two ways to output
System.out.print(a);
System.out.println(number);
System.out.print(b);
System.out.println(s);
// PrintStream out = new PrintStream( new BufferedOutputStream(System.out));
// out.print(s);
//题目二
String s1,s2;
s1=s2=null;
int num1,num2;
System.out.print("Please input two number:");
s1=br.readLine();
s2=br.readLine();
if(s1.length()==0||s2.length()==0)
{
System.out.print("erro!");
return;
} //有空字符串报错
for(int i=0;i<s1.length();i++)
{
if(s1.charAt(i)>'9'||s1.charAt(i)<'0')
{
System.out.print("erro!");
return;
}
}
for(int i=0;i<s2.length();i++)
{
if(s2.charAt(i)>'9'||s2.charAt(i)<'0')
{
System.out.print("erro!");
return;
}
} //只要s1,s2中有非数字的字符就报错退出
num1=Integer.parseInt(s1);
num2=Integer.parseInt(s2); //若没问题,则转为数字
System.out.print("The average of the two numbers is: ");
System.out.print((num1+num2)/2);
}
}