简单的数学不能按预期显示?

时间:2013-06-15 14:30:31

标签: java math

为什么不是“int total =(mark / overall)* 100;”工作?当我输入25和50时它应该是50%,它计算25/50 * 100.但它显示0%而不是很奇怪。有什么建议??

    public class MarksCalculator {
        private static Scanner kb = new Scanner(System.in);

        public static void main (String[]args) {

            int mark = 0, overall = 0;

            //System.out.print("Hello Deepo!  To calculate the marks enter value (percantage) of the task: ");
            //int weight = kb.nextInt();

            System.out.print("Enter your marks for this Task/Assesment/Test: ");
            mark = kb.nextInt();

            System.out.print("What was this mark out of: ");
            overall = kb.nextInt();

            int total = (mark/overall) * 100;

            System.out.println("You have scored " + total + "% with this assesment.");
}
}

2 个答案:

答案 0 :(得分:3)

你需要投两次:

double total = ((double)mark/overall) * 100;

来自Chapter 15

  

如果提升的类型是int或long,则整数算术是   进行。

答案 1 :(得分:2)

如果你想避免浮点(但为什么?),你也可以重新排序因子:

int total = 100 * mark / overall;