如何计算 Java 学生的平均百分比?

如何解决如何计算 Java 学生的平均百分比?

我编写了一个小型 Java 程序来计算学生考试的百分比。我昨天刚开始用 Java 编程,所以遇到了一个错误代码如下:

public class Userinput {
public static void main(String[] args) {
    System.out.println("Hi dear Student. This is a percentage calculator.");
    Scanner sc = new Scanner(system.in);
    System.out.println("How many Subjects do you have?");
    int totalSubjects = sc.nextInt();
    System.out.println("Out of how many marks did you have your exams/tests?");
    int outOf = sc.nextInt();

    int actualOutof = outOf;
    int markSubjects1 = 0;
    int markSubjects2 = 0;
    int markSubjects3 = 0;
    int markSubjects4 = 0;
    int markSubjects5 = 0;
    int markSubjects6 = 0;
    int markSubjects7 = 0;
    int markSubjects8 = 0;
    int markSubjects9 = 0;
    int markSubjects10 = 0;
    int i = 0;
    int totalSubjectsActual = totalSubjects + 1;

    for (i=1; i<totalSubjectsActual; i++) {
        System.out.println("How many marks did you score in the next subject?");

        if (i == 1) {
            markSubjects1 = sc.nextInt();
        } else if (i == 2) {
            markSubjects2 = sc.nextInt();
        } else if (i == 3) {
            markSubjects3 = sc.nextInt();
        } else if (i == 4) {
            markSubjects4 = sc.nextInt();
        } else if (i == 5) {
            markSubjects5 = sc.nextInt();
        } else if (i == 6) {
            markSubjects6 = sc.nextInt();
        } else if (i == 7) {
            markSubjects7 = sc.nextInt();
        } else if (i == 8) {
            markSubjects8 = sc.nextInt();
        } else if (i == 9) {
            markSubjects9 = sc.nextInt();
        } else if (i == 10) {
            markSubjects10 = sc.nextInt();
        }
    }
    int outOfMarks = actualOutof*totalSubjectsActual;
    int startAverage = markSubjects1 + markSubjects2 + markSubjects3 + markSubjects4 + markSubjects5 + markSubjects6 + markSubjects7 + markSubjects8;
    int decimalMarks = startAverage/outOfMarks;
    int average = decimalMarks*100;

    System.out.println("Average Percentage for all Subjects: " + average + "%");
}};

所有代码都运行良好,除了最后一部分。当我尝试根据结果计算百分比时,它会显示 0 或 100。请帮我解决这个问题...

解决方法

原因是,您使用 int 来计算百分比。

将最后四个变量从 int 更改为 double 或 float。

Int 不能显示小数。例如,如果您将 0.24 作为值,它将减少 0.24 并且只显示 0

您还可以使用其他一些东西来优化您的程序。尝试查看数组。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?