簡體   English   中英

不了解如何使我的for循環和if語句起作用

[英]Not understanding how to make my for loop and if statement work

我有這個,但是我對如何打印最終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不能正確打印出來,因為條件匹配等級之后,它會立即打印下來,對嗎? 而且,如果他們輸入了無效的成績,我該怎么辦,這會使用戶重新開始。

你很親密 您需要在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");
    }
}

您也可以執行類似的操作。 您可以使用開關塊。 需要將points帶到循環之外,否則每次用戶輸入成績時它將重置為0.0。

要使用戶重新開始學習(如果評分無效),可以使用遞歸調用。 這意味着在“無效成績”消息之后,您將調用重新開始該過程的方法。 這將需要更多的重構,並將其中一些代碼分成更多的方法。

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

除了@Alex K的答案。 不知道您是否了解HashMaps ,但是可以通過使用它們來縮短代碼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);

您所需要做的就是將點聲明為全局變量,並在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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM