谁能帮我解答java这个程序为什么n=5?
来源:百度知道 编辑:UC知道 时间:2024/06/01 11:43:31
public class CallbyValuePri {
void half(int n){
n=n/2;
System.out.println("half方法n="+n);
}
public static void main(String ages[]){
CallbyValuePri cb=new CallbyValuePri();
int m=10;
System.out.println("Before the Invocation,m="+m);
cb.half(m);
System.out.println("After the Invocation,m="+m);
请帮我注释一下 我是新手···
void half(int n){
n=n/2;
System.out.println("half方法n="+n);
}
public static void main(String ages[]){
CallbyValuePri cb=new CallbyValuePri();
int m=10;
System.out.println("Before the Invocation,m="+m);
cb.half(m);
System.out.println("After the Invocation,m="+m);
请帮我注释一下 我是新手···
这个问题很简单,传值问题
public class CallbyValuePri {
//half方法,接收一个int型的参数,传进来的是10
void half(int n){
// 10/2=5;
n=n/2;
//打印n的值,为5
System.out.println("half方法n="+n);
}
public static void main(String ages[]){
//实例化CallbyValuePri类
CallbyValuePri cb=new CallbyValuePri();
//声明一个int型的m并赋值10
int m=10;
//打印 m,结果为10
System.out.println("Before the Invocation,m="+m);
调用CallbyValuePri 类里的half方法,这时去CallbyValuePri 类里这个方法
cb.half(m);
//这里继续打印m的值为10
System.out.println("After the Invocation,m="+m);
}
这不废话嘛,你传10进去,10/2当然等于5了哦