我没有在构造函数程序中得到输出,该程序没有错误,但结果显示为零 [英] i did't get output in constructor program, this program No ERROR but result is display zero

查看:71
本文介绍了我没有在构造函数程序中得到输出,该程序没有错误,但结果显示为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮助我...如果您发现我的错误,请通知我.并给我解决方案.


any one help me... if you find my mistake please inform me. and give me the solution.


class Testconstructor
{
int x,y;

Testconstructor(int a, int b)
{
x=a;
y=b;
}

/**Testconstructor()
{
x=10;
y=20;
}*/

  int areaa()
    {
      int ar= 1/2*x*y;
System.out.println("Area of triangle :"+ar);
    return ar;
    }
}

class dTestconstructor
{
  public static void main(String args[])
{
  int ar;

Testconstructor a1 = new Testconstructor(10,20);

//a1.Testconstructor(10,20);

//ar=a1.areaa();


System.out.println("Area of triangle :"+ a1.areaa());
}
}

推荐答案

int ar= 1/2*x*y;


鉴于x=10y=20,您具有整数表达式1 / 2 * 10 * 20
无论您以哪种方式查看,它都将返回零.


Given that x=10 and y=20 you have the integer expression 1 / 2 * 10 * 20
which will return zero whichever way you look at it.


这甚至不是问题.您没有得到任何输出,因为您没有尝试输出任何东西.

顺便说一句,areaa是输出内容的唯一方法,当然是对自己的犯罪.您返回值,为什么要在此方法中放入任何输出?这称为函数的副作用,通常应避免这种情况.

—SA
This is not even a question. You did get any output because you did not try to output anything.

By the way, your only method where you output something, areaa is simply a crime, against yourself, of course. You return the value, why putting any output into this method? This is called side-effect of the function, something you usually should avoid.

—SA


这篇关于我没有在构造函数程序中得到输出,该程序没有错误,但结果显示为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆