Java无法识别计算?

时间:2013-12-02 22:01:50

标签: java

我是Java的初学者并失去了情节。有人可以帮助我,我只是想让这个代码工作。一切都运行良好,除了最后的计算。我复制并粘贴了代码和输出。

import java.util.Scanner;
public class newfile {
public static void main(String[]args) { // starting point of the program
    Scanner input = new Scanner(System.in);
    int oneA, oneB, oneC;
    double finA=0, finB=0, finC=0,average=0;

    System.out.println("Hello, Welcome to the 'Calculate Your Grades' program!\nPlease input your predicted grade for Assignment 1A in a percentage format:");
    oneA=input.nextInt();
    System.out.println("Thank you, can you please input your predictive grade for Assignment 1B?");
    oneB=input.nextInt();
    System.out.println("Finally, can you please input your predictive grade for Assignment 1C?");
    oneC=input.nextInt();
    average=(oneA+oneB+oneC)/3;
    System.out.println("Excellent, Thank you. The program has calculated your average grade to be "+average+"%");
    finA=(oneA/100)*25;
    finB=(oneB/100)*25;
    finC=(oneC/100)*50;
    System.out.println("This module states:\n Assignment 1A is weighted at 25%. You achieved "+finA+"%\n Assignment 1B is weighted at 25%. You achieved "+finB+"%\n Assignment 1C is weighted at 50%. You achieved "+finC+"%");
    }       
}

输出:

--------------------Configuration: newfile - JDK version 1.7.0_45 <Default> - <Default>--------------------

Hello, Welcome to the 'Calculate Your Grades' program!
Please input your predicted grade for Assignment 1A in a percentage format:
60
Thank you, can you please input your predictive grade for Assignment 1B?
60
Finally, can you please input your predictive grade for Assignment 1C?
60
Excellent, Thank you. The program has calculated your average grade to be 60.0%
This module states:
Assignment 1A is weighted at 25%. You achieved 0.0%
Assignment 1B is weighted at 25%. You achieved 0.0%
Assignment 1C is weighted at 50%. You achieved 0.0%

流程已完成。

构建报告很清楚。

3 个答案:

答案 0 :(得分:7)

average=(oneA+oneB+oneC)/3;

此处相同:

finA=(oneA/100)*25;
finB=(oneB/100)*25;
finC=(oneC/100)*50;

  

在计算机程序中划分整数需要特别小心。一些   编程语言,处理整数除法(即给出   整数商作为答案)。所以答案是整数。

你正在执行整数除法。 因此:

60/100 => 0 

你可以通过将一个参数设为double来避免这种情况:

finA=(oneA/100.0)*25;
finB=(oneB/100.0)*25;
finC=(oneC/100.0)*50;

答案 1 :(得分:2)

在java中分割两个整数时,默认行为是返回商并截断余数(或十进制)部分。所以3/2 == 1而不是1.5。当你在一个整数和一个double(或float)之间进行除法时,首先将整数转换为double(或float),然后完成浮点除法,返回double(或float)。

在代码中,转换为double是作为变量赋值的一部分完成的,因为算术表达式中的所有值都是整数。这意味着实际表达式(oneA/100)*25作为整数运算执行。除oneA < 100之外,除法的商为0,这意味着乘法只是0*25

正如ZouZou所说,你可以通过将100更改为100.0来将除法更改为浮点,这意味着编译器会将它们视为双精度而不是整数,这使得除法将oneA转换为双精度。

答案 2 :(得分:0)

这是您的固定代码。

    import java.util.Scanner;

public class newfile {
    public static void main(String[] args) { // starting point of the program
        Scanner input = new Scanner(System.in);
        int oneA, oneB, oneC;
        double finA = 0, finB = 0, finC = 0, average = 0;

        System.out.println("Hello, Welcome to the 'Calculate Your Grades' program!\nPlease input your predicted grade for Assignment 1A in a percentage format:");
        oneA = input.nextInt();
        System.out.println("Thank you, can you please input your predictive grade for Assignment 1B?");
        oneB = input.nextInt();
        System.out.println("Finally, can you please input your predictive grade for Assignment 1C?");
        oneC = input.nextInt();
        average = (oneA + oneB + oneC) / 3;
        System.out.println("Excellent, Thank you. The program has calculated your average grade to be " + average + "%");
        finA = (oneA / 100.0) * 25;
        finB = (oneB / 100.0) * 25;
        finC = (oneC / 100.0) * 50;
        System.out.println("This module states:\n Assignment 1A is weighted at 25%. You achieved " + finA + "%\n Assignment 1B is weighted at 25%. You achieved " + finB
                + "%\n Assignment 1C is weighted at 50%. You achieved " + finC + "%");
    }
}