为什么我的扫描仪输入2仅记录最后一个输入?

时间:2017-03-30 23:30:33

标签: java

我正在尝试创建一个大学gpa计算器。该课程首先要求课程数量,每个课程的学分数量,然后是每个课程的成绩。当每个班级具有相同的学分,但当每个班级有不同的学分时,该课程不起作用。我已经尝试通过包含在程序运行时显示值的打印语句来调试程序,并且我已经确定信用仅记录最后输入的值。这只是.nextDouble()的工作原理还是我犯了错误?我该如何解决这个问题呢?我也是Java的新手,所以如果我犯了一个简单的错误,那就是为什么......谢谢

import java.util.*;

public class Calculator {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    System.out.println("This program calculates your college GPA.");
    System.out.println("Enter the number of classes you've taken this quarter/semester:");
    int classes;
    classes = input.nextInt();
    double gradePoint = 0;
    double credits = 0;
    double totalCredits = 0;
    String grades;
    double gpa = 0;
    double total = 0;
    // for loop record number of credits per class
    for (int i = 0; i < classes; i++) {
        Scanner input2 = new Scanner(System.in);
        if (i > 0) {
            // user will be asked this second
            System.out.println("Enter next amount of credit hours:");
        }
        else {
            // user will be asked this first
            System.out.println("Enter the number of credits for each class one by one:");
        }
        credits = input2.nextDouble();
        System.out.println("credits:" + credits);
        totalCredits += credits;
        System.out.println("totalcredits:" + totalCredits);
    }
    System.out.println("credits:" + credits);
    // for loop to record grades input and calculate gpa
    for (int b = 0; b < classes; b++) {
        System.out.println("credits:" + credits);
        Scanner input3 = new Scanner(System.in);
        if (b > 0) {
            System.out.println("Enter your next grade:");
        }
        else {
            System.out.println("To receive your GPA, enter your grades one by one:");
        }
        grades = input3.nextLine();
        // if statements will convert letter grades to grade points
        // grade points will then be multiplied by their respective amount of credits
        if (grades.equals("A+")) {
            gradePoint=4.0;
            total += (gradePoint*credits);
        }
        else if (grades.equals("A")) {
            gradePoint=4.0;
            total += (gradePoint*credits);
        }
        else if (grades.equals("A-")) {
            System.out.println("credits:" + credits);
            gradePoint=3.7;
            System.out.println("gradePoint:" + gradePoint);
            total += (gradePoint*credits);
            System.out.println("total:" + total);
        }
        else if (grades.equals("B+")) {
            System.out.println("credits:" + credits);
            gradePoint=3.3;
            System.out.println("gradePoint:" + gradePoint);
            total += (gradePoint*credits);
            System.out.println("total:" + total);
        }
        else if (grades.equals("B")) {
            System.out.println("credits:" + credits);
            gradePoint=3.0;
            System.out.println("gradePoint:" + gradePoint);
            total += (gradePoint*credits);
            System.out.println("total:" + total);
        }
        else if (grades.equals("B-")) {
            System.out.println("credits:" + credits);
            gradePoint=2.7;
            System.out.println("gradePoint:" + gradePoint);
            total += (gradePoint*credits);
            System.out.println("total:" + total);
        }
        else if (grades.equals("C+")) {
            gradePoint=2.3;
            total += (gradePoint*credits);
        }
        else if (grades.equals("C")) {
            gradePoint=2.0;
            total += (gradePoint*credits);
        }
        else if (grades.equals("C-")) {
            gradePoint=1.7;
            total += (gradePoint*credits);
        }
        else if (grades.equals("D+")) {
            gradePoint=1.3;
            total += (gradePoint*credits);
        }
        else if (grades.equals("D")) {
            gradePoint=1.0;
            total += (gradePoint*credits);
        }
        else if (grades.equals("D-")) {
            gradePoint=0.7;
            total += (gradePoint*credits);
        }
        else if (grades.equals("F")) {
            gradePoint=0;
            total += (gradePoint*credits);
        }
        else {
            System.out.println("Invalid input.");
        }
        // gpa algorithm
        gpa = total/totalCredits;
        System.out.println("totalCredits" + totalCredits);
        System.out.println("gpa:" + gpa);
    }
    System.out.println("GPA: " + gpa);
}

}

此外,输入时:

3
5
5
3
A+
B+
C-

该程序输出以下内容:

This program calculates your college GPA.
Enter the number of classes you've taken this quarter/semester:
3
Enter the number of credits for each class one by one:
5
credits:5.0
totalcredits:5.0
Enter next amount of credit hours:
5
credits:5.0
totalcredits:10.0
Enter next amount of credit hours:
3
credits:3.0
totalcredits:13.0
credits:3.0
credits:3.0
To receive your GPA, enter your grades one by one:
A+
totalCredits13.0
gpa:0.9230769230769231
credits:3.0
Enter your next grade:
B+
credits:3.0
gradePoint:3.3
total:21.9
totalCredits13.0
gpa:1.6846153846153844
credits:3.0
Enter your next grade:
C-
totalCredits13.0
gpa:2.076923076923077
GPA: 2.076923076923077

(正确的gpa应为3.2)

1 个答案:

答案 0 :(得分:0)

输入

时,信用变量中的问题
  
      
  • 5
  •   
  • 5
  •   
  • 3
  •   

最后一个3输入存储在您的信用变量中,并且相同的值将通过您用于成绩的第二个循环。

解决方案1: 使用数组学分数组并将所有课程学分输入到此数组中,以便您可以在成绩输入中访问不同的学分。

解决方案2: 只使用一个循环获得学分和成绩并逐个输入我为你制作一些代码。 这是代码。

for (int b = 0; b < classes; b++) {
    Scanner input3 = new Scanner(System.in);
    System.out.println("Enter credit hours for class :"+(b + 1));
    credits = input2.nextDouble();
    System.out.println("credits:" + credits);
    totalCredits += credits;
    System.out.println(" ");
    System.out.println("Enter grade for class : "+(b + 1));
    grades = input3.nextLine();

   }

<强>输入

 3  //number of classes
 5  //first class credits
 A+ //first class grade
 5  .
 B+ .
 3  .
 c- ..........

<强>输出:

gpa:3.2
GPA: 3.2