简体   繁体   English

为什么该方法的值不会改变?

[英]Why does the value not change by the method?

I am a new java self-learning.我是新java自学。 I'm try to across a practice question (couldn't find the answer), but I ran into the following problem: the passenger cannot be changed following the station change.我试图穿过一个练习题(找不到答案),但我遇到了以下问题:换站后无法更换乘客。

public class BusGame {
    
    static int station, passenger;

    public static void main(String[] args) {
        station = 0;
        passenger = 0;
        
        for(station = 0; station <=5; station++) {
            System.out.println("Station\tPassenger");
            passengerChange(station,passenger);
            System.out.println(station + "\t" + passenger);
        }
    }
    
    public static int passengerChange(int s,int p) {
        if(s==1 || s==5) {
            p++;
        }
        if(s==2 || s==3) {
            p=p+2-1;
        }
        if(s==4) {
            p--;
        }
        return p;
    }
}

and the output is: output 是:

Station Passenger
0       0
Station Passenger
1       0
Station Passenger
2       0
Station Passenger
3       0
Station Passenger
4       0
Station Passenger
5       0

Can someone tell me why the "passenger" value cannot be changed but the "station" value can changed?有人能告诉我为什么“乘客”的值不能改变,但“车站”的值可以改变吗?

You are not using the value returned from passengerChange .您没有使用从passengerChange返回的值。 Try尝试

System.out.println(station + "\t" + passengerChange(station,passenger));

You should try this.passenger++ instead of p++.你应该试试 this.passenger++ 而不是 p++。
p is a new integer value created inside passengerChange. p 是在passengerChange 中创建的新integer 值。

if you used else in the static function you could even completely remove p and s.如果您在 static function 中使用 else,您甚至可以完全删除 p 和 s。

my idea whould look like thi:我的想法应该是这样的:

public class BusGame {

static int station, passenger;

public static void main(String[] args) {
    station = 0;
    passenger = 0;
    
    for(station = 0; station <=5; station++) {
        System.out.println("Station\tPassenger");
        passengerChange();
        System.out.println(station + "\t" + passenger);
    }
}

public static int passengerChange() {
    if(this.station==1 || this.station==5) {
        this.passenger++;
    }else{
        if(this.station==2 || this.station==3) {
            this.passenger=this.passenger+2-1;
        }else{
            if(this.station==4) {
                this.passenger--;
    }}}
    return p;
}

} }

I feel like there are a few concept you will benefit from knowing.我觉得有一些概念你会从了解中受益。 Those are:那些是:

  1. Primitive datatype and Wrapper class In object-oriented programming, a wrapper class is a class that encapsulates types, so that those types can be used to create object instances and methods in another class that need those types. Primitive datatype and Wrapper class In object-oriented programming, a wrapper class is a class that encapsulates types, so that those types can be used to create object instances and methods in another class that need those types.

So that means that primitive datatypes are like int a =5;这意味着原始数据类型就像int a =5; whereas using wrapper classes I actually create an object of class Integer .而使用包装类我实际上创建了一个 object 的 class Integer

This is important to know because we can now learn:知道这一点很重要,因为我们现在可以学习:

  1. Call by value vs call by reference Java is always Pass by Value and not pass by reference.按值调用与按引用调用 Java 始终是按值传递而不是按引用传递。

Call by value: Call by Value means calling a method with a parameter as value.按值调用:按值调用是指调用以参数为值的方法。 Through this, the argument value is passed to the parameter.通过这个,参数值被传递给参数。

Call by reference: While Call by Reference means calling a method with a parameter as a reference.引用调用:而引用调用是指调用带有参数作为引用的方法。 Through this, the argument reference is passed to the parameter.通过这个,参数引用被传递给参数。


In case of call by value original value is not changed.在按值调用的情况下,原始值不会更改。 Note: numbers below are line numbers for reference注:以下数字为行号供参考

1. class Operation{  
2.  int data=50;  
  
3.  void change(int num){  
4.  num=data+100;//changes will be in the local variable only  
 }  
     
5. public static void main(String args[]){  
6.   Operation op=new Operation();  
  
7.   System.out.println("before change "+op.data);  
8.   op.change(500);  
9.   System.out.println("after change "+op.data);  
  
 }  
} 

Output will be: Output 将是:

Output:before change 50
       after change 50  

This is because num is int which is primitive.这是因为 num 是原始的 int。 So when I pass 500 in line 8, and the method in line 3 receives it, changes it and assigns it it remains in that function only!因此,当我在第 8 行传递 500 并且第 3 行中的方法接收到它、更改它并分配它时,它只保留在 function 中! (Search about this more) (搜索更多)

If num was an object of a wrapper class then things would have been different.如果 num 是包装器 class 的 object ,那么情况会有所不同。 I would suggest you first learn these concepts then I will teach you what happens if num was Integer.我建议你先学习这些概念,然后我会教你如果 num 是 Integer 会发生什么。

  1. keyword Return关键字Return

A return statement causes the program control to transfer back to the caller of a method. return 语句使程序控制权转移回方法的调用者。

When a method returns anything, the control is transferred back to the statement where the method was call.当一个方法返回任何东西时,控制权被转移回调用该方法的语句。 We need to have a variable to catch the value returned.我们需要一个变量来捕获返回的值。 So your passengerChange(station,passenger);所以你的passengerChange(station,passenger); should have been:本来应该:

passenger = passengerChange(station,passenger);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM