扫描仪只读取一次循环? - Java

时间:2013-11-21 00:15:02

标签: java

我正在努力创建一个课程,您可以输入任意数量的成绩(A-F)并计算GPA并返回GPA并获得课外活动的资格。看起来扫描仪只允许一个输入,然后打印GPA和资格。 到目前为止,这就是我所拥有的:

import java.util.Scanner;

public class Grades
{
    public static void main(String[] args)
    {
        double myGPA;
        int myNumClasses;
        double myValue;

        Scanner sc = new Scanner(System.in);

        System.out.println("Press any other lettter to calculate.");
        System.out.print("Enter grades: ");
        String input = sc.nextLine();

        myValue = 0;
        myNumClasses = 0;
        myGPA = 0;

        for (String next = sc.next(); input.equalsIgnoreCase("a") ||     input.equalsIgnoreCase("b") || 
        input.equalsIgnoreCase("c")|| input.equalsIgnoreCase("d") || input.equalsIgnoreCase("f"); next = sc.next())
        {
            if (input.equalsIgnoreCase("a"))
            {
                myValue += 4.0;
                myNumClasses += 1;
            }

            else if (input.equalsIgnoreCase("b"))
            {
                myValue += 3.0;
                myNumClasses += 1;
            }

            else if (input.equalsIgnoreCase("c"))
            {
                myValue += 2.0;
                myNumClasses += 1;
            }

            else if (input.equalsIgnoreCase("d"))
            {
                myValue += 1.0;
                myNumClasses += 1;
            }

            else if (input.equalsIgnoreCase("f"))
            {   
               myNumClasses += 1;
            }

            myGPA = myValue / myNumClasses;

            if (myGPA >= 2.0 && myNumClasses >= 4)
            {
                System.out.println("Eligible");
            }
            else if (myNumClasses < 4)
            {
                System.out.println("Ineligible, taking less than 4 classes");
            }
            else if (myGPA >= 2.0 && input.equalsIgnoreCase("f"))
            {
                System.out.println("Ineligible, gpa above 2.0 but has F grade");
            }
            else if (myGPA <= 2.0 && input.equalsIgnoreCase("f"))
            {
                System.out.println("Ineligible, gpa below 2.0 and has F grade");
            }
            else if (myGPA < 2.0)
            {
                System.out.println("Inelligible, gpa below 2.0");
            }
            System.out.println("Your GPA = " + myGPA);
         }
      }
}

1 个答案:

答案 0 :(得分:0)

    // It looks like your missing a for loop. I just copied some of your 
    //code and ran it through a for loop. The rest of the code is kind of unclear. 

    System.out.println("Enter the number of grades you will enter: ");
    int userAns = sc.nextInt();

    for (int index = 0; index <= userAns; index++)
    {
       System.out.println("Press any other letter to calculate.");
       System.out.print("Enter grades: ");
       String input = sc.nextLine();
    }