【问题标题】:I need to turn a comma-separated string into an array list and then calculate the average of those scores [duplicate]我需要将逗号分隔的字符串转换为数组列表,然后计算这些分数的平均值 [重复]
【发布时间】:2016-05-21 12:09:32
【问题描述】:

我正在尝试获取我拥有的代码输入并将它们转换为一个 int 数组列表,以便我可以使用它们来计算平均分数,但我不确定如何执行此操作。此外,当涉及到分数时,输入的范围可以从 1 到 5,因此一个学生可能有 3 个分数,下一个学生可能有 5 个分数,依此类推。这是我到目前为止的代码。

我的主要问题是如何获取列表输入并将它们转换为单独的数组整数?

//data declarations and initalizations
    final int MAX_STUDENTS = 12;
    final int MAX_SCORE = 5;

    //data declarations
    double avg; //average total for all students
    int numStudents; //number of students 1-12
    double totalAvgStu = 0.0; //
    int num;
    String stuScores = " "; //student score 1-5

    //array declarations and inistiations
    int [] score = new int[MAX_SCORE]; // score from input range between 1-5
    double [] avgScores = new double[MAX_STUDENTS]; //avg total of score for one student
    String [] testScores = new String[MAX_STUDENTS];
    String [] array1 = new String[MAX_SCORE]; 

    //user inputs for number of students
    numStudents = UtilsKS.readInt("Enter number of students: ", false);                                

    //while loop for correct input of students
    while (numStudents <= 0 || numStudents > 12) { 
        System.out.print("ERROR: must be 1-12, ");

        numStudents = UtilsKS.readInt("Enter number of students: ", false);
    }

    // user input for student score using a for loop
    for (int i = 0; i < numStudents; i++) {
        stuScores = UtilsKS.readString("Enter comma-separated test score for student " + (i+1) + ": ", false);

    }

    //for loop for arrays
    for (int i=0; i<avgScores.length; i++) {
        totalAvgStu += avgScores[i];

    }

    //outputs
    String header = "average score1 score2 score3 score4 score5";
    System.out.println("\n" + header);                                                                                  
    System.out.println("\nAvg for all students = " + totalAvgStu/avgScores.length);

    //initializing testscore to = what scores are inputed per student
    for (int t=0; t<numStudents; t++) {
        testScores[t] = stuScores;
    }    

【问题讨论】:

  • 您已发布要求和代码,但没有提出具体问题。我们当然不会为您编写代码,但我们可以回答问题。所以请解决这个问题,以便我们知道如何为您提供帮助。
  • 这里有很多代码。究竟是什么问题?
  • 对不起,我添加了我的主要问题,我是新手。我知道你必须以某种方式使用 .split() 和 integer.parseInt() 但我不明白如何?
  • 另请查看this Google search,了解本网站上的其他类似问题和答案。
  • 感谢那些真正提供帮助的人!

标签: java arrays integer


【解决方案1】:
Arrays.stream("1,2,3,4,5".split(",")).mapToInt(Integer::parseInt).average();

我建议考虑我的紧凑型解决方案。返回OptionalDouble。如果您首先通过isPresent() 进行检查,getAsDouble() 将返回结果以避免NoSuchElementException


编辑:

我不喜欢用长变量名编写大量代码,但为了让你更好地理解,我做了个例外。

String commaSeparatedString = "1,2,3,4,5";
String[] numbers = commaSeparatedString.split(",");
Stream<String> stringStream = Arrays.stream(numbers);
IntStream intStream = stringStream.mapToInt(value -> Integer.parseInt(value));
OptionalDouble average = intStream.average();
System.out.println(average.isPresent() ? 
                     "average = " + average.getAsDouble() : 
                     "stream is empty!")

【讨论】:

  • 啊,是的,Java 8 的方法。 1+
  • 我对编码还是有点陌生​​,所以我有点困惑
  • Arrays 是从 java.util.* 导入的 :) 以防万一'只是提一下。
  • 老兄,我想学习如何使用:: 哪里可以参考?请告诉! @安德鲁
  • @TilakMadichetti,看here
【解决方案2】:

你能提供一个示例输入和输出吗?
还有一种简单(hacky)的方法是初始化 2 个变量 temp 和 sum。使用逗号解析字符串。将值存储在 temp 中并添加到 sum。为您完成这些步骤的次数存储一个计数器。将总和除以计数器以获得平均值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 2014-08-24
    • 2017-10-02
    • 1970-01-01
    • 2021-12-18
    • 1970-01-01
    相关资源
    最近更新 更多