程序要求用户输入要平均的数量
打印输入的数字
一行上有5个数字(使用打印件)
这是我需要做的,但我不认为我是正确的,因为我是java的新手 这就是我到目前为止所做的:
import java.util.Scanner;
public class AnyAverageArr {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the max number:");
int max = input.nextInt();
int[] arr1 = new int[max];
for (int i = 0; 1<= max; i++) {
arr1[i] = input.nextInt();
}
System.out.println("Average of All is " + arr1[max]);
}
}
答案 0 :(得分:0)
你一定要读一下java中的for循环。 https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html
您还应该阅读平均值的含义。 https://www.reference.com/math/average-mean-mathematics-b2fb5e4b69989ed8
以下是对代码的简单快速修复
Scanner input = new Scanner(System.in);
System.out.print("Please enter the max number:");
int max = input.nextInt();
int sum = 0;
for (int i = 0; i< max; i++) {
sum += input.nextInt();
}
System.out.println("Average of All is " + sum / max);
答案 1 :(得分:0)
以下是对您的代码的修复:
Scanner input = new Scanner(System.in);
System.out.print("Please enter the max number:");
int numberOfElements = input.nextInt();
int[] arr = new int[numberOfElements];
int sum = 0;
for (int i = 0; i< numberOfElements; i++) {
arr[i] = input.nextInt();
sum += arr[i];
}
System.out.println("Average of All is " + sum / numberOfElements);
我使用相同的循环来保存数组中的元素并同时计算总和。
希望这能解决您的问题
答案 2 :(得分:0)
我了解您的要求,并且我的源代码更新了一点。 我相信你可以遵循以下几点。
请准确参考以下源代码。
public class StackOverflow {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the max number:");
int max = input.nextInt();
int[] arr1 = new int[max];
for (int i = 0; i < max; i++) {
arr1[i] = input.nextInt();
}
int sum = 0;
for(int num : arr1){
sum = sum + num;
}
double avg = sum/max;
System.out.println(" Avarage : " + avg);
}
}
答案 3 :(得分:0)
如果我没弄错的话,这就是你所需要的(解释见评论):
try (Scanner input = new Scanner(System.in)) {
System.out.print("Please enter the max number:");
int max = input.nextInt();
int[] arr1 = new int[max];
for (int i = 0; i < max; i++) {
arr1[i] = input.nextInt();
}
for (int i = 1; i <= max; i++) {
System.out.print(arr1[i - 1] + " ");
if (i % 5 == 0) {
System.out.println();
}
}
System.out.println();
double sum = 0.0;
for (int i = 0; i < max; i++) {
sum += arr1[i];
}
System.out.println("Average: " + (sum / max));
} catch (Exception e) {
e.printStackTrace();
}
答案 4 :(得分:0)
import java.util.Scanner;
//here is your edited code
public class AnyAverageArr {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Please enter the max number:");
int max = input.nextInt();
int[] arr1 = new int[max];
//since array starts from 0, we need to loop less than max
//i< max : so loop will terminate when we will have input equals to max numbers
for (int i = 0; i< max; i++) {
arr1[i] = input.nextInt();
}
int sum=0;
//loop again through array and calculate the sum of each number
for(int v:arr1) {
sum+=v;
}
//divide sum by total number of inputs to get the answer
System.out.println("Average of All is " + sum/max);
}
}
------------------------------------------------------------------------
答案 5 :(得分:0)
您可以尝试以下代码:
public class StackOverflow {
public static void main(String[] args) {
try (Scanner input = new Scanner(System.in)) {
System.out.print("Please enter the max number: ");
int max = input.nextInt();
// Creates a new array of the size as amount of numbers (max)
int[] arr1 = new int[max];
double sum = 0.0;
// It stores each number in an array entry
for (int i = 0; i < max; i++) {
arr1[i] = input.nextInt();
sum += arr1[i];
}
// Prints the numbers entered
// 5 numbers on a line (use print)
// Put a println appropriately (use % check)
for (int i = 0; i < max; i++) {
System.out.print(arr1[i] + " ");
}
// Print the average of all the numbers
System.out.println();
System.out.println("Average of all is " + sum / max);
}
}
}