我没有在构造函数程序中得到输出,该程序没有错误,但结果显示为零
问题描述:
任何人都可以帮助我...如果您发现我的错误,请通知我.并给我解决方案.
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=10
和y=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