有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何检查双精度值是否包含特殊字符

我在为一个程序编写代码,在这个程序中,用户输入2到4个数字,最长可达2位小数。但是,在用户输入这些数字之前,他们必须输入英镑符号,例如#12.34。我想知道如何检查输入的双精度值是否以磅符号开头,如果用户忘记输入,如何重新提示他们再次输入。到目前为止,我使用的是字符串值和'。startsWith()的代码,但我后来发现字符串值使程序的其余部分无法进行编码,所以我想将其保留为双精度值。这是我目前拥有的代码,但我希望更改为双代码:

String input;
System.out.print("Enter the 4 numbers with a pound key: ");
input = keyboard.next();
while (!input.startsWith("#")) {
 System.out.print("Re-enter the 4 numbers with a pound key: ");
 input = keyboard.next();
}

如前所述,我想用double替换字符串


共 (4) 个答案

  1. # 1 楼答案

    你应该使用startsWith方法。我不知道你为什么这么说

    I'm finding later on that a String value is making the rest of the program impossible to code

    基本上,您希望反复提示它,直到用户输入#符号。那么为什么不使用while循环呢

    System.out.println("Enter a number:");
    String s = keyboard.next();
    double d = 0.0; // You actually wnat to store the user input in a
                    // variable, right?
    while (!s.trim().startWith("#")) {
        System.out.println("You did not enter the number in the correct format. Try again.");
        s = keyboard.next();
        try {
            d = Double.parseDouble(s.substring(1));
        } catch (NumberFormatException ex) {
             continue;
        } catch (IndexOutOfBoundsException ex) {
            continue;
        }
    }
    

    这是很多代码

    说明:

    首先,它提示用户输入一个数字并将输入存储在s。这很容易理解。现在困难的部分来了。我们希望循环,直到用户输入正确格式的输入。让我们看看有哪些类型的无效输入:

    • 用户未输入#符号
    • 用户没有输入数字

    第一个由startsWith方法处理。如果输入不是以#开头,请再次询问用户。在此之前,首先调用trim来删除输入中的空白,以便像" #5.90"这样的输入是有效的。第二部分由本部分处理:

        try {
            d = Double.parseDouble(s.substring(1));
        } catch (NumberFormatException ex) {
             continue;
        } catch (IndexOutOfBoundsException ex) {
            continue;
        }
    

    如果输入的格式错误,请再次询问用户。如果用户输入长度小于1的字符串,请再次询问用户

    “但是等等!为什么要调用substring方法?”你问。如果用户在前面输入一个#,然后输入正确的数字格式,您将如何将该字符串转换为双精度?在这里,我只是通过调用substring来修剪第一个字符

  2. # 2 楼答案

    String input;
    System.out.print("Enter the 4 numbers with a pound key: ");
    input = keyboard.next();
    while (!input.startsWith("#")) {
     System.out.print("Re-enter the 4 numbers with a pound key: ");
     input = keyboard.next();
    }
    // if your excution reaches here. then it means the values entered by user is starting from '#'.
    String temp = input;
    double value = Double.parseDouble(temp.replace("#",""));
    

    对于程序的其余部分,请使用value。我认为现在应该可以进行编码了

  3. # 3 楼答案

    您可以尝试这样做,删除所有不是数字或点的字符:

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input;
        System.out.println("Input a number: ");
        while (!(input = br.readLine()).equals("quit")) {
            Double parsedDouble;
            if (input.matches("#[0-9]+\\.[0-9]{1,2}")) {
                parsedDouble = Double.parseDouble(input.replaceAll("[^0-9.]+", ""));
                System.out.println("double: " + parsedDouble);
                System.out.println("Input another number: ");
            } else {
                System.out.println("Invalid. Try again. Example: #10.10");
            }
        }
    }
    
  4. # 4 楼答案

    double值没有货币符号。您应该在执行操作时检查货币符号,并在解析double之前将其删除

    String input;
    System.out.print("Enter the 4 numbers with a pound key: ");
    input = keyboard.next();
    while (!input.startsWith("£")) {
       System.out.print("Re-enter the 4 numbers with a pound key: ");
       input = keyboard.next();
    }
    doulble number = Double.parseDouble(input.substring(1));