开发者

How do you make sure Java finds your variable across methods?

开发者 https://www.devze.com 2023-04-12 08:13 出处:网络
I am trying to write a Java class where the main method creates an array to store 30 integers. Afterwards, I call a method called LOAD() in the main method, whose job is to populate the array with 30

I am trying to write a Java class where the main method creates an array to store 30 integers. Afterwards, I call a method called LOAD() in the main method, whose job is to populate the array with 30 integers from 1-300.

I have written what I think is a complete Java class to do this, yet the compiler keeps telling me this error of that it cannot find the symbol for symbol randomThirty, my array variable's name.

Here is my code so far, yet I am not sure why my randomThirty is not be开发者_开发知识库ing picked up by LOAD(), perhaps I need to pass the parameters explicitly in the parens of LOAD?

import java.util.*;

public class arrayNums
{

public static void main(String args[])
{
     //main method which creates an array to store 30 integers

    int[] randomThirty = new int[30]; //declare and set the array randomThirty to have 30 elements as int type

    System.out.println("Here are 30 random numbers: " + randomThirty);

LOAD(); // the job of this method is to populate this array with 30 integers in the range of 1 through 300.
}


public static void LOAD()
{
    // looping through to assign random values from 1 - 300
    for (int i = 0; i < randomThirty.length(); i++) {
        randomThirty[i] = (int)(Math.random() * 301); 
}


The clue is in your comment:

LOAD(); // the job of this method is to populate this array

So you've got an array that you want the method to work with... you should pass it to the method:

load(randomThirty);

// Method declaration change...
public static void load(int[] arrayToFill)

That's a good approach when a method isn't naturally in the context where it has access to state. In this case is looks like the appropriate approach. For other situations, you could use a static or instance variable, depending on the context.

(You should look at the Random class, by the way - and learn about Java naming conventions.)


you need to pass randomThirty into your load method, or make it a static variable on the class (i.e. move it out of the method).

So you could do

LOAD(randomThirty); // pass the reference in

and change your method definition to

public static void LOAD(int[] randomThirty) {...}

or you can make randomThirty a static property on the class

public class arrayNums {
    // will be available anywhere in the class; you don't need to pass it around
    private static int [] RANDOM_THIRTY = new int[30]; // instantiate here
    ...

    public static void main(String args[]) { ... } // calls load
    ...
    public static void load(){...}
    ...
}

as a note, java conventions are that class names are like ArrayNums and method names should be like load or loadNums.


you need to pass your array as a param , cuz it's out of the load method's scope .

LOAD(randomThirty);




public static void LOAD(int[] randomThirty)
    {
        // looping through to assign random values from 1 - 300
        for (int i = 0; i < randomThirty.length(); i++) {
            randomThirty[i] = (int)(Math.random() * 301); 
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号