不了解如何使我的for循环和if语句工作

时间:2014-12-31 21:38:28

标签: java if-statement for-loop

我有这个,但我很失落如何才能将最终的GPA打印出来,我尝试了各种各样的方法,但未能成功完成。这就是我所拥有的:

Scanner input = new Scanner(System.in);

    System.out.println("How many grades are you putting? ");
    int length = input.nextInt();
    input.nextLine();

    String[] gradesArray = new String[length];

    for(int i = 0; i < gradesArray.length; i++)
    {
        System.out.println("Enter grade (include + or -) ");
        gradesArray[i] = input.nextLine();

        double points = 0.0;
        if(gradesArray[i].equalsIgnoreCase("A+") || gradesArray[i].equalsIgnoreCase("A"))
        {
           points += 4;

        }
        else if(gradesArray[i].equalsIgnoreCase("A-"))
        {
            points+= 3.7;

        }
        else if(gradesArray[i].equalsIgnoreCase("B+"))
        {
            points += 3.3;

        }
        else if(gradesArray[i].equalsIgnoreCase("B"))
        {
            points += 3.0;

        }
        else if(gradesArray[i].equalsIgnoreCase("B-"))
        {
            points += 2.7;

        }
        else if(gradesArray[i].equalsIgnoreCase("C+"))
        {
            points += 2.3;

        }
        else if(gradesArray[i].equalsIgnoreCase("C"))
        {
            points += 2.0;

        }
        else if(gradesArray[i].equalsIgnoreCase("D"))
        {
            points += 1.0;

        }
        else if(gradesArray[i].equalsIgnoreCase("F"))
        {
            points += 0.0;

        }
        else
        {
            System.out.println("Invalid grade");
        }
        System.out.println("GPA: " + points / gradesArray.length);
    }

我猜GPA没有正确打印出来,因为在条件匹配等级后,它会向下打印,对吧?而且,我怎么能这样做,如果他们输入无效等级,它会让用户重新开始。

4 个答案:

答案 0 :(得分:2)

你非常接近。您需要在println之前添加另一个括号来关闭forloop。您只想在输入所有成绩后计算GPA。像这样:

而且,正如Jason在评论中提到的那样,您需要确保在循环之外创建points。否则,您将无法在以后访问它以计算完整的GPA。

double points = 0.0;  //this has to go out here to be able to access it later

for(int i = 0; i < gradesArray.length; i++) {
    System.out.println("Enter grade (include + or -) ");
    gradesArray[i] = input.nextLine();

    ...

    else if(gradesArray[i].equalsIgnoreCase("F")) {
        points += 0.0;

    } else {
        System.out.println("Invalid grade");
    }
}
System.out.println("GPA: " + points / gradesArray.length);

对于重置输入,这也很容易。你可以使用while循环 - 所以做这样的事情。

boolean inputNeeded = true;

while(inputNeeded) {
    System.out.println("Please enter grades:);
    String grade = scan.nextLine();
    if(grade_is_valid_check_here) {
        inputNeeded = false;
    } else {
        System.out.println("Input is invalid - please try again");
    }
}

答案 1 :(得分:1)

你也可以做类似的事情。您可以使用开关块。 points需要被带到循环之外,或者每次用户输入成绩时都会重置为0.0。

要让用户重新开始,如果它是无效的等级,您可以使用递归调用。这意味着在&#34;无效成绩&#34;消息,您将调用再次启动该过程的方法。这需要更多的重构,并将一些代码分离为更多方法。

    Scanner input = new Scanner(System.in);
    double points = 0.0;

    System.out.println("How many grades are you putting? ");
    int length = input.nextInt();
    input.nextLine();

    String[] gradesArray = new String[length];

    for(int i = 0; i < gradesArray.length; i++){
        System.out.println("Enter grade (include + or -) ");
        gradesArray[i] = input.nextLine();

        String grade = gradesArray[i].toUpperCase();
        switch (gradesArray[i]) {
            case "A+":
                points += 4;
                break;
            case "A":
                points += 4;
                break;
            case "A-":
                points += 3.7;
                break;
            case "B+":
                points += 3.3;
                break;
            default:
                System.out.println("Invalid grade");
                break;
        }
    }
    System.out.println("GPA: " + points / gradesArray.length);

答案 2 :(得分:1)

除了@Alex K的回答。不确定您是否已了解HashMaps,但您可以通过使用它们来缩短代码。它还可以更轻松地轻松添加其他成绩和分数值。

Scanner input = new Scanner(System. in );

System.out.println("How many grades are you putting? ");
int length = input.nextInt();

String[] gradesArray = new String[length];
//Create a HashMap with String as the key, and a Double as the value
Map < String, Double > grades = new HashMap < > ();
//Insert all the grades and point values
grades.put("A+", 4.0);
grades.put("A-", 3.7);
grades.put("B+", 3.3);
grades.put("B", 3.0);
grades.put("B-", 2.7);
grades.put("C+", 2.3);
grades.put("C", 2.0);
grades.put("D", 1.0);
grades.put("F", 0.0);

double points = 0.0;

for (int i = 0; i < gradesArray.length; i++) {
    System.out.println("Enter grade (include + or -) ");
    String grade = input.nextLine();
    gradesArray[i] = grade;
    //If the grades Map contains the inputted grade, use Map.get(grade) to obtain the value and add that to the points Double. 
    //Otherwise print invalid grade
    if (grades.containsKey(grade)) {
        points += grades.get(gradesArray[i]);
    } else {
        System.out.println("Invalid grade");
    }
}
System.out.println("GPA: " + points / gradesArray.length);

答案 3 :(得分:0)

您只需要将点声明为全局变量,并在for循环外打印GPA

 public class Grade {

public static void main(String[] args){
double points=0.0;
Scanner input = new Scanner(System.in);

System.out.println("How many grades are you putting? ");
int length = input.nextInt();
input.nextLine();

String[] gradesArray = new String[length];

for(int i = 0; i < gradesArray.length; i++)
{
    System.out.println("Enter grade (include + or -) ");
    gradesArray[i] = input.nextLine();


    if(gradesArray[i].equalsIgnoreCase("A+") || gradesArray[i].equalsIgnoreCase("A"))
    {
       points += 4;

    }
    else if(gradesArray[i].equalsIgnoreCase("A-"))
    {
        points+= 3.7;

    }
    else if(gradesArray[i].equalsIgnoreCase("B+"))
    {
        points += 3.3;

    }
    else if(gradesArray[i].equalsIgnoreCase("B"))
    {
        points += 3.0;

    }
    else if(gradesArray[i].equalsIgnoreCase("B-"))
    {
        points += 2.7;

    }
    else if(gradesArray[i].equalsIgnoreCase("C+"))
    {
        points += 2.3;

    }
    else if(gradesArray[i].equalsIgnoreCase("C"))
    {
        points += 2.0;

    }
    else if(gradesArray[i].equalsIgnoreCase("D"))
    {
        points += 1.0;

    }
    else if(gradesArray[i].equalsIgnoreCase("F"))
    {
        points += 0.0;

    }
    else
    {
        System.out.println("Invalid grade");
    }
}
    System.out.println("GPA: " + points / gradesArray.length);
}