【问题标题】:Grade Calculator Using Arrays使用数组的成绩计算器
【发布时间】:2015-01-19 01:52:42
【问题描述】:

我们正在编写一个名为 GradeBook 的程序,它允许用户输入学生数据,例如姓名和不同年级项目的分数。 GradeBook 为每个学生提供选项计算成绩并绘制班级成绩分布。用户可以随时返回并添加更多学生数据。它支持的最大学生数为 200,一个类别中的最大成绩项目数为 10。例如,它仅支持最多 10 个测验、10 个考试和 10 个家庭作业。

数据输入示例如下:

乔·W·史密斯:e100 e95 e87 q10 q10 q8 h10 h10 h10

迈克尔·布朗:q10 q10 h7 h10 h9 h10 e80

要显示学生的成绩和统计数据,我们应该使用 System.out.printf。

这是最终输出的另一个示例。

 Name     Exam Exam Exam Quiz Quiz Quiz HWork HWork HWork Grade

 Danny Devito 100.0  80.0 90.0 10.0 10.0 0.0 10.0 5.0 10.0 84.0

 Joe Smith 85.0 90.0 100.0 10.0 10.0 5.0 0.0 10.0 5.0      81.7

 Will Smith 60.0 100.0 90.0 10.0 10.0 8.0 10.0 0.0 10.0    82.0

这个最后的例子应该排列得更好,并用小数对齐,所以它看起来整洁干净,但我不确定如何做到这一点。

  import java.util.Scanner;

公共类 GradeCalcWithArrays { /* * Logan Wegner 目的是计算 * 输入成绩 */ public static void main(String[] args) {

    Scanner s = new Scanner(System.in);

    boolean done = false;
    boolean quit = false;
    int choice = 0;

    int studentcounter = 0;

    int[] examstats = new int[3]; /*
                                 * Array created to store the information
                                 * entered for exams
                                 */
    int[] quizstats = new int[3]; /*
                                 * Array created to store the information
                                 * entered for quizzes
                                 */
    int[] homeworkstats = new int[3]; /*
                                     * Array created to store the
                                     * information entered for homework
                                     */

    String[] studentnames = new String[200]; /*
                                             * Array created to store the
                                             * student name information
                                             * entered
                                             */

    System.out.println("Welcome to GradeBook!");
    System.out.println("Please provide grade item details");

    System.out.print("Exams    (number, points, weight):");

    examstats[0] = s.nextInt(); // inputs exam number
    examstats[1] = s.nextInt(); // inputs exam points
    examstats[2] = s.nextInt(); // inputs exam weight

    System.out.print("Quizzes     (number, points, weight):");

    quizstats[0] = s.nextInt(); // inputs quiz number
    quizstats[1] = s.nextInt(); // inputs quiz points
    quizstats[2] = s.nextInt(); // inputs quiz weight

    System.out.print("Homework    (number, points, weight):");

    homeworkstats[0] = s.nextInt(); // inputs homework number
    homeworkstats[1] = s.nextInt(); // inputs homework points
    homeworkstats[2] = s.nextInt(); // inputs homework weight

    System.out.println("--------------------");

    do {
        System.out.println("What would you like to do?");
        System.out.println("    1 Add student data");
        System.out.println("    2 Display student grades & statistics");
        System.out.println("    3 Plot grade distribution");
        System.out.println("    4 Quit");
        System.out.print("Your choice:");
        choice = s.nextInt(); /*
                             * Choice will determine what the next course of
                             * action will be with the program
                             */

        if (choice == 1) {
            System.out.println("Enter student data:");
            for (int i = 0; i <= 200; i++) {
                studentcounter = studentcounter + 1;
                System.out.print("Data>");
                studentnames[i] = s.nextLine();

                if (studentnames[i].equals("done")) {
                    break;
                }
            }
        }

        if (choice == 2) {

        }

        if (choice == 3) {

        }

        if (choice == 4) {
            quit = true;
            System.out.println("Good bye!");
        }

    } while (quit == false);

}

 }

我坚持的最大部分是能够输入数据并将其放入字符串和数组中。我不确定如何获取使用 e100 q100 h100 输入的数据,因为它们可能会混淆和乱序。我真的非常感谢一些帮助。在此先感谢各位。

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    尝试将您的问题与您真正感到困惑的事情隔离开来。通常这会导致你弄清楚你需要做什么。

    您似乎真的只是不确定如何将 examquizhomework 字符串分成 3 个单独的数组,因为它们没有任何顺序。好吧,要将它们分开,它们必须有差异。三种类型的字符串有什么区别?

    所有 3 种类型的字符串输入都可以在字母上附加 0 到 100 之间的某个数字:“e”、“q”或“h”。所以唯一的区别是每个字符串的第一个字母。因此,要将所有各种字符串分成单独的数组,请检查第一个字母是什么并将其相应地放入相应的数组中。

    现在您终于到了需要编码的地步:如何根据字符串的第一个字母以编程方式分隔字符串?

    嗯,你有if 语句,我相信你可以弄清楚如何获取字符串的第一个字符。

    用文字(又名伪代码)列出:

    String s;
    Char c;
    
    c = //get the first letter of string 's'. Figure this out with a simple google search.
    
    if (c == 'e') 
    {
        //Add the string after the first character in string s to the Exam array
        //NOTE: You have int arrays, so you will need to convert the string to an int
        //      before adding it to the array.
    }
    else if (c == 'q')
    {
        //Same as above with Quiz array
    } 
    else if ...
    {
        //... you should be able to finish this!
    }
    

    这应该可以帮助您开始您的项目。尝试在其余作业中模仿上面详细介绍的思维过程,您会很快发现您在初学者级别遇到的每个问题都已经解决了;你只需要找出你在编程方面的具体问题。

    【讨论】:

    • 感谢您的帮助。这绝对是我问题的 90%。我想我知道你的意思,而不是做 s.firstLetter 的事情。你是说我应该使用 split 来挑选 e,然后我就可以将不同的分数输入到正确的数组中?还是我应该尝试使用子字符串来查找 e,然后在正确的数组中输入我的成绩?
    • @Daniel 对你来说最有意义的。你可以用很多不同的方式来做到这一点。我不想告诉你具体怎么做,但是如果你阅读我上面贴的代码,你可以分析它并弄清楚我的思维过程和意图(主要通过if 块)。
    • 给出的例子是'Joe Smith: e100 e80 e90 q10 q10 q0 h10 h5 h10' J 是序列的第一个字符,并且有多个 e、q 和H。您解释如何进行此操作的方式只需要正确的字符吗?我很困惑我将如何找到 3 e 或者会有多少,然后将它们的所有值放入一个数组中。
    • @Daniel 啊,好的。你需要做你当时说的两件事。在每个输入中查找模式并相应地拆分字符串。我要做的是用分号分割字符串,这样你就有两个字符串,名称和字符串的其余部分:' e100 q100' 等。但是你需要使用名称字符串,然后用空格分割第二个字符串,所以你会有'e100','q100'等。然后用这些字符串做我在答案中所说的。您将不得不遍历字符串并将每个字符串添加到其对应的数组中。
    • 感谢到目前为止马修的所有帮助。除了在将字符串添加到数组之前将字符串转换为 int 的部分之外,我现在已经完成了所有工作。你能给我一些关于如何做到这一点的见解吗?
    猜你喜欢
    • 1970-01-01
    • 2015-09-09
    • 2017-03-01
    • 1970-01-01
    • 2015-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多