java - 拆分一个数组并将它们保存在 2 个单独的数组中

标签 java arrays

我有一个程序要求提供学生姓名和分数。名称和分数用“|”分隔。我将它们分开并将它们放入一个数组中,以便我可以根据分数显示统计数据。

当我尝试显示得分低于平均水平的学生的姓名时,我遇到了问题。每当我运行该程序时,都不会打印出 <belowAvg> 的内容。标签。我不认为这些名称被保存到 studentNames[i] 中文件。

如果有人能指出我正确的方向,我将不胜感激。

 // Get the data from the text area and dump it in the file in XML format
    String text = textArea.getText();
    // Print the text to the file  - for testing purposes only
    outfile.println(text);

    Double[] studentScores = new Double[10];
    String [] studentNames = new String[10];
    double sumScores = 0;
    String []lines = text.split("\n");
    int i;
    outfile.println("<students>");
    for(i=0;i<lines.length;i++)
    {
        outfile.println("<student>");
        String[]tokens =lines[i].split("\\|");
        outfile.println("<name>" + tokens[0] + "</name>");
        studentNames[i] = tokens[0];

        outfile.println("<score>" + tokens[1] + "</score>");
        Double score = Double.parseDouble(tokens[1]);
        studentScores[i] = score;
        outfile.println("</student>");
    }
    double arraySize = i;
    double average = 0;
    double maximum = studentScores[1];
    double minimum = studentScores[1];

    for(i=0;i<arraySize;i++)
    {
     sumScores = sumScores + studentScores[i];
     if(studentScores[i] > maximum)
     {
         maximum = studentScores[i];
     }
     if(studentScores[i] < minimum)
     {
        minimum = studentScores[i];
     }
    }

     average = sumScores / arraySize;

     outfile.printf("\nThe sum is: %.1f" , sumScores);
     outfile.printf("\n<average> %.1f", average);
     outfile.println("</average>");
     outfile.printf("\n<maximum> %.1f" , maximum);
     outfile.println("</maximum>");
     outfile.printf("\n<minimum> %.1f", minimum);
     outfile.println("</minimum");

     for(i=0;i<arraySize;i++)
     {
         if(studentScores[i] < average)
         {
             outfile.printf("\n<belowAvg>" , studentNames[i]);
             outfile.println("</belowAvg>");
         }
     }

    outfile.println("\n</students>");
    outfile.close();

这就是 XML 文件正在打印的内容。

jill|87
phil|23
michael|99
leny|67

<students>
<student>
<name>jill</name>
<score>87</score>
</student>
<student>
<name>phil</name>
<score>23</score>
</student>
<student>
<name>michael</name>
<score>99</score>
</student>
<student>
<name>leny</name>
<score>67</score>
</student>

The sum is: 276.0
<average> 69.0</average>

<maximum> 99.0</maximum>

<minimum> 23.0</minimum

<belowAvg></belowAvg>

<belowAvg></belowAvg>

</students>

最佳答案

if(studentScores[i] < average)
{
 outfile.printf("\n<belowAvg>" , studentNames[i]);
 outfile.println("</belowAvg>");
}

在您打算打印 StudentNames 数组中的值的行中,您实际上并未使用该值。您需要添加%s在您的<belowAvg>之后标签。

关于java - 拆分一个数组并将它们保存在 2 个单独的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53068688/

相关文章:

java - 在 websphere 中启动应用程序时出现异常 java.lang.NoSuchMethodError : org/w3c/dom/Node. getTextContent()Ljava/lang/String

java - 如何在 p :dataGrid using p:graphicImage 中显示图像

java 8迭代列表并按索引与另一个列表进行比较

c++ - 如何在 C++ 中初始化动态数组?

php - 为什么 array_uintersect() 比较 array1 和 array2、array1 和 array1 以及 array2 和 array2 之间的元素?

java - 无法打印整个字符串

Java NIO TCP超时问题

c++ - 如何用0初始化类成员数组

php - 如何在 Yii2 应用程序的多选下拉列表中显示选定的值?

arrays - 对读入数组的元素进行排序