[英]Array Addition in java?
I have so far a program that asks the user to choose a number of elements. 到目前为止,我有一个程序要求用户选择一些元素。 The program then asks the user to choose numbers as many times indicated by the number of elements.
然后,程序要求用户选择元素数量所指示的次数。 How would I find the variance of this set of numbers using only arrays and either while or for loops.
我将如何仅使用数组以及while或for循环来找到这组数字的方差。 Nothing too fancy as I am in high school.
没有什么比我在读高中时那么花哨了。 Below is my current code:
下面是我当前的代码:
//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);
}
}
Your program may be like: 您的程序可能像:
//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();
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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.