初学者java,帮我修复我的程序吗?
我正在尝试制作一个大学平均绩点计算器。我删掉了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您忘记初始化
等级
。NullPointerException
告诉您grade
为null
。第一次尝试在语句grade[counter] = 4;
中使用grade
时,会引发异常。使用new
分配所需的空间。You forgot to initialize
grade
. TheNullPointerException
is telling you thatgrade
isnull
. The exception is thrown the first time you try to usegrade
, in the statmentgrade[counter] = 4;
. Allocate as much space as you need withnew
.成绩的初始化可以静态也可以动态完成:
或者
对
value
也执行相同的操作。Initialization of grade can be done statically as well dynamically:
or
Do the same for
value
as well.以下是清理代码的一些建议:
letter
,但它绝不会在 while 语句的范围之外使用。您应该在此处声明该变量以及初始值设定项(String letter =gradeObject.next()
)。type name[]
形式声明数组。建议使用type[] name
形式。Here are a few pointers for cleaning up your code:
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()
).type name[]
form is discouraged. It is recommended to use thetype[] name
form instead.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.