package test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Test {
public String strAdd(int int1, int int2){
return String.valueOf(int1+int2);
}
public String strAdd(String str1,String str2){
return str1+str2;
}
public String strAdd(float f1,float f2){
return String.valueOf(f1+f2);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1,s2,ss,si,sf;
int i1,i2;
float f1,f2;
Test pp=new Test();
/*
*在一个类中写了一个public String getContent()方法和一个main()方法
*当getContent()方法中包含了getClass()方法,在main()方法中直接调用了getContent()方法时
*就会出现Cannot make a static reference to the non-static method的错误
*解决这个问题的方法是:创建一个实例,用来调用Test类中的几个重构的方法
*/
BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
/*
* 从标准输入流System.in中直接读取使用者输入时,使用者每输入一个字符,System.in就读取一个字符。
* 为了能一次读取一行使用者的输入,使用了BufferedReader来对使用者输入的字符进行缓冲。
* readLine()方法会在读取到使用者的换行字符时,再一次将整行字符串传入。
* System.in是一个位流,为了转换为字符流,可使用InputStreamReader为其进行字符转换,然后再使用BufferedReader为其增加缓冲功能
* 这里用的就是这种使用InputStreamReader的方法。
*/
try{
System.out.println("input the first string:");
s1=strin.readLine();
System.out.println("input the second string:");
s2=strin.readLine();
i1=Integer.parseInt(s1);
i2=Integer.parseInt(s2);
//使用Integer类中的一个方法parseInt(),用来实现从字符串到十进制整型数据的解析
f1=Float.parseFloat(s1);
f2=Float.parseFloat(s2);
//使用Float类中的一个方法parseFloat(),用来实现字符串到浮点型数据的解析
ss=pp.strAdd(s1,s2);
si=pp.strAdd(i1,i2);
sf=pp.strAdd(f1,f2);
//这里就是使用类的方法调用来完成,防止报错
System.out.println(ss);
System.out.println(si);
System.out.println(sf);
}catch(Exception e){
System.out.println(e.getMessage());
}
}
}
关于BufferedReader的理解
最新推荐文章于 2024-02-28 13:25:00 发布