java - #帮助。 Java代码,获取5名学生的分数,并使用Arrays方法显示获得A的学生

标签 java arrays netbeans runtimeexception

我有一个问题。我的任务是使用数组方法编写一个 Java 程序,接收 5 个学生的分数,然后查找并显示获得 A 级的学生人数。标记为 (60,56,78,99,92.5)。获得 A 级的标准是 80 分及以上。

我的代码中的一切都很顺利,除了最后一条语句: System.out.println("学生人数"+count);

这是我的代码:

import javax.swing.JOptionPane;

public class Q2 {

 public static void main(String [] args) {

    double[] marks = new double[6];
    int numbers = 1;

    // This is for asking input
    for (int i = 0; i < marks.length; i++,numbers++) {
        String marksString = JOptionPane.showInputDialog (null,
                "Enter the marks for student "+numbers+": ");

        marks[i] = Double.parseDouble(marksString);

        int count = 0;
        if(marks[i] >= 80.0) {
            count++;
        }
    }
    System.out.println("The number of students "+count); 
 }

}

我的代码中的一切都很顺利,除了最后一条语句: System.out.println("学生人数"+count);

我收到一个错误:

Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous tree type:

有谁可以解释并纠正我的错误吗? :D

最佳答案

您在 for 循环中错误地声明了 count。因此,它无法在循环外部访问(因此会出现编译错误),此外,它在循环的每次迭代中都会被覆盖为 0,这意味着它的值始终为 0 或 1(在退出循环之前) ),而不是正确的计数。

将其移到循环之外:

double[] marks = new double[6];
int numbers = 1;
int count = 0;
// This is for asking input
for (int i = 0; i < marks.length; i++,numbers++) {
  String marksString = JOptionPane.showInputDialog (null,
      "Enter the marks for student "+numbers+": ");
  marks[i] = Double.parseDouble(marksString);
  if(marks[i] >= 80.0) {
    count++;
  }
}
System.out.println("The number of students who got A is " + count); 

关于java - #帮助。 Java代码,获取5名学生的分数,并使用Arrays方法显示获得A的学生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44745061/

相关文章:

java - DBUnit自动数据集生成

java - 使用netbeans创建oracle Jet应用程序: Project folder is already NetBeans project (maybe only in memory)

Java 应用程序 - AWT 渲染导致抗锯齿打开时文本损坏

java - 如何使用 Jsonb 库从 Postgres 读取 jsonb

Java 泛型 : capture cannot be applied to Object

c - 如何将文件的内容存储到数组中(C)

javascript - 减少连接不工作?

C - 在函数之间传递数组得到不同的值

java - AWS lambda : How to access S3 bucket from Lambda function using java

java - JSON:解析数组的ArrayNode