字符 Class 方法问题

Character Class Method issues

所以我目前正在做一项学校作业,即设计一个允许用户输入一些文本的程序,然后程序检查:

  1. 第一个单词首字母大写(需小写)
  2. 整个用户输入只有字母和数字
  3. 用户输入中没有空格

所以程序确实有效,但我遇到的问题是打印语句,我将 post 下面的代码并解释:

public static void main(String[] args) {
            try (Scanner stdln = new Scanner(System.in)) {
            String userInput;
            
            boolean legal = true;
            boolean style = true;
            
            char input; //Checks the userInput variable to be sure that boolean is false if there's no lowercase letter at char0 + if there are no letters
            char loop;
            
            System.out.println("The program checks the properness of a proposed Java variable name.");
            System.out.println("\nPlease enter a variable name (q to quit): ");
            userInput = stdln.nextLine();
            input = userInput.charAt(0);
            
            do
            {
                
                if (!(Character.isLetter(input)))
                {
                    legal = false;
                }
                if (userInput.contains(" "))
                {
                    legal = false;
                }
                if (!(Character.isLowerCase(input)))
                {
                    style = false;
                }
                    for (int i = 1; i < userInput.length() &&legal; i++)
                    {
                        loop = userInput.charAt(i);
                                    
                        if (!(Character.isLetterOrDigit(loop)))
                        {
                            style = false;
                        }
                    }
                
                if (!(legal) && !(style))
                {
                    System.out.println("Illegal.");
                } 
                else if (legal && !(style)) 
                {
                    System.out.println("Legal, but uses poor style.");
                } 
                else  
                {
                    System.out.println("Good.");
                }
                
                System.out.println("\nPlease enter a variable name (q to quit): ");
                userInput = stdln.nextLine();
                input = userInput.charAt(0);
                
            } while (!(userInput.equalsIgnoreCase("q"))); 
        }
    }
}

所以代码可以正常工作,我测试的第一个输入也能正常输出,但是,一旦我得到一个不是“好”的响应,那么每个条目都会打印相同的响应,这是一个示例来自 session 我刚刚做了:

程序检查提议的 Java 变量名的正确性。

  1. 请输入变量名(q退出): 街道地址2 好

  2. 请输入变量名(q退出): 街道地址2 合法,但风格不佳。

  3. 请输入变量名(q退出): 街道地址2 合法,但风格不佳。

  4. 请输入变量名(q退出): 街道地址2 非法。

  5. 请输入变量名(q退出): 街道地址2 非法。

在该示例 session 中,3 和 5 应该 return 语句“好”。但出于某种原因,它只是打印了上一个条目的语句。我对 Java 还是很陌生,所以我有点难过。有什么想法吗?

您忘记将 legalstyle 布尔变量重置为真。

在每次迭代中,legalstyle 变量将继续包含先前输入的结果。例如,如果您在第一次输入时立即写了一个语法不合法且风格不佳的变量名,您会发现后面的任何名称都会显示相同的结果。即使这些名称很好或者它们只是缺乏风格,输出仍然是相同的(错误的),因为两个变量都被保留为 false 并且没有任何东西将它们设置回 true。

此外,打印输出消息的逻辑没有正确考虑所有组合。

变量逻辑和输出打印都可以re-written如下:

do {
    //forgotten reset
    legal = true;
    style = true;

    //excat same implementation of yours
    if (!(Character.isLetter(input))) {
        legal = false;
    }
    if (userInput.contains(" ")) {
        legal = false;
    }
    if (!(Character.isLowerCase(input))) {
        style = false;
    }
    for (int i = 1; i < userInput.length() && legal; i++) {
        loop = userInput.charAt(i);

        if (!(Character.isLetterOrDigit(loop))) {
            style = false;
        }
    }

    //If it's illegal it does not matter whether the variable name has a poor or good style, it's illegal anyway
    if (!legal) {
        System.out.println("Illegal.");
        
        //If we're in this else branch then the variable name is legal, but we have to check its style.
        //If it has poor style then we print the "poor style" message.
    } else if (!style) {
        System.out.println("Legal, but uses poor style.");
    } else {
        //Last case where the variable name is legal and has a good style
        System.out.println("Good.");
    }

    System.out.println("\nPlease enter a variable name (q to quit): ");
    userInput = stdln.nextLine();
    input = userInput.charAt(0);

} while (!(userInput.equalsIgnoreCase("q")));

您必须在每次迭代开始时将 legalstyle 重置为 true。但是,这不是您的代码的唯一问题。逻辑不对

现在在 for 循环中,您检查所有字符都是字母或数字。如果此条件失败,则将 style 设置为 false。但是,您应该将 legal 设置为 false,因为这样的标识符是不允许的。

此外,当您打印结果时,您没有正确检查条件。例如,如果 legal 为假,但 style 为真,您的代码将打印 Good.