为什么我的Java代码不起作用(初学者,数组)

huangapple go评论55阅读模式
英文:

Why is my Java code not working (beginner, array)

问题

感谢您的关注。我卡在了为什么这个代码没有处理输入成绩的情况。它可以正确处理 A+(返回 4.0),但是 'A' 报错,'A-' 返回 4.0(应该是 3.7),然后 'B+' 正确,'B' 报错,'B-' 返回 3.0(应该是 2.7),这个模式一直持续下去。有什么想法吗?

public class Grade {
    private String grade = "";
    public Grade(String grade) {
        this.grade = grade;
    }
    public double getGrade() {
        double gpa = 0.0;
        char[] gradeArray = this.grade.toCharArray();
        if (gradeArray[0] == 'A') {
            gpa += 4.0;
        }
        if (gradeArray[0] == 'B') {
            gpa += 3.0;
        }
        if (gradeArray[0] == 'C') {
            gpa += 2.0;
        }
        if (gradeArray[0] == 'D') {
            gpa += 1.0;
        }        
        if (gradeArray[1] == '+') {
            if (gradeArray[0] != 'A') {
                gpa += 0.3;
            }
        }
        if (gradeArray[1] == '-') {
            gpa -= 0.3;
        }
        return gpa;
    }
}
英文:

thanks for looking. I'm stuck on why this is not handling cases for inputted grades. It handles A+ fine (returns 4.0) but 'A' gives an error, 'A-' gives 4.0 (should be 3.7), then 'B+' is correct, 'B' gives error, 'B-' gives 3.0 (should be 2.7), and this is the pattern. Any thoughts?

public class Grade {
    private String grade = "";
    public Grade(String grade) {
        this.grade = grade;
    public double getgrade() {
        double gpa = 0.0;
        char[] gradeArray = this.grade.toCharArray();
        if (gradeArray[0] == 'A') {
            gpa += 4.0;
        }
        if (gradeArray[0] == 'B') {
            gpa += 3.0;
        }
        if (gradeArray[0] == 'C') {
            gpa += 2.0;
        }
        if (gradeArray[0] == 'D') {
            gpa += 1.0;
        }        
        if (gradeArray[1] == '+') {
            if (gradeArray[0] != 'A') {
                gpa += 0.3;
            }
        }
        if (gradeArray[0] == '-') {
            gpa -= 0.3;
        }
        return gpa;
    }
}```

</details>


# 答案1
**得分**: 1

如果你调用A或者B,例如:

    new Grade("A").getNumericGrade()

你会在以下代码的这一行得到java.lang.IndexOutOfBoundsException错误:

    if (gradeArray[1] == '+') {

因为gradeArray[1]并不存在,这里只有一个字符(在索引0处),它可以是'A'或者'B'。

简单的解决方案:检查长度!例如:

    if (gradeArray.length > 1 && gradeArray[1] == '+') {

<details>
<summary>英文:</summary>

well if you call A or B, e.g.:

    new Grade(&quot;A&quot;).getNumericGrade()

you get java.lang.IndexOutOfBoundsException on line 

    if (gradeArray[1] == &#39;+&#39;) {

because gradeArray[1] does not exist, there is only 1 character (at index 0) it is &#39;A&#39; or &#39;B&#39;

Simple solution: check for length! E.g.:

    if (gradeArray.length &gt; 1 &amp;&amp; gradeArray[1] == &#39;+&#39;) {

</details>



# 答案2
**得分**: 0

```java
public class Grade {

    private String grade = "";

    public Grade(String grade) {
        this.grade = grade;
    }

    public String getLetterGrade() {
        return this.grade;
    }

    public double getNumericGrade() {
        double gpa = 0.0;
        char[] gradeArray = this.grade.toCharArray();

        // 始终良好地进行空值/空字符串检查
        if (gradeArray.length == 0 || gradeArray[0] == ' '){
            return 0;
        }

        // 使用switch语句可以提高代码的可读性。也可以使用if语句
        // 考虑到您最初的情况,这将使您最后的代码块变得多余
        switch (gradeArray[0]){
            case 'A': gpa += 4.0; break;
            case 'B': gpa += 3.0; break;
            case 'C': gpa += 2.0; break;
            case 'D': gpa += 1.0; break;
            default: break;
        }

        // 首先需要检查是否存在第二个字符
        // 如果存在,那么它只能是+/-符号
        if(gradeArray.length == 2 && (gradeArray[1] == '+' || gradeArray[1] == '-')){
            if (gradeArray[1] == '+') {
                if (gradeArray[0] != 'A') {
                    gpa += 0.3;
                }
            }
            // '-'应该是第二个字符,所以索引应为1
            if (gradeArray[1] == '-') {
                gpa -= 0.3;
            }
        } // 还需要包含else以处理其他情况

        return gpa;
    }
}
英文:

public class Grade {

private String grade = &quot;&quot;;

public Grade(String grade) {
    this.grade = grade;
}

public String getLetterGrade() {
    return this.grade;
}

public double getNumericGrade() {
    double gpa = 0.0;
    char[] gradeArray = this.grade.toCharArray();

    // Good to have null/empty check always
    if ( gradeArray.length == 0  || gradeArray[0] == &#39; &#39;){
        return 0;
    }

    // Code readability increases manifolds with switch. if clause can also be used
    // Considering only the cases you originally had. With this your code block at last becomes redundant
    switch ( gradeArray[0] ){
        case &#39;A&#39;: gpa += 4.0; break;
        case &#39;B&#39;: gpa += 3.0; break;
        case &#39;C&#39;: gpa += 2.0; break;
        case &#39;D&#39;: gpa += 1.0; break;
        default: break;
    }

    // First you need to check if there is a second character present or not
    // If present then it should be only +/-
    if( gradeArray.length == 2 &amp;&amp; ( gradeArray[1] == &#39;+&#39; || gradeArray[1] == &#39;-&#39; )){
        if (gradeArray[1] == &#39;+&#39;) {
            if (gradeArray[0] != &#39;A&#39;) {
                gpa += 0.3;
            }
        }
        // &#39;-&#39; would be second character, so index should be 1
        if (gradeArray[1] == &#39;-&#39;) {
            gpa -= 0.3;
        }
    } //Include else to handle other scenarios

    return gpa;
}

}

huangapple
  • 本文由 发表于 2020年10月1日 03:41:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/64144656.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定