0

每次我运行此代码时,它都会到达询问学生 ID 的位置,并打印出学生 ID 部分和作业部分。为什么?我正在尝试获取名称、id、作业、实验室、考试、讨论和项目的字符串,然后在另一堂课中,我将作业、实验室和考试字符串拆分为数组,然后将这些数组解析为双精度数。在我解析它们之后,我用另一种方法对它们进行总计,并将总计与项目和讨论相加以获得总分。

import java.util.Scanner;
import java.io.*;

public class GradeApplication_Kurth {
public static void main(String[] args) throws IOException
{
    Student_Kurth one;
    int choice;

    boolean test = true;

    do
    {   
        Scanner keyboard = new Scanner(System.in);
        PrintWriter outputFile = new PrintWriter("gradeReport.txt");

        System.out.println("Please select an option: \n1. Single Student Grading \n2. Class Grades \n3. Exit");
        choice = keyboard.nextInt();

        switch (choice)
        {
        case 1 :
            System.out.println("Please enter your Student name: ");
            String name = keyboard.next();
            System.out.println("Please enter you Student ID: ");
            String id = keyboard.nextLine();
            System.out.println("Please enter the 10 homework grades seperated by a space: ");
            String homework = keyboard.next();
            System.out.println("Please enter the 6 lab grades seperated by a space: ");
            String lab = keyboard.nextLine();
            System.out.println("Please enter the 3 exam grades seperated by a space: ");
            String exam = keyboard.nextLine();
            System.out.println("Please enter the discussion grade: ");
            double discussion = keyboard.nextDouble();
            System.out.println("Please enter the project grade: ");
            double project = keyboard.nextDouble();

            one = new Student_Kurth(name, id, homework, lab, exam, discussion, project);

            outputFile.println(one.toFile());
            System.out.println(one);
            break;
        case 2 :
            File myFile = new File("gradeReport.txt");
            Scanner inputFile = new Scanner(myFile);
            while(inputFile.hasNext())
            {
                String str = inputFile.nextLine();
                System.out.println("\n" + str);
            }   
            break;
        case 3 :
            test = false;
            keyboard.close();
            outputFile.close();
            System.exit(0);
        }
    } while (test = true);
}
 }

二等

public class Student_Kurth 
{
public String homework;
public String name;
public String id;
public String lab;
public String exam;
public double project;
public double discussion;
public double[] hw = new double[10];
public double[] lb = new double[6];
public double[] ex = new double[3];
public final double MAX = 680;
public double percentage;
public String letterGrade;

public Student_Kurth()
{
    homework = null;
    name = null;
    id = null;
    lab = null;
    exam = null;
    project = 0;
    discussion = 0;
}

public Student_Kurth(String homework, String name, String id, String lab, String exam, double project, double discussion)
{
    this.homework = homework;
    this.name = name;
    this.id = id;
    this.lab = lab;
    this.exam = exam;
    this.project = project;
    this.discussion = discussion;
}

public void Homework(String homework)
{
    String delims = " ";
    String[] tokens = this.homework.split(delims);
    int tokenCount = tokens.length;
    for(int i = 0; i < tokenCount; i++)
    {
        hw[i] = Double.parseDouble(tokens[i]);
    }
}

public void Lab(String lab)
{
    String delims = " ";
    String[] tokens = this.lab.split(delims);
    int tokenCount = tokens.length;
    for(int i = 0; i < tokenCount; i++)
    {
        lb[i] = Double.parseDouble(tokens[i]);
    }
}

public void Exam(String exam)
{
    String delims = " ";
    String[] tokens = this.exam.split(delims);
    int tokenCount = tokens.length;
    for(int i = 0; i < tokenCount; i++)
    {
        ex[i] = Double.parseDouble(tokens[i]);
    }
}

public double getHomeworkTotal(double[] hw)
{
    double hwTotal = 0;
    for(int i = 0; i < hw.length; i++)
    {
        hwTotal += hw[i];
    }
    return hwTotal;
}

public double getLabTotal(double[] lb)
{
    double lbTotal = 0;
    for(int i = 0; i < lb.length; i++)
    {
        lbTotal += lb[i];
    }
    return lbTotal;
}

public double getExamTotal(double[] ex)
{
    double exTotal = 0;
    for(int i = 0; i < ex.length; i++)
    {
        exTotal += ex[i];
    }
    return exTotal;
}

public double getTotalScores(double getExamTotal, double getLabTotal, double getHomeworkTotal)
{
    return getExamTotal + getLabTotal + getHomeworkTotal + this.project + this.discussion;
}

public double getPercentage(double getTotalScores)
{
    return 100 * getTotalScores / MAX;
}

public String getLetterGrade(double getPercentage)
{
    if(getPercentage > 60)
    {
        if(getPercentage > 70)
        {
            if(getPercentage > 80)
            {
                if(getPercentage > 90)
                {
                    return "A";
                }
                else
                {
                    return "B";
                }   
            }
            else
            {
                return "C";
            }
        }
        else
        {
            return "D";
        }
    }
    else
    {
        return "F";
    }
}

public void getLetter(String getLetterGrade)
{
    letterGrade = getLetterGrade;
}

public void getPercent(double getPercentage)
{
    percentage = getPercentage;
}
public String toFile()
{
    String str;
    str = " " + name + " - " + id + " - " + percentage + " - " + letterGrade;
    return str;
}

public String toString()
{
    String str;
    str = "Student name: " + name + "\nStudent ID: " + id + "\nTotal Score: " + getTotalScores(getExamTotal(ex), getLabTotal(lb), getHomeworkTotal(hw)) +
            "\nMax Scores: " + MAX + "Percentage: " + percentage + "Grade: " + letterGrade;
    return str;
}

}

4

2 回答 2

2

在切换结束时,您有

while ( test = true)

您可能想将其更改为

while ( test == true)

此外,将这些行从循环中取出:

Scanner keyboard = new Scanner(System.in);
PrintWriter outputFile = new PrintWriter("gradeReport.txt");
于 2013-07-20T02:35:35.043 回答
1

除了 Ermir 的回答,这条线不会记录所有的成绩:

System.out.println("Please enter the 10 homework grades seperated by a space: ");
String homework = keyboard.next();

Keyboard.next 只读取到下一个分隔符标记,因此如果要捕获由空格分隔的 10 个等级,则需要捕获整行,例如:

System.out.println("Please enter the 10 homework grades separated by a space: ");
String homework = keyboard.nextLine();
于 2013-07-20T02:38:38.557 回答