ascii table 计数器 Java 不返回 null 作为 false

ascii table counter with Java not returning null as false

我尝试验证用户是否输入了数字 0-9 或者是否有小数。它会验证它是数字还是字母,但如果用户未输入任何内容,循环仍会像它是有效数字一样运行。

所以,有两个错误:

  1. 用户在输入有效浮点数之前按回车键,脚本仍然有效地运行。

  2. 用户输入了一个句点但没有输入任何数字,脚本仍然运行有效。

package numbers;

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();

        boolean isTrue = NumberIsValid(validat);
        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();
    }
    
    public static boolean NumberIsValid(String value)
    {
        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;
    }
}

我怎样才能正确地做到这一点?

对于错误 #1:在您的方法 NumberIsValid(String value) 中,您从不检查参数是否为空。当一个空参数被传递给你的方法时,for 循环被跳过并且它 returns 为真。

对于错误 #2:不是最好的解决方案,但您可以简单地创建如下内容:

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

经过一番求助,解决方法如下。错误 #1 的修复是实现一种检查传递的字符串以查看它是否为空的方法。错误 #2 的修复是检查用户是否只输入了一个句点并且只有一个字符长度。 @Flaom 很聪明。

修复 #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: jbthype@gmail.com
     * 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;

        }
    }

首先,在您的代码中,您检查的是不利情况,但我建议您仅检查有利情况,例如检查数字 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 和单次出现的“.”。如果发生任何其他情况,它将 return false。 这样我们就可以很简单
你在你的代码中添加这个方法,然后编译,如果你再次遇到同样的问题,请告诉我。