在java中,“int”在方法之前有什么用途?
在下面的代码中,我们可以直接使用“calculate”,但在计算之前“int”有什么用途在java中,“int”在方法之前有什么用途?,java,Java,在下面的代码中,我们可以直接使用“calculate”,但在计算之前“int”有什么用途 class Rectangle{ int length, breadth; void show(int x, int y){ length = x; breadth = y; } int calculate(){ return(length * breadth); } } public class EnterValues
class Rectangle{
int length, breadth;
void show(int x, int y){
length = x;
breadth = y;
}
int calculate(){
return(length * breadth);
}
}
public class EnterValuesFromKeyboard{
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
rectangle.show(a, b);
System.out.println(
" you have entered these values : " + a + " and " + b);
int area = rectangle.calculate();
System.out.println(" area of a rectange is : " + area);
}
}
这说明了方法的返回类型
int calculate(){
return(length * breadth);
}
在本例中,它告诉调用方calculate方法返回一个int值
我建议阅读函数中的第一个关键字指定返回值的类型。那么在
int calculate(){
return(length * breadth);
}
int表示返回的值将是整数。调用calculate方法后,结果将是int类型。这有助于您将该方法用作值。 e、 g.:int a=计算//计算结果将分配给变量a。 xyz=a+calculate+b//calculate的返回值将加入公式
等等。-1这个问题太难了,没有任何实际用处。我建议你买一本Java入门书,通读前几章。而且,你的问题很糟糕,因为你有一堆无用的代码。你能不能把代码裁剪成包含一些相关的东西?事实上我正在学习java入门书,所以有点怀疑Adrian…我不知道为什么会有这些反对票??你对鼓励初学者不感兴趣…@srithatsmyname如果你看一下,你会发现stackoverflow社区对初学者的鼓励非常多,有些人可能会争辩说,太多了。然而,这个问题非常薄弱,包括我在内的许多人认为,你应该通过书籍和在线资源自己解决这个问题,或者至少证明你比表面上更努力了……Tq Nambari ur ans很有帮助,你的建议也很有用。@srithatsmyname:很高兴它有帮助。祝你好运