简体   繁体   English

java 中的数据类型和计算平均值

[英]data type and calculate average in java

I am working on a java code that calculates the average of grades in an array for N of students and it is working fine when I enter grades like {3,4,3} but when I use numbers with decimals like {3.7,2.5,3.2} it starts giving me errors and I want to make a class of data type Students for example.我正在研究一个 java 代码,该代码计算 N 个学生的数组中的平均成绩,当我输入 {3,4,3} 这样的成绩时它工作正常但是当我使用带小数的数字时 {3.7,2.5, 3.2} 它开始给我错误,我想制作一个数据类型为 Students 的 class。

import java.util.*;
public class ArrayAverageProblem { 
   public static void main(String[] args) { 
      System.out.println("Enetr number of students : "); 
      Scanner adnan = new Scanner(System.in); 
      int length = adnan.nextInt(); 
      int[] input = new int[length]; 
      System.out.println("Enter cgpa of students : "); 
      for (int i = 0; i < length; i++) { 
         input[i] = adnan.nextInt(); 
      } 
      double averageCgpa = averageCgpa(input); 
      System.out.println("Average of students cgpa :  " + averageCgpa); 
      adnan.close(); 
   }   
   public static double averageCgpa(int[] input) { 
      double sum = 0f; 
      for (int number : input) { 
         sum = sum + number; 
      } 
      return sum / input.length; 
   } 
}

Any help would be really appreciated.任何帮助将非常感激。

Problem is with the input array datatype.问题出在输入数组数据类型上。 If input array is also expecting double value (with decimal) you need to take the input array as double.如果输入数组也需要双精度值(带小数),则需要将输入数组作为双精度值。

Please change the below line请更改以下行

int[] input = new int[length];

with

double[] input = new double[length];

and below line和线下

input[i] = adnan.nextInt(); 

with

input[i] = adnan.nextDouble(); 

It will work for both the types of numbers(with and without decimal).它适用于两种类型的数字(带小数点和不带小数点)。

I hope this will solve the issue you're facing.我希望这能解决您面临的问题。

i improved your code to use double numbers.我改进了您的代码以使用双数。 note that your grades must be double and use nextDouble() method to get grade from scanner.请注意,您的成绩必须是双倍的,并使用nextDouble()方法从扫描仪获取成绩。 this code is below这段代码在下面

public static void main(String[] args) {

    System.out.println("Enetr number of students : ");

    Scanner adnan = new Scanner(System.in);

    int length = adnan.nextInt();

    double[] input = new double[length];

    System.out.println("Enter cgpa of students : ");

    for (int i = 0; i < length; i++) {

        input[i] = adnan.nextDouble();

    }

    double averageCgpa = averageCgpa(input);

    System.out.println("Average of students cgpa :  " + averageCgpa);

    adnan.close();

}



public static double averageCgpa(double[] input) {

    double sum = 0f;

    for (double number : input) {

        sum = sum + number;

    }

    return sum / input.length;

}

In java if both are int, result is also int. java如果都是int,result也是int。 So you can use sum / Double.valueOf(input.length);所以你可以使用 sum / Double.valueOf(input.length);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM