2017-10-10 107 views
0

所以這個項目的目的是驗證,如果用戶輸入數字0-9或有小數。它會驗證它是否是數字或字母,但如果用戶沒有放置循環仍然運行的任何東西,就像它是有效的數字一樣。ascii表格計數器與Java不返回爲空虛假

所以,錯誤一:用戶輸入一個有效的浮點數前輸入並且腳本仍然運行有效。

錯誤二:用戶輸入一段時間而不輸入任何數字,腳本仍然運行有效。

package numbers; 

/* 
* Name: Joshua Trimm 
* Date: 9-24-17 
* class: CISP 1010 
* Project: IsAValidNumber 
*/ 

import java.util.Scanner; 

public class IsAValidNumber 
{ 




    public static void main(String[] args) 
    { 

     //console input scanner 
     Scanner consoleInput = new Scanner(System.in); 


     //Prompt user for input 
     System.out.print("enter a valid number:"); 
     String validat = consoleInput.nextLine(); 
     //define boolean from method 
     boolean isTrue = NumberIsValid(validat); 
     //while loop 
     while(isTrue == true) 
     { 


      System.out.println(validat + " is a valid number. Please enter another value:"); 
      validat = consoleInput.nextLine(); 
      isTrue = NumberIsValid(validat); 


     } 
     //if it jumps out of the while because it is false run this 
     System.out.println(validat + " is not a valid number, bye"); 
     consoleInput.close(); 
    } 

    //setup a boolean method 
    public static boolean NumberIsValid(String value) 
    { 

     //define local variables 
     int period = 0; 
     boolean valid = true; 
     int length = value.length(); 


     //run through the string 

     //check the numbers 

     for(int i = 0; i < length; i++) 
     { 
      char aChar = value.charAt(i); 
      // make sure it doesn't start with a space 
      if((int)aChar == 20) 
      { 
       valid =false; 
       break; 
      } 

      // check to make sure its is a int 0-9 
      else if((int)aChar < 48 || (int)aChar > 57) 
      { 

       if((int)aChar == 46) 
       { 
        period++; 
       } 
       else 
       { 
        valid = false; 
        break; 
       } 

      } 

      //make sure it doesn't have more than one period 

      if (period > 1) 
      { 
       valid = false; 
       break; 
      } 

     } 


     return valid; 

    } 
} 

任何幫助將不勝感激!先謝謝了。

回答

0

對於bug#1:在您的方法NumberIsValid(String value)中,您從不檢查參數是否爲空。當一個空參數傳遞給你的方法時,for循環會被跳過並返回true。

了BUG#2:不是最好的解決辦法,但你可以簡單地創建這樣的事情:

if (value.contains(".") && length == 1) { 
     return false 
    } 
+0

謝謝!錯誤#2修復工作很好。 #1仍然有點麻煩。什麼時候應該檢查輸入isEmpty()?這些循環讓我環繞着自己的大腦! –

+0

這並不重要。我會檢查一下,看看在輸入'int length = value.length()'之後輸入是否爲空。 – Flaom

+0

感謝@Flaom。我會運行它,看看會發生什麼。 –

0

後一些非常需要幫助,解決方案如下。修復bug#1的目的是實現一種方法來檢查傳遞的字符串是否爲空。修復bug#2的目的是檢查用戶是否只輸入一個句點,並且只有一個字符長度。 @ Flamen非常聰明。

修復#1:確保輸入開始之前的for循環

//validate to see if string is empty before starting! 
     if(value.isEmpty()) 
     { 
      valid = false; 

     } 

修復#2:

// check to see if user only input a period 
     if (value.contains(".") && length == 1) { 
      valid = false; 
     } 

請查看下面的代碼,看看究竟在什麼地方。

package numbers; 

    /* 
    * File name: IsAValidNumber.java 
    * Project name: Is a Valid Number 
    * 
    * Creator's name: Joshua Trimm 
    * Email: [email protected] 
    * Course and section: CISP 1010 
    * Creation Date: 10-9-17 
    */ 

    import java.util.Scanner; 
    /* 
    * <b>Floating point value validation</b> 
    * <hr> 
    * Date created: 10-9-2017 
    * <hr> 
    * @author Joshua Trimm 
    * 
    */ 
    public class IsAValidNumber 
    { 


     /* 
     * Method description: This is the main method 
     * @param args 
     * @return void 
     */ 

     public static void main(String[] args) 
     { 

      //console input scanner 
      Scanner consoleInput = new Scanner(System.in); 


      //Prompt user for input 
      System.out.print("enter a valid number:"); 
      String validat = consoleInput.nextLine(); 
      //define boolean from method 
      boolean isTrue = NumberIsValid(validat); 
      //while loop 
      while(isTrue == true) 
      { 


       System.out.println(validat + " is a valid number. Please enter another value:"); 
       validat = consoleInput.nextLine(); 
       isTrue = NumberIsValid(validat); 


      } 
      //if it jumps out of the while because it is false run this 
      System.out.println(validat + " is not a valid number, bye"); 
      consoleInput.close(); 
     } 
     /* 
     * Method description: Setup a boolean method 
     * @param string 
     * @return boolean 
     */ 
     public static boolean NumberIsValid(String value) 
     { 

      //define local variables 
      int period = 0; 
      boolean valid = true; 
      int length = value.length(); 
      int digitCount = 0; 


      //validate to see if string is empty before starting! 
      if(value.isEmpty()) 
      { 
       valid = false; 

      } 

      //Start running through the string 
      for(int i = 0; i < length; i++) 
      { 


       char aChar = value.charAt(i); 


       // check to make sure its is a int 0-9 
       if((int)aChar < 48 || (int)aChar > 57) 
       { 
       // check for the period 
        if((int)aChar == 46) 
        { 
         period++; 
         if(length < 2) 
         { 
          valid = false; 
          break; 
         } 
        } 

        else 
        { 
         valid = false; 
         break; 
        } 

       } 

       //make sure it doesn't have more than one period 

       if (period > 1) 
       { 
        valid = false; 
        break; 
       } 

       // check to see if user only input a period 
       if (value.contains(".") && length == 1) { 
        valid = false; 
       } 
      } 


      return valid; 

     } 
    } 
0

都在你的代碼的拳頭正在檢查的非有利的情況下,但我會建議你只檢查有利的情況下,以檢查是否有數字1到9的「」單次發生我改變了你的方法

public static boolean NumberIsValid(String value) 
{ 


    int period = 0; 
    boolean valid = false; 


    for(int i = 0; i < length; i++) 
    { 
    if(value.charAt(i)>=48 && value.charAt(i)<=57) valid=true; 
    if(value.charAt(i)==46) {valid=true;period++;} 
    if(period!=1 && period!=0) {valid=false;break;} 

    } 


    return valid; 

} 

通過這段代碼,我們只檢查0到9以及「。」的單個出現。如果發生任何其他情況,它將返回錯誤。 通過這種方式,我們可以簡化
您在代碼中添加此方法,然後編譯並讓我知道您是否再次遇到相同的問題。