简体   繁体   English

在Java中的方法内部调用另一个方法

[英]Call another method inside the method in Java

I'm doing an exercise about the method and one-dimensional arrays.我正在做一个关于方法和一维 arrays 的练习。 I created an array under a method.我在一个方法下创建了一个数组。 How can I use that array under another method?如何在另一种方法下使用该数组? For example, I determined a few identities in my first method and the second, I will extract some features from those identities using these identities.例如,我在第一种方法中确定了一些身份,第二种方法,我将使用这些身份从这些身份中提取一些特征。 Let's say that the more times "xyz" are written in this code, I will increase their health by 1.假设这段代码中写“xyz”的次数越多,我就会将他们的生命值增加 1。

The problem is that the Array ID's is currently a local variable of your method population问题是数组 ID 当前是您的方法填充的局部变量

Here some important Points about Variable in scope in Java:这里关于 Java 中 scope 中的变量的一些要点:

  • In general, a set of curly brackets { } defines a scope.通常,一组大括号 { } 定义了一个 scope。
  • Any variable defined in a class outside of any method can be used by all member methods.所有成员方法都可以使用在任何方法之外的 class 中定义的任何变量。
  • In Java we can usually access a variable as long as it was defined within the same set of brackets as the code we are writing or within any curly brackets inside of the curly brackets where the variable was defined.在 Java 中,我们通常可以访问一个变量,只要它与我们正在编写的代码在同一组括号内或在定义变量的大括号内的任何大括号内定义。

Scope means that variables are only accessible inside the region they are created. Scope 意味着变量只能在它们创建的区域内访问。

Here an example to show this behavior这是一个显示此行为的示例

public class Main {
  public static void main(String[] args) {

    // Code here CANNOT use x

    { // This is a block

      // Code here CANNOT use x

      int x = 100;

      // Code here CAN use x
      System.out.println(x);

   } // The block ends here

  // Code here CANNOT use x

  }
}

In your case you can define the id's in a "higher scope" outside of your population method.在您的情况下,您可以在填充方法之外的“更高范围”中定义 id。

public class L {
    public static String[] IDs = new String[n];

        public static void main(String[] args) {
            
            
        
}
        public static void population(int n){
            String[] letter = {"x","y","z"};
            for (int j=0; j<n; j++){
                IDs[j] = "";
                for (int i=0; i<128; i++){
                    Random rand = new Random();
                    int w = rand.nextInt(letter.length);
                    IDs[j]+=letter[w];
                }
            }
        }

         private static void calculateHealth(int health) {
            population(health);
            health =0;

            for (int i=2; i<128; i++){
                if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
                    health+=1;
                }
            }
            System.out.println(health);;
        }
}

Then you can use it in both of your methods.然后你可以在你的两种方法中使用它。


Here is another solution for this problem where you don't have to care about scopes.这是此问题的另一种解决方案,您不必关心范围。 You can just pass to each of the functions an array as parameter.您可以将数组作为参数传递给每个函数。

public class L {

        public static void main(String[] args) {
            
            
        
}
        public static void population(String[] IDs, int n){
            String[] letter = {"x","y","z"};
            for (int j=0; j<n; j++){
                IDs[j] = "";
                for (int i=0; i<128; i++){
                    Random rand = new Random();
                    int w = rand.nextInt(letter.length);
                    IDs[j]+=letter[w];
                }
            }
        }

         private static void calculateHealth(String[] IDs, int health) {
            population(health);
            health =0;

            for (int i=2; i<128; i++){
                if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
                    health+=1;
                }
            }
            System.out.println(health);;
        }
}

Just declare the String[] outside of the method declaration;只需在方法声明之外声明String[] setting it's dimension is useless:设置它的尺寸是没用的:

String[] IDs = new String[]{};

And you probably don't need these static keywords either.而且您可能也不需要这些static关键字。

well there are two things 1st - As alex mentioned 2nd you can create array in that function有两件事第一 - 正如亚历克斯提到的第二你可以在 function 中创建数组

public static String[] population(int n){
    String[] IDs = new String[n];
    String[] letter = {"x","y","z"};
    for (int j=0; j<n; j++){
        IDs[j] = "";
        for (int i=0; i<128; i++){
            Random rand = new Random();
            int w = rand.nextInt(letter.length);
            IDs[j]+=letter[w];
        }
    }
    return IDs;
}

 private static void calculateHealth(int health) {
    String[] Ids = population(health);
    health =0;

    for (int i=2; i<128; i++){
        if (IDs[i-2].equals("X") && IDs[i-1].equals("Y") && IDs[i].equals("Z")) {
            health+=1;
        }
    }
    System.out.println(health);;
}

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

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