JAVA编程

编写一个Java应用程序,从键盘读取用户输入两个字符串,并重载3个函数分别实现这两个字符串的拼接、整数相加和浮点数相加。要进行异常处理,对输入的不符合要求的字符串提示给用户,不能使程序崩溃。
public class Strinput
{
public static void main(String args[]) {
String s1,s2,ss,si,sf;
int i1,i2;
float f1,f2;
BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));
try{
System.out.print ("输入第一个字符串:" );
s1=strin.readLine();
System.out.print ("输入第二个字符串:" );
s2=strin.readLine();
}
catch(Exception e){ System.out.println(e.getMessage());}
i1=Integer.parseInt(s1);
i2=Integer.parseInt(s2);
f1=Float.parseFloat(s1);
f2=Float.parseFloat(s2);
ss=strAdd(s1,s2);
si=strAdd(i1,i2);
sf=strAdd(f1,f2);
System.out.println("输入的二个字符串相加结果为:"+ss);
System.out.println("输入字符串转换为整数相加结果为:"+si);
System.out.println("输入字符串转换为浮点数相加结果为:"+sf);
}
String strAdd(String str1,String str2) {
return str1+str2;
}
String strAdd(int int1,int int2) {
return String.valueOf(int1+int2);
}
String strAdd(float flt1,float flt2) {
return String.valueOf (flt1+flt2);
}
}
运行时提示无法从静态上下文中引用非静态方法。
望高手赐教。

这个应该不是在运行时提示, 应该编译器就会提示, 因为你写的三个方法都是非静态的, 而你的main方法 静态的, 所以你无法直接在main方法中调用那个三个字符串处理方法, 你要先实例化一个Strinput的对象然后通过这个对象调用那三个方法
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-05-06
main是静态方法,前面有static关键字。
strAdd是实例方法,属于某个对象的

要想直接调用,在strAdd前面也声明static就可以了

比如
public static String strAdd()本回答被网友采纳
第2个回答  2013-01-01
先实例化 再调用就OK了额
相似回答