java - 每个输入多次打印。为什么?

标签 java

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

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;
}

}

最佳答案

在切换结束时,您有

while ( test = true)

您可能想将其更改为

while ( test == true)

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

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

关于java - 每个输入多次打印。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17758087/

相关文章:

java - Play Framework 2.2.1 : Create Http. 测试上下文

java - 由 org.springframework.beans.factory.BeanCreationException 引起的成功安装 maven 的 Tomcat war 部署错误

Java String pool相关疑惑

Java如何将alpha、色调、饱和度和亮度转换为argb值?

java - 在 titan 图形数据库中使用 gremlin 服务器加载 Json

java - 带或不带可调用的 Spring 支架 Controller

java - Apache FOP - 有没有办法以编程方式嵌入字体?

java - 如何避免gradle中的字母数字执行顺序

java - wget/curl 从 Oracle Archive 页面下载 Oracle Java 7 SDK

java - 线程 "main"java.net.ConnectException : Connection refused: connect Socket Programming Java 中的异常