java中的数组添加?

java中的数组添加?,java,arrays,addition,variance,Java,Arrays,Addition,Variance,到目前为止,我有一个程序,要求用户选择一些元素。然后,程序要求用户选择元素数量所指示的次数。如何仅使用数组和while或for循环来查找这组数字的方差。没有什么比我在高中时更花哨的了。以下是我目前的代码: //write an app that finds the variance of a set of numbers class temp1 { public static void main(String args[]) { int counter = 0;

到目前为止,我有一个程序,要求用户选择一些元素。然后,程序要求用户选择元素数量所指示的次数。如何仅使用数组和while或for循环来查找这组数字的方差。没有什么比我在高中时更花哨的了。以下是我目前的代码:

//write an app that finds the variance of a set of numbers
class temp1 {
    public static void main(String args[])
    {
    int counter = 0;
    String question;
    question = "How many elements do you want?: ";
    EasyReader console = new EasyReader();
    System.out.println(question);
    int answer;
    int answer2;
    answer = console.readInt();
    int[] numbers = new int[answer];
    int mean;
    System.out.println();
    while(true)
    {
    System.out.println("Please enter a number: ");
    answer2 = console.readInt();
    counter++;
    if(counter==answer)
    {
    break;
    }
    }
    mean = (numbers[0]+numbers[1]+numbers[2]+numbers[answer])/answer;
    System.out.print(mean);
    }
    }

您的程序可能类似于:

//编写一个应用程序,查找一组数字的方差

class temp1
{
    public static void main(String args[])
    {
        int counter = 0;
        String question;
        question = "How many elements do you want?: ";
        EasyReader console = new EasyReader();
        System.out.println(question);
        int answer;
        int answer2;
        answer = console.readInt();
        int[] numbers = new int[answer];
        int mean;
        System.out.println();
        while (true)
        {
            System.out.println("Please enter a number: ");
            answer2 = console.readInt();
            numbers[counter] = answer2;
            counter++;
            if (counter == answer)
            {
                break;
            }
        }
        counter = 0;
        int sum = 0;
        while(true)
        {
            sum = sum + numbers[counter];
            counter++;
            if(counter == answer)
            {
                break;
            }
        }
        mean = sum / answer;
        System.out.print(mean);
    }
}

您可以使用循环在元素上迭代并计算它。检查你关于如何使用循环和如何计算方差的笔记,然后用Java编程。我知道如何找到方差,只是我不懂数组。我不知道如何添加数组的内容。我可以用一个循环吗?通过。不客气。一个建议,Stackoverflow不喜欢代码请求。在询问代码时,我们希望您包括尝试的解决方案、为什么它们不起作用以及预期的结果。哦,对不起。我没有意识到。我应该在描述中发布我的当前代码等吗?谢谢。扫描仪命令与EasyReader命令相同吗?我不知道任何EasyReader,扫描仪是由Java提供的,EasyReader类可能由您的导师提供给您。非常感谢。这帮了大忙。