Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Can';无法正确显示百分比_Java - Fatal编程技术网

Java Can';无法正确显示百分比

Java Can';无法正确显示百分比,java,Java,我似乎无法正确显示我的百分比。每次循环时,它总是显示0.0。代码中的其他所有内容似乎都能正常工作 我可能错过了一些显而易见的东西,但对于Java,我有点不知所措,因此非常感谢您的帮助 package e2E3; import java.util.Scanner; public class Main { public static void calculatePercentage(int studentMark, int maxMark){ double percent

我似乎无法正确显示我的百分比。每次循环时,它总是显示
0.0
。代码中的其他所有内容似乎都能正常工作

我可能错过了一些显而易见的东西,但对于Java,我有点不知所措,因此非常感谢您的帮助

package e2E3;
import java.util.Scanner;

public class Main {

    public static void calculatePercentage(int studentMark, int maxMark){
        double percentage;
        percentage = studentMark / maxMark * 100;
        System.out.println("%" + percentage);

    }

    public static void totalAndAverageMark(){

            Scanner userInput = new Scanner(System.in);
            int numberOfStudents = 0;
            int maxMark = 0;
            int totalMark = 0;
            int studentCount = 0;
            int studentMark = 0;
            int average = 0;

            System.out.println("how many Students Sat the Test?");
            numberOfStudents = userInput.nextInt();

            System.out.println("what is the Maximum score?");
            maxMark = userInput.nextInt();

            do {

                System.out.println("enter students score..");
                studentMark = userInput.nextInt();

                calculatePercentage(studentMark, maxMark);

                totalMark = totalMark + studentMark;

                studentCount = studentCount + 1;

            } while (studentCount != numberOfStudents);

            average = totalMark / numberOfStudents;
            System.out.println("The Average Grade of Students was.. " + average);   

    }

    public static void main(String[] args) {

        totalAndAverageMark();

    }

}

您正在除以整数,因此结果总是一个整数值。您可以将studentMark声明为double,它将按预期工作。如果你不想这样做,你可以在
(double)studentMark/maxMark*100中将其转换为double