gpt4 book ai didi

java扫描仪数据多种类型

转载 作者:行者123 更新时间:2023-12-01 11:35:13 25 4
gpt4 key购买 nike

过去 3 小时我一直在做家庭作业,但没有取得太大进展。我想知道是否有人可以帮助我走向正确的方向。

任务是从文件中读取数据并将其转换为对读者更友好的内容。数据文件看起来像这样:

0.30 0.30 0.40

161

3333 70 60 50

4444 50 50 50

5555 80 90 80

0

162

1212 90 85 92

6666 60 80 90

7777 90 90 90

8888 95 87 93

9999 75 77 73

0

263

2222 90 65 75

8989 60 40 60

9090 70 80 30

0

该文件包含 5 种不同类型的数字。
1. 三位数为类(class)编号。
2. 四位数字是学号。

  • 这两位数字是每个学生的作业、期中和期末成绩。
  • 顶部的小数用于计算分数的加权平均值。
  • 零是类(class)之间的休息时间。
  • 我应该读取文件中的所有数据,然后格式化并输出它,以便于阅读:

    Grade Data For Class 161

    ID Programs Midterm Final Weighted Average Programs grade
    -- -------- ------- ----- ---------------- --------------
    3333 70 60 50 59.00 Pass
    4444 50 50 50 50.00 Fail
    5555 80 90 80 83.00 Pass
    Class Average: 64.00

    我有扫描仪设置,并且能够从文本中解析出数字,我的问题是它们都保存为字符串,所以我无法对它们进行任何数学检查。我开始怀疑这是否是解决这个问题的最佳方法。

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;

    public class classAverage
    {
    public static void main(String[] args) throws IOException
    {
    //in variable equals entire file
    Scanner in = new Scanner(new File("courseData.txt"));

    int classID;



    System.out.println("ID Programs Midterm Final Weighted Average Programs Grade");
    System.out.println("-- -------- ------- ----- ---------------- --------------");

    while(in.hasNextLine()) //While file has new lines
    {
    //line equals each line of text
    String line = in.nextLine();
    Scanner lineParser = new Scanner(line);
    System.out.println(line);

    for(; lineParser.hasNext();)
    {
    //number = each number
    String number = lineParser.next();
    System.out.println(number);



    if(number < 1 && number > 0)
    {
    double programsAverage = number.nextDouble();
    double midtermAverage = number.nextDouble();
    double finalAverage = number.nextDouble();
    System.out.println(programsAverage);
    System.out.println(midtermAverage);
    System.out.println(finalAverage);

    }

    }

    }



    }

    }

    更新 我已包含更新的代码。我现在的问题是我的 for 语句中的条件。这些应该检查传入扫描仪数据的值以查看数据是否:

    • a weight calculator(0,1)
    • a score(1,100),
    • a classNumber (100,400),
    • a studentNumber(1000,9999),
    • a class separator (0).

    我在想这样的事情:

    for(in.next(); in < 100 && in > 1; next());

    但这并没有完全做到这一点。

    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;

    /**
    * Write a description of class classAverage here.
    *
    * @author
    * @version
    */
    public class classAverage
    {
    public static void main(String[] args) throws IOException
    {
    //in variable equals entire file
    Scanner in = new Scanner(new File("courseData.txt"));

    int classNumber;
    int studentNumber;
    int programs;
    int midterm;
    int finals;


    double programWeight = in.nextDouble();
    double midtermWeight = in.nextDouble();
    double finalWeight = in.nextDouble();

    //System.out.println(programWeight + " " + midtermWeight + " " + finalWeight);
    for(int k = 0; k < 3; k++)
    {
    for(int i = 0; i <= 0; i++)
    {
    classNumber = in.nextInt();

    System.out.println("Grades for class: " + classNumber);
    System.out.println(" ID Programs Midterm Final Weighted Average Programs Grade");
    System.out.println(" -- -------- ------- ----- ---------------- --------------");
    }

    int studentCount = 0;
    double sumAverage = 0.0;

    for(int j = 0; j <= 2; j++)
    {
    studentNumber = in.nextInt();
    programs = in.nextInt();
    midterm = in.nextInt();
    finals = in.nextInt();
    studentCount++;
    double weightedAverage = (programWeight * programs) + (midtermWeight * midterm) + (finalWeight * finals);
    sumAverage += weightedAverage;

    System.out.printf("%d %d %d %d %.2f ", studentNumber,programs,midterm,finals,weightedAverage);
    if(programs >= 70)
    {
    System.out.print("PASS");
    } else {
    System.out.print("FAIL");
    }
    System.out.println();
    }
    double classAverage = sumAverage / studentCount;
    System.out.printf("Class average is: %.2f", classAverage);
    System.out.println("\n\n");

    }



    }

    }

    最佳答案

    这里有 2 个选择:

    1. Use Scanner#hasNextInt() and nextInt() instead of nextLine() to read Integers directly instead of numbers in String format.

    2. Keep your reading / scanning code intact. Use Integer.parseInt() to convert String to Integers. --> Preferred solution. As it is more efficient.

    关于java扫描仪数据多种类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30068456/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com