永发信息网

JAVA中少了一个对象帮忙改下谢谢了

答案:4  悬赏:0  手机版
解决时间 2021-04-21 06:10

public class hello
{

int x = 20;
public void add(int s, int d)

{
System.out.println("这是第一个add方法");
int a = s + d;
System.out.println("运算结果是 "+a);
}


public void add(String s, String d)
{
System.out.println("这是第二个add方法");
String w = s + d;
System.out.println("运算结果是 "+w);
}

public static void main(String[] args)
{
int s = 10;
int d = 20;
String s1 = "ss";
String s2 = "dd";

}

}

最佳答案

public class hello
{


int x = 20;
public void add(int s, int d)


{
System.out.println("这是第一个add方法");
int a = s + d;
System.out.println("运算结果是 "+a);
}



public void add(String s, String d)
{
System.out.println("这是第二个add方法");
String w = s + d;
System.out.println("运算结果是 "+w);
}


public static void main(String[] args)
{
int s = 10;
int d = 20;
String s1 = "ss";
String s2 = "dd";
hello hh = new hello();
hh.add(s, d);
hh.add(s1, s2);


}


}

全部回答

你这个程序中所写的方法没在main方法中调用,这样就不会有输出结果,应在main方法中加上: hello he = new hello(); he.add(s,d); he.add(s1,s2);

这样你的程序就能看到运行结果了,还有你的变量X也没使用过,不知道你定义它有什么用了!

public class hello {

int x = 20;

public void add(int s, int d)

{ System.out.println("这是第一个add方法"); int a = s + d; System.out.println("运算结果是 " + a); }

public void add(String s, String d) { System.out.println("这是第二个add方法"); String w = s + d; System.out.println("运算结果是 " + w); }

public static void main(String[] args) {

hello hi = new hello(); int s = 10; int d = 20; String s1 = "ss"; String s2 = "dd";

hi.add(s, d); hi.add(s1, s2); } }

看是不是要这个,上面的粗体部分

或者可以改写为:

class hello {

int x = 20;

public void add(int s, int d)

{ System.out.println("这是第一个add方法"); int a = s + d; System.out.println("运算结果是 " + a); }

public void add(String s, String d) { System.out.println("这是第二个add方法"); String w = s + d; System.out.println("运算结果是 " + w); } }

public class helloTest { public static void main(String[] args) {

int s = 10; int d = 20; String s1 = "ss"; String s2 = "dd";

hello hi = new hello(); hi.add(s, d); hi.add(s1, s2); }

}

少什么对象,把两个add方法调用一次不就完了吗?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
工程造价专业工作能拿多少工资
亭字的部首是什么,亭的部首是什么?
让空间变成黑色
宿舍设计创意
湖州东吴银泰城C馆我想知道这个在什么地方
赞美支教志愿的诗歌,赞场老师支教的诗句
魔兽争霸版本问题
合肥哪里好玩?
地下城勇士怎麽升级快?多少级点亮?
我想收只流浪猫,那里能收到?
where IS my mouth?
男的怎么跟男的结婚???
澄海3c...
紫荆花园西区地址在哪,我要去那里办事
二手车需要交税吗,机动车过户需要交税吗
推荐资讯
怎样添加自己为QQ好友
求一个SS的,整合包!(PVE和PVP斗能用)顺便
争什么斗艳?
诺基亚N79饰物孔在哪里
收款人用英语怎么说,收款日期用英语怎么说
大漠奇石国书艺术馆这个地址在什么地方,我要
万轩花园酒店-洗手间在哪里啊,我有事要去这
赞美青山的诗词,关于青山的句子
一名初3的学生做些什么可以赚些钱?
电脑安装视频就蓝屏死机
资产负债表中 一年内到期的非流动资产/负债指
通城县退役士兵培训中心(玉立花园东北)地址有
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?