-1

为什么不是“int total = (mark/overall) * 100;” 在职的?当我输入 25 和 50 时,计算 25/50 * 100 后应该是 50%。但它显示的是 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.");
}
}
4

2 回答 2

3

你需要投双:

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

第 15 章开始:

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

于 2013-06-15T14:31:23.823 回答
2

如果您想避免使用浮点数(但为什么?),您还可以对因子重新排序:

int total = 100 * mark / overall;
于 2013-06-15T14:34:37.007 回答