如何计算两个数字的差异

时间:2015-11-18 02:20:37

标签: java

如果我输入让我们说             第一个数字:5             第二个数字:6             它打印出来             总计:0.0总计:1.0             但是,当第一个数字在第二个数字上更大时,它可以正常工作并正确输出。             该程序用于计算两个数字的差异。

if (input.equals("6")) {
    System.out.println("First Number:");
    double num1 = scanner.nextInt();
    System.out.println("Second Number:");
    double num2 = scanner.nextInt();
    double total = 0;
    double total2 = 0;

    if (num1>num2)
    total = ((num1 - num2));
    System.out.print("Total: "+ total);
    playing = false;
    {
    if (num1<num2)
    {
    total2 = ((num2 - num1));
    playing = false;
    System.out.println("Total: "+ total2);
    }

1 个答案:

答案 0 :(得分:1)

正确答案:

if (input.equals("6")) {
        System.out.println("First Number:");
        double num1 = scanner.nextInt();
        System.out.println("Second Number:");
        double num2 = scanner.nextInt();
        double total = 0;
        double total2 = 0;
    if (num1>num2){
        total = ((num1 - num2));
        System.out.print("Total: "+ total);
        playing = false;
    }

    if (num1<num2){
        total2 = ((num2 - num1));
        playing = false;
        System.out.println("Total: "+ total2);