到目前为止,我有一个程序要求用户选择一些元素。然后程序要求用户选择元素数量所指示的次数。我如何仅使用数组以及 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);
}
}