简体   繁体   English

如何在方法中为变量设置值并在main方法中将其打印出来?

[英]How do I set a value to a variable in a method and print it out in the main method?

public class Shape 
{
    public static void main(String args[]){
        int num = 0;
        cng(num);
    }

    public static void cng(int x){
        x = 52;
        System.out.println(x);
    }

}

As you can see, in the cng Method I set the value of x to 52 and then print out the value of x. 如您所见,在cng方法中,我将x的值设置为52,然后打印出x的值。

Then, back in the main method, the cng method is performed on the num variable. 然后,回到main方法中,对num变量执行cng方法。

What I want to do, however is set the value of 52 to x without the System.out.println(x); 但是,我想做的是在没有System.out.println(x);情况下将52的值设置为x System.out.println(x); function in my cng method and print out the value in my main method. 功能在我的cng方法并打印出我的价值main方法。 How would I go about doing that? 我将如何去做?

I tried doing 我试着做

public static void cng(int x){
    x = 52;
}

and then 接着

public static void main(String args[]){
    int num = 0;
    cng(num);
    System.out.println(num);
}

but it only prints out a 0 because num is set to 0. I thought that performing cng on the num variable would change it to 52, but it doesn't. 但它只打印出0,因为num设置为0。我认为对num变量执行cng会将其更改为52,但事实并非如此。

make your cng method return an int variable 使您的cng方法返回一个int变量

public static int cng(int num){    
num = 52; 
return num; 
}

In Your Main method, assign the returned variable from cng() method 在您的Main方法中,分配从cng()方法返回的变量

int num = 0;
 num =cng(num);
 System.out.println(num);

Or: 要么:

you could always, make num as a member static variable, 您总是可以将num设为成员静态变量,

   static int num;

You want to pass the data by reference. 您想通过引用传递数据。 This is not possible for primitive values in Java (int, double, boolean, etc). 对于Java中的原始值(int,double,boolean等),这是不可能的。

You have the following options: 您有以下选择:

  • Make it a member of the class 使其成为班级成员
  • Create a wrapper object (object references are also passed by value, but you can change the members of that object in a function, just not the object reference itself) 创建一个包装对象(对象引用也按值传递, 但是可以在函数中更改该对象的成员, 不能更改对象引用本身)
  • Return the value in the function (as mentioned by other answers) 返回函数中的值(如其他答案所述)

Try to change your code to this: 尝试将您的代码更改为此:

public class Shape 
{


public static void main(String args[]){
    int num = 0;
    num = cng(num);
    System.out.println(num);
}

public static int cng(int x){
    x = 52;
    return x;
}

}

read along the comments : 阅读评论:

public class Shape 
{

    public static void main(String args[]){
    int num = 0;
    num = cng();   //store value returned by cng() in num
    System.out.println("num : " +num); // display num
    }

    public static int cng(){    //change return type to int
    return 52;
    }

}

The reason for this behaviour is that arguments are passed by value in Java. 出现这种现象的原因是,参数在Java中按值传递。 This implies that it is simply a copy of the variable that you passed to it. 这意味着它只是您传递给它的变量的副本。 Thus the assignment that you do is only local to the method. 因此,您所做的分配仅在方法本地。

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

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