首页 > 解决方案 > 为什么 IF 语句不适用于舍入浮点数和双精度数?

问题描述

我正在尝试检查用户是否输入了 3.5 和 7 之间的双精度。我尝试使用实际的双精度和Math.round();,以及StrictMath.round()。我也尝试解析作为浮点数输入的字符串,但它没有改变任何东西。这是我使用的基本代码:

import java.util.Scanner;

public class IfStatement {
    public static void main(String[] args) {
        //create a Scanner
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first double: ");
        double number = input.nextDouble();
        if (isDouble(number)==true) {
            double x = Double.parseDouble(number);
            if (3.5 <= x <= 7) {
                System.out.println("good");
            } else {
                System.out.println("incorrect input");
            }
        } else {
           System.out.println("incorroct input");
        }
    }
    public static booleen isDouble(String str) {
        if (str == null) {
            return false;
        }
        int length = str.length();
        if (length == 0) {
            return false;
        }
        int i = 0;
        if (str.charAt(0) == '-') {
            if (length == 1) {
                return false;
            }
            ++i;
        }
        int integerPartSize = 0;
        int exponentPartSize = -1;
        while (i < length) {
            char c = str.charAt(i);
            if (c < '0' || c > '9') {
                if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
                    exponentPartSize = 0;
                } else {
                    return false;
                }
            } else if (exponentPartSize > -1) {
                ++exponentPartSize;
            } else {
                ++integerPartSize;
            }
            ++i;
        }
        if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1)
                || exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
            return false;
        }
        return true;
    }
}

我也尝试在 if 语句中制作 3.5 3 但没有骰子。我只需要一个松散的解释,而不是一个完整的解决方案。

标签: javaif-statementdouble

解决方案


我已经编辑了你的代码。

  1. boolean拼写
  2. isDouble()接受一个字符串
  3. a <= x <= bJava 中不允许使用链式表达式

还,

if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1) || exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
            return false;
}
return true;

可以简化为

return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
                && exponentPartSize != 0 && (str.charAt(length - 1) != '.');

这是完整的代码。

class Test {
    public static boolean isDouble(String str) {
        if (str == null) {
            return false;
        }
        int length = str.length();
        if (length == 0) {
            return false;
        }
        int i = 0;
        if (str.charAt(0) == '-') {
            if (length == 1) {
                return false;
            }
            ++i;
        }
        int integerPartSize = 0;
        int exponentPartSize = -1;
        while (i < length) {
            char c = str.charAt(i);
            if (c < '0' || c > '9') {
                if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
                    exponentPartSize = 0;
                } else {
                    return false;
                }
            } else if (exponentPartSize > -1) {
                ++exponentPartSize;
            } else {
                ++integerPartSize;
            }
            ++i;
        }
        return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
                && exponentPartSize != 0 && (str.charAt(length - 1) != '.');
    }

    public static void main(String[] args) {
        //create a Scanner
        Scanner input = new Scanner(System.in);
        System.out.print("Enter first double: ");
        String number = input.nextLine();
        if (isDouble(number)) {
            double x = Double.parseDouble(number);
            if (3.5 <= x && x <= 7) {
                System.out.println("good");
            } else {
                System.out.println("incorrect input");
            }
        } else {
            System.out.println("incorroct input");
        }
    }
}

推荐阅读