Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.9k views
in Technique[技术] by (71.8m points)

javascript - Why can't I access my int array values that I made in my main method from another method? (beginner Java coder)

I am making a program that is simple for my school project, I am asking for a list then return either a max, min, or average. I make an array in the class then edit the values in my main method, I am trying to access the new values from my other methods but cant, IDK why.

import java.util.Scanner; import java.util.Arrays;

public class numbersAreWeird {

    private static double[] thing;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        
        print("Hello");
        print("What is the length of your set of numbers?");
        int whyEven = in.nextInt();
        
        thing = new double[whyEven];
        
        for(int i = 0; i < thing.length; i++)
        {
            System.out.println("What do you want to place for " + i  + " ?");
            whyEven = in.nextInt();
            thing [i] = whyEven;
        }
             
        print("This is your arrray " + Arrays.toString(thing));
        
        print("Do you want to [1] Find the max [2] the min");
        print(" [3] find the average");
        int tree = in.nextInt();
        
        if(tree == 1)
        {
            getMax();
        }
        if(tree == 2)
        {
            getMin();
        }
        if(tree == 3)
        {
            getAve();
        }
            
    }

public static String getMax() {

        Double max = Double.MIN_VALUE;
        
        for(int i = 0; i < thing.length; i++)
        {
            if(max<thing[i])
                max = thing[i];
        }
        
        return ("The Max value of the list is ")+ max;
    }
    
    public static String getMin()
    {
        Double min = Double.MAX_VALUE;
        
        for(int i = 0; i < thing.length; i++)
        {
            if(min>thing[i])
                min = thing[i];
        }
        
        return ("The Min value of the list is ") + min;
    }
    
    public static String getAve()
    {
        int total = 0;
        int count = 0;
        
        for (int i = 0; i < thing.length; i++)
        {
            total += thing[i];
            count++;
        }
        
        return ("The average of the list is ") + total/count;
    }
        
    public static void print(String str)
    {
        System.out.println(str);
    }

}// end of class


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you intended to print rather than return, or rather, forgot to print the returned value.

You should be printing the returned value (which involves less of a fix up with your code) like so:

if (tree == 1) {
    print(getMax());
}else if (tree == 2) {
    print(getMin());
}else if (tree == 3) {
    print(getAve());
}

Also, I replaced the 3 if statements with 1 if and 2 else if statements.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...