使用数组的成绩计算器

时间:2014-11-21 01:07:18

标签: java arrays

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

输入数据的一个例子是这样的:

Joe W. Smith:e100 e95 e87 q10 q10 q8 h10 h10 h10

Michael Brown: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输入数据,因为它们可能混淆不正常。我非常感谢你对此有所帮助。先谢谢你们。

1 个答案:

答案 0 :(得分:0)

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

您好像不确定如何将examquizhomework字符串分成3个单独的数组,因为它们不属于任何订单。那么,要将它们分开,它们必须有差异。这三种字符串之间的区别是什么?

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

现在,您最终需要编写代码:如何根据首字母以编程方式分隔字符串?

嗯,你有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!
}

这可以让你开始你的项目。尝试模仿上面详细介绍的思考过程,您将很快发现在初学者级别遇到的每个问题都已经解决过;你只需要在编程方面找出具体问题。