简体   繁体   English

为什么我不能从另一个方法访问我在 main 方法中创建的 int 数组值? (初学者Java编码器)

[英]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.我在 class 中创建了一个数组,然后在我的主要方法中编辑值,我试图从我的其他方法中访问新值,但不能,IDK 为什么。

import java.util.Scanner;导入 java.util.Scanner; import java.util.Arrays;导入 java.util.Arrays;

public class numbersAreWeird {公共 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() {公共 static 字符串 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 }// class 结束

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.另外,我用 1 个if和 2 个else if语句替换了 3 个if语句。

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

相关问题 为什么我不能从 object 方法访问我的自定义字符串原型? - Why can't I access my custom string prototype from an object method? 为什么我不能在另一台计算机上访问我的API? - Why can't I access my API on another computer? 为什么我不能从hamsters.js内部运行我的方法? - Why can't I run my method from inside hamsters.js? 在创作 jquery 插件时,如何从另一个方法调用我的一个主要插件方法? - When authoring a jquery plugin, how do I call one of my main plugin methods from another method? 我无法在我的方法中访问我的 useState 挂钩的初始 state 以更改 state - I can't access the initial state of my useState hook inside of my method to change the state 为什么我不能从我的数组中抓取特定的对象? - Why can't I grab a specific objects from my array? 为什么我不能用用户的URL填充数组? - Why can't I populate my array with the URL from the user? 为什么不能在模块中调用构造函数方法? - Why can't I call my constructor method within my module? 创建我自己的 array.prototype.map 方法。 我怎样才能访问数组? - Creating my own array.prototype.map method. How can I access the array? 为什么不能从同一类中的另一个方法调用此方法? - Why can I not call this method from another method in the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM