【问题标题】:how to make my allowed inputs stricter如何使我的允许输入更严格
【发布时间】:2016-10-08 01:14:46
【问题描述】:

我的任务是编写一个程序来检查输入字符串是否为有效数字。有效数字是带有或不带有 + 或 - 符号和一位小数的任何数字,例如:(+1 -1 1.0 1.0000000000 -1.0 -1.000000000) 但是当我输入以下内容时:(1.2.3) 它可以工作。我做错了什么

import java.util.Scanner;

public class parseNum {

  public static void main(String[] args) {
    System.out.println("Please enter a number:");
    Scanner input = new Scanner(System.in);
    String inStr = input.nextLine();
    int i;
    String state = "start";

    for (i=0; i<inStr.length(); i++) {
      if (state.equals("start")) {
        if (inStr.charAt(i) == '+' || inStr.charAt(i) == '-') {
          state = "afterSign";
          continue;
        }
      }
      // i assume this is where i messed up
      if(inStr.charAt(i) == ('.')){
        if(inStr.charAt(i+1) >= '0' && inStr.charAt(i+1) <= '9'){
        state = "accept";
        continue;
        }
        else{
        state = "reject";
        break;
        }
      }

      if (inStr.charAt(i) >= '0' && inStr.charAt(i) <= '9') {
        state = "accept";
        continue;
      }
      else {
        state = "reject";
        break;
      }
    }

    if (state.equals("accept"))
      System.out.println("Thank you");
    else
      System.out.println("Invalid input");

    input.close();
  }

}

【问题讨论】:

    标签: java string loops if-statement


    【解决方案1】:

    在你的 for 循环之上声明一个 int 来计算点数,

    int dotCount = 0;
    

    接下来,每次找到一个点时计数

    if(inStr.charAt(i) == ('.')){
                dotCount++; //THIS LINE
                if(inStr.charAt(i+1) >= '0' && inStr.charAt(i+1) <= '9'){
                    state = "accept";
                    continue;
                }
                else{
                    state = "reject";
                    break;
                }
            }
    

    最后,添加一个 if 语句,如果找到超过 1 则拒绝。

       if(dotCount > 1) {
                state = "reject";
                break;
            }
    

    【讨论】:

      【解决方案2】:

      见 cmets:

      import java.util.Scanner;
      
      public class ParseNum {//use java naming conventions 
      
          public static void main(String[] args) {
      
              System.out.println("Please enter a number:");
              Scanner input = new Scanner(System.in);
              String inStr = input.nextLine();
              int i =0;
              String state = "start";
              int dotCount = 0;
      
              //check for +/- outside the loop. It should only be 
              //at the beginning. This will cause number with multiple 
              //+/- signs to fail 
              if (inStr.charAt(0) == '+' || inStr.charAt(0) == '-') {
                  state = "afterSign";
                  i=1; 
              }
      
              for (i=0; i<inStr.length(); i++) {
      
                  if(inStr.charAt(i) == ('.')){
                      if(dotCount == 0){
                          state = "accept";
                          dotCount++;
                          continue;
                      }
                      else{//multiple dots fond 
                          state = "reject"; 
                          break;
                      }
                  }
      
                  if (inStr.charAt(i) >= '0' && inStr.charAt(i) <= '9') {
                      state = "accept";
                      continue;
                  }
                  else {
                      state = "reject";
                      break;
                  }
              }
      
              if (state.equals("accept"))
                  System.out.println("Thank you");
              else
                  System.out.println("Invalid input");
      
              input.close();
          }
      }
      

      请注意,像 +000009.8 这样带有多个 0 的数字被认为是有效的。
      一个更简单更好的实现是:

          public static void main(String[] args) {
      
              System.out.println("Please enter a number:");
              Scanner input = new Scanner(System.in);
              String inStr = input.nextLine();
              String state = "start";
      
              try {
      
                  Double d = Double.valueOf(inStr);
                  state = "accept";
              } catch (NumberFormatException ex) {
      
                  state = "reject";
              }
      
              if (state.equals("accept")) {
                  System.out.println("Thank you");
              } else {
                  System.out.println("Invalid input");
              }
      
              input.close();
          }
      

      【讨论】:

        猜你喜欢
        • 2010-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-21
        相关资源
        最近更新 更多