我的'别人'陈述有什么问题?

时间:2015-09-16 05:07:10

标签: java if-statement

运行程序时,我收到以下错误:

输入一个数字: 五 您的原始号码是5.0 错误 输入不是数字。

我尝试了所有我能想到的东西,但我很难过。以下是代码段

import java.util.Scanner;

public class NegativeNumberConversion {

    public static void main(String[] args) {
        // Convert negative numbers to positive and display back to user

        // Create a console object for Scanner class
        Scanner input = new Scanner (System.in);

        // Declare variable
        double numberOne = 0;

        // Prompt user to enter number
        System.out.println("Enter a number: ");

        // Read in the number
        numberOne = input.nextDouble();

        // If the number is positive - display it
        if (numberOne > 0){

        // Display it - Explain that it is the original number
        System.out.println("Your original number is " + numberOne);

        }else  {

        // Report the error
        System.out.println("***ERROR*** The input is not a number.");

        // Terminate the error
        System.exit(1);

    }

        // If the number negative - convert it to positive - 
        if(numberOne < 0){


        // Display it - Explain that it was converted
        System.out.println("Your number was converted to " + (-1*numberOne) + " because it was negative.");

        }else{
        // Report the error
        System.out.println("***ERROR*** The input is not a number.");

        // Terminate the error
        System.exit(1);
    }

        // Close input
        input.close();
    }
}

3 个答案:

答案 0 :(得分:6)

问题在于if语句的设置方式

要解决此问题,请使用else if包含第二种情况。

if (numberOne > 0) {
    System.out.println("Your original number is " + numberOne);
}
else if (numberOne < 0) {
    System.out.println("Your number was converted to " + (-1 * numberOne) + " because it was negative.");
}
else {
    // Report the error
    System.out.println("***ERROR*** The input is not a number.");
    // Terminate the error
    System.exit(1);
}

编辑:帮助您了解为什么它不起作用(而不仅仅是显示解决方案)。当某人输入一个输入时,无论它是正面的还是负面的,如果比较if语句,并且因为一个数字不能同时存在,那么else语句之一将始终运行。

答案 1 :(得分:1)

错误在这里,当输入5时,程序将执行else语句

 // If the number negative - convert it to positive - 
    if(numberOne < 0){


    // Display it - Explain that it was converted
    System.out.println("Your number was converted to " + (-1*numberOne) + " because it was negative.");

    }else{
    // Report the error
    System.out.println("***ERROR*** The input is not a number.");

    // Terminate the error
    System.exit(1);
}

也许以下代码就是你想要的。

try {
    numberOne = input.nextDouble();
    if (numberOne > 0) {
        // Display it - Explain that it is the original number
        System.out.println("Your original number is " + numberOne);
    } else if (numberOne < 0) {
        // Display it - Explain that it was converted
        System.out.println("Your number was converted to " + (-1 * numberOne) + " because it was negative.");
    }
} catch (Exception ex) {
    System.out.println("***ERROR*** The input is not a number.");
    System.exit(1);
}

希望有所帮助。

答案 2 :(得分:1)

试试这个;

public class NegativeNumberConversion {

public static void main(String[] args) {
    // Convert negative numbers to positive and display back to user
    // Create a console object for Scanner class
    Scanner input = new Scanner(System.in);

    // Declare variable
    double numberOne = 0;

    // Prompt user to enter number
    System.out.println("Enter a number: ");

    // Read in the number
    numberOne = input.nextDouble();

    // If the number is positive - display it
    if (numberOne > 0) {
        // Display it - Explain that it is the original number
        System.out.println("Your original number is " + numberOne);
    } 

    // If the number negative - convert it to positive -
    else if (numberOne < 0) {
        // Display it - Explain that it was converted
        System.out.println("Your number was converted to " + (-1 * numberOne) + " because it was negative.");
    } else {
        // Report the error
        System.out.println("***ERROR*** The input is not a number.");
        // Terminate the error
        System.exit(1);
    }

    // Close input
    input.close();
}