初学者java,帮我修复我的程序吗?

发布于 2024-11-29 03:49:49 字数 1142 浏览 0 评论 0原文

我正在尝试制作一个大学平均绩点计算器。我删掉了 20 个 if 语句,只说明每个字母的等级。我为再次关注这个问题的人修复了我的第一个程序。该程序现在可以运行,但无论我在 gpa 中输入什么字母,它返回的都是 2.0 。如果有人发现任何问题,我们将非常感激......再次。谢谢

import java.util.Scanner;

public class universityGPA {
    public static void main(String args[]){

        int classes = 4;
        int units[] = {3, 2, 4, 4};
        double[] grade = new double[4];
        double[] value= new double[4];
        int counter = 0;
        double total = 0;
        double gpa;
        String letter;


        while(classes > counter){
            Scanner gradeObject = new Scanner(System.in);
             letter = gradeObject.next();

            if(letter.equalsIgnoreCase("A+") || letter.equalsIgnoreCase("A")){
                grade[counter] = 4;
            }
            if(letter.equalsIgnoreCase("F")){
                grade[counter] = 0;
            }

            value[counter] = grade[counter] * units[counter];
            counter++;
        }

        for(int i = 0; i < classes; i++  ){
            total += value[i];
        }

        gpa = total/classes;
        System.out.println("You gpa is " +gpa);
    }
}

I am trying to make a calculator for college gpa's. I cut out all like 20 if statements that just say what each letter grade is. I fixed my first program for anybody looking at this again. The program now works, but regardless of the letters i type in the gpa it returns is a 2.0 . If anybody sees anything wrong it would be very much appreciated...again. Thanks

import java.util.Scanner;

public class universityGPA {
    public static void main(String args[]){

        int classes = 4;
        int units[] = {3, 2, 4, 4};
        double[] grade = new double[4];
        double[] value= new double[4];
        int counter = 0;
        double total = 0;
        double gpa;
        String letter;


        while(classes > counter){
            Scanner gradeObject = new Scanner(System.in);
             letter = gradeObject.next();

            if(letter.equalsIgnoreCase("A+") || letter.equalsIgnoreCase("A")){
                grade[counter] = 4;
            }
            if(letter.equalsIgnoreCase("F")){
                grade[counter] = 0;
            }

            value[counter] = grade[counter] * units[counter];
            counter++;
        }

        for(int i = 0; i < classes; i++  ){
            total += value[i];
        }

        gpa = total/classes;
        System.out.println("You gpa is " +gpa);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

帝王念 2024-12-06 03:49:49

您忘记初始化等级NullPointerException 告诉您 gradenull。第一次尝试在语句 grade[counter] = 4; 中使用 grade 时,会引发异常。使用 new 分配所需的空间。

You forgot to initialize grade. The NullPointerException is telling you that grade is null. The exception is thrown the first time you try to use grade, in the statment grade[counter] = 4;. Allocate as much space as you need with new.

情魔剑神 2024-12-06 03:49:49

成绩的初始化可以静态也可以动态完成:

double []grade = new double[4];

或者

double []grade = new double[classes];

value 也执行相同的操作。

Initialization of grade can be done statically as well dynamically:

double []grade = new double[4];

or

double []grade = new double[classes];

Do the same for value as well.

把时间冻结 2024-12-06 03:49:49

以下是清理代码的一些建议:

  • 尝试与格式更加一致。确保所有内容都正确缩进,并且在行的开头或结尾处没有残留空格(第 18 行)。
  • 您应该在尽可能靠近第一个使用变量的地方声明变量。这不仅使您的代码更具可读性,还最大限度地减少了范围。例如,在第 18 行,您初始化了 letter,但它绝不会在 while 语句的范围之外使用。您应该在此处声明该变量以及初始值设定项(String letter =gradeObject.next())。
  • 不鼓励以 type name[] 形式声明数组。建议使用type[] name 形式。
  • 尝试将您的程序分成不同的部分。例如,对于这个程序,您可以清楚地看到涉及几个步骤。也就是说,您首先必须获取一些输入,然后解析它,然后计算返回值。这些部分可以分解为单独的方法来清理代码并促进重用。虽然对于这样一个简单的程序来说,它似乎不会产生很多好处,但一旦你开始解决更大的问题,这个组织将是绝对强制性的。

Here are a few pointers for cleaning up your code:

  • Try to be more consistent with your formatting. Make sure everything is properly indented and that you don't have lingering spaces at the beginnings or endings of lines (line 18).
  • You should declare variables as close to the first spot you use them as possible. This, along with making your code much more readable, minimizes the scope. For instance, on line 18, you initialize letter, but it is never used outside the scope of the while statement. You should declare the variable right there, along with the initializer (String letter = gradeObject.next()).
  • Declaring arrays in the type name[] form is discouraged. It is recommended to use the type[] name form instead.
  • Try to separate your program into distinguished sections. For instance, for this program, you can clearly see a few steps are involved. Namely, you first must grab some input, then parse it, then calculate the return value. These sections can be factored out into separate methods to clean up the code and promote reuse. While it may not seem to yield many benefits for such a simple program, once you start working on larger problems this organization will be absolutely mandatory.
傲鸠 2024-12-06 03:49:49

NullPointerException 意味着您正在尝试访问不存在的内容。

由于您的 Grade[] 为空,因此在第 21 行通过 Grade[counter] 访问它实际上意味着您正在访问尚未创建的内容。

您需要初始化该数组,因此它实际上有一个实例。

NullPointerException means you are trying to access something that does not exist.

Since your grade[] is null, accessing it on line 21 by grade[counter] actually means you are accessing something that has yet to be created.

You need to initialize the array, so it actually has an instance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文