![](/img/trans.png)
[英]I am trying to create a method that calculates the averages of numbers in two arrays
[英]I am trying to create a program that averages numbers, but cannot figure out how to add the user inputted numbers together
我是一名学习Java的AP计算机科学专业的学生。 在上这门课程之前,我学习过JavaScript,但是在弄清楚如何完成此代码时遇到了麻烦。 我的老师正在休假,我们的分校不是程序员。 感谢您的帮助。 我的老师希望我们发表评论以解释一切。 这是我的代码:
package com.company;
//导入扫描器类和所需的任何其他可导入类
导入java.util。*;
公共班级主要{
public static void main(String[] args) {
//Asks a question and lets the user input an answer, saved as the int Dividend
System.out.println("How many numbers do you want to average?");
Scanner dividend = new Scanner(System.in);
int Dividend = dividend.nextInt();
//this is a variable set in order to make the while statement work for any number
int change = 0;
//Asks for the numbers that should be averaged
System.out.println("Enter the numbers to find their average: ");
/*This is saying that while change is less than the amount of numbers being averaged,
continue to provide an input for numbers*/
while (change <= Dividend) {
int Num = 0;
int nums = 0;
Scanner num = new Scanner(System.in);
//I am trying to add the input to num so I can average them
nums = num.nextInt();
Num += nums;
/*this is making sure that change is letting the code run through the exact amount of necessary times
in order to input the correct amount of numbers*/
change++;
//once all of the numbers have been put in, average them
if (change == Dividend) {
System.out.println("Average: " + Num / Dividend);
}
System.out.println(Num);
}
}
}
我修复了您的问题,因此它现在添加了所有数字,但仍不确定我添加了nums = nums + num.nextInt();
那一半nums = nums + num.nextInt();
因此它将总计所有数字
如果您想在自己的下方找到可用的参考,我也创建了解决方案
public static void main(String[] args){
int change = 0;//Total Numbers It think
int nums = 0;//Sum
System.out.println("Enter the numbers to find their average: ");
Scanner num = new Scanner(System.in);//Should be moved out of loop so its not initialized every time
int Dividend=1;//I had to declare to run it
while (change <= Dividend) {//not sure why your comparing these
nums = nums + num.nextInt();//Total Sum
change++;//Total nums
if (change == Dividend) {
System.out.println("Average: " + nums / Dividend);
}
System.out.println(nums);
}
}
这是我会做的
public static void main(String[] args){
int sum = 0;
int countOfNumbers = 0;//Tthe same as your change im guessing
System.out.println("Enter the numbers to find their average type stop to quit: ");//Added stop statement
Scanner scanner = new Scanner(System.in);//Should be moved out of loop so its not initialized everytime
while (scanner.hasNext()){//Ensures there is something to check
String next = scanner.next(); //Pull next Value
if(next.equalsIgnoreCase("Stop"))//This will prevent looping forever
break;
sum = sum + Integer.valueOf(next); //Get total sum
countOfNumbers++; //Increment total numbers
System.out.println("The Sum is:"+sum
+"\nThe count of Numbers averaged is:"+countOfNumbers
+"\nThe Average is:"+sum/countOfNumbers);//Print info
}
System.out.println("Done Averaging Goodbye");
}
您可以使用ArrayList
来存储所有用户输入,然后在其间进行迭代以平均它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.