如何计算数组中的元素数量?
因此数组可以容纳10个元素,但并非所有元素都被填充。 如何计算输入的元素数量?
另外,找到数组平均值的正确方法是什么? 我现在拥有的就是这个,
输入的有效成绩数为10 [D @ 7ea987ac [D @ 7ea987acAverage:0.0
//validate strings entered by user
if(convertedInput >= 0 && convertedInput <=100){
ValidArray[validArraycount] = convertedInput;
validArraycount ++;
}
}
catch(NumberFormatException e){
}
//Prints number of valid values entered
System.out.println("The number of valid grades entered is " + vArrayLen);
//for printing array backwards
for (int i = (arraycount-1); i>=0; i--){
System.out.print(ValidArray);
}
//calculates sum of all values in array of ValidArray (of grades)
for(double d : ValidArray){
sum += d;
}
//avergae of valid number array
double average = (sum/ValidArray.length);
System.out.println("Average: " + average);
}
}
谢谢。
编辑: 输入最多为10个字符串。任何东西都可以输入。有效字符串是0-10之间的双数,可以是0和10.所有字符串无效都将被丢弃。 截至目前,这是我的代码:
import java.util.*;
public class Grades{
public static void main(String args[]){
int arraycount = 0;
final int SIZE = 100;
int validArrayCount = 0;
final int ValidArraySize = 100;
int valuesinValidArray = 0;
Scanner reader = new Scanner(System.in);
String initialInput = new String ("");
String [] sArray = new String[SIZE];
double [] ValidArray = new double[ValidArraySize];
double sum = 0;
boolean exit = false;
System.out.println("You may enter up to 100 grades.");
System.out.println("When you are done entering grades, press the enter/return key.");
//Prints to user. Stops if nothing is entered.
while((arraycount < SIZE)&&(exit == false)){
System.out.println("Enter line " + (arraycount+1) + ": ");
initialInput = reader.nextLine();
if (initialInput.length()<1){
exit = true;
}
else{
sArray[arraycount]=initialInput;
arraycount++;
}
}
//convert string to double
try{
double convertedInput = Double.parseDouble(initialInput);
//validate strings entered by user
if(convertedInput >= 0 && convertedInput <=100){
ValidArray[validArrayCount] = convertedInput;
validArrayCount ++;
}
}
catch(NumberFormatException e){
}
//Prints number of valid values entered
System.out.println("The number of valid grades entered is " + vArrayLen);
//for printing array backwards
for (int i = (ValidArraySize-1); i>=0; i--){
System.out.println(ValidArray[i] + "");
}
//calculates sum of all values in array of ValidArray (of grades)
for(double d : ValidArray){
sum += d;
}
//avergae of valid number array
double average = (sum/validArrayCount);
System.out.println("Average: " + average);
}
}
答案 0 :(得分:0)
你已经在做了,ValidArray
也是一个可怕的名字。您忘记在for
循环中使用数组索引,而您使用的数字是validArraycount
-
// Prints number of valid values entered
System.out.println("The number of valid grades entered is "
+ validArraycount);
// for printing array backwards
for (int i = validArraycount-1; i>=0; i--){
System.out.print(ValidArray[i] + " "); // <-- don't forget the index.
}
System.out.println(); // <-- next line.
double sum = 0;
// sum the values in array of ValidArray (of grades)
for(double d : ValidArray){
sum += d;
}
// Average. Sum / the count
double average = (sum / validArraycount);
System.out.println("Average: " + average);
答案 1 :(得分:0)
validArrayCount
已经包含输入的元素数量。
打印vArrayLen
:
validArrayCount
输入的有效成绩数量。
System.out.println("The number of valid grades entered is " + validArrayCount);
要获得平均值,您可以除以validArrayCount
而不是ValidArray.length
:
double average = (sum/validArrayCount);
此外,您必须在向后打印数组时添加数组索引:
// for printing array backwards
for (int i = validArrayCount-1; i>=0; i--){
System.out.print(ValidArray[i] + " ");
}
System.out.println(); //For inserting a new line after printing all array elements