【问题标题】:changing from Double ArrayList to Integer ArrayList从 Double ArrayList 更改为 Integer ArrayList
【发布时间】:2016-11-19 01:42:13
【问题描述】:

在下面的程序中,我编写了一个程序,它读取 5 个学生姓名,以及每个学生 5 次测验的分数。我已将名称加载到 String 类型的 ArrayList 中,并将测验标记加载到 Double 类型的 ArrayList 中。但是我需要将这些测验标记加载到一个整数中,我不知道如何更改它

import java.util.Scanner;
import java.util.ArrayList;
public class onemoretime 
{
    public static final double MAX_SCORE = 15 ;
    public static final int NAMELIMIT = 5;
    public static void main(String[] args) 
    {
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Double> averages = new ArrayList<>();  //line that needs to become a Integer ArrayList
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < NAMELIMIT; i++)
    {
        String line = in.nextLine();
        String[] words = line.split(" ");
        String name = words[0] + " " + words[1];
        double average = findAverage(words[2], words[3], words[4], words[5], words[6]);
        System.out.println("Name: " + name + "   Quiz Avg: " + average);

        names.add(name);
        averages.add(average);
    }
  }

 public static double findAverage(String a, String b, String c, String d, String e)
 {
    double sum = Double.parseDouble(a) + Double.parseDouble(b) + Double.parseDouble(c) + Double.parseDouble(d) + Double.parseDouble(e);
return (sum / NAMELIMIT);
  }
}

对于输入:

Sally Mae 90 80 45 60 75
Charlotte Tea 60 75 80 90 70
Oliver Cats 55 65 76 90 80
Milo Peet 90 95 85 75 80
Gavin Brown 45 65 75 55 80

我得到了正确的输出

Name: Sally Mae   Quiz Avg: 70.0
Name: Charlotte Tea   Quiz Avg: 75.0
Name: Oliver Cats   Quiz Avg: 73.2
Name: Milo Peet   Quiz Avg: 85.0
Name: Gavin Brown   Quiz Avg: 64.0

【问题讨论】:

  • 所以你想要整数输出,但你得到的输出是小数位,对吗?
  • 我只想读取 Integer ArrayList 而不是 double ArrayList 中的值
  • 但我还是想输出双倍
  • 请显示预期的输出格式,以便您的问题变得清晰。
  • 你为什么要这么做?

标签: java arraylist integer double


【解决方案1】:

在 Java 中,如果你用一个整数除以一个整数,你会得到一个整数,结果会丢失精度, 示例:-

Integer division    8 / 5 = 1
Double division     8.0 / 5.0 = 1.6
Mixed division  8.0 / 5 = 1.6

因此,如果您仍想将输入更改为整数,很酷!继续将输入更改为整数。如果您想要精度,请确保输入之一是双精度。 例如:-

Input  = saumyaraj zala 45 25 14 78 45
Output = Name: saumyaraj zala   Quiz Avg: 41.4

【讨论】:

    【解决方案2】:

    这样的?

      List<Double> dValues = new ArrayList<>();
      // not shown: add your double values to the dValues list
      List<Integer> iValues =  dValues.stream()
          .map(p -> p.intValue())
          .collect(Collectors.toList());
      // or the equivalent
      //  List<Integer> iValues = dValues.stream()
      //      .map(Double::intValue)
      //      .collect(Collectors.toList()); 
    

    注意:intValue() 方法可能不会像您期望的那样四舍五入,因此您可能需要对 lambda 的那部分更感兴趣。

    【讨论】:

      【解决方案3】:

      Java 数学库有几个静态方法,如果你想要一个快速的解决方案,它们会返回双精度值,但你可以像这样将它们转换为整数

      (int)Math.floor(doubleNum); // 4.3 becomes 4
      (int)Math.ceil(doubleNum); // 4.3 becomes 5
      

      【讨论】:

      • 请注意,如果您只是想从 double 转换为 int,(int)someDouble 等效于 (int)Math.floor(someDouble)。 Java 在转换为整数时会自动丢弃双精度数的小数部分。
      猜你喜欢
      • 2019-10-03
      • 2013-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多