角色类方法问题

发布于 2025-01-29 04:38:17 字数 2892 浏览 2 评论 0原文

,我目前正在从事一项学校作业,该分配是设计一个程序,该程序允许用户输入一些文本,然后检查程序检查:

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

因此程序确实有效,但是我遇到的问题是打印语句,我将在下面发布我的代码并说明:

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"))); 
        }
    }
}

因此代码作品和第一个输入I测试的内容会出现,但是,一旦我得到了“不好”的响应。那么,每个条目都会打印相同的响应,这是我刚刚完成的会话中的一个示例:

该程序检查了提出的Java变量名称的适当性。

  1. 请输入一个变量名(q to退出): StreetAddress2 好。

  2. 请输入一个变量名(q to退出): StreetAddress2 合法,但使用糟糕的风格。

  3. 请输入一个变量名(q to退出): StreetAddress2 合法,但使用糟糕的风格。

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

  5. 请输入一个变量名(q to退出): StreetAddress2 非法。

在该示例会话中,第3和5号应返回“良好”。但是由于某种原因,它只是在上一个条目中打印说明。我仍然是Java的新手,所以我有点难过。有什么想法吗?

So I'm currently working on a school assignment which is to design a program that allows the user to enter some text, and then the program checks:

  1. The first letter of the first word is a capital letter (it needs to be lowercase)
  2. That there are only letters and numbers in the entire user input
  3. That there are no spaces in the user input

So the program does work but the issue I'm having is with the print statements, I'll post my code below and explain:

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"))); 
        }
    }
}

So the code works and the first input I test comes out as it should, however, once I get a response that isn't "Good.", then the same response will print for every entry, here's a sample from a session I just did:

The program checks the properness of a proposed Java variable name.

  1. Please enter a variable name (q to quit):
    streetAddress2
    Good.

  2. Please enter a variable name (q to quit):
    StreetAddress2
    Legal, but uses poor style.

  3. Please enter a variable name (q to quit):
    streetAddress2
    Legal, but uses poor style.

  4. Please enter a variable name (q to quit):
    Street Address2
    Illegal.

  5. Please enter a variable name (q to quit):
    streetAddress2
    Illegal.

In that sample session, 3 and 5 should return the statement "Good." but for some reason, it just prints the statement from the previous entry. I'm still fairly new to Java so I'm a little stumped. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠栀 2025-02-05 04:38:17

您必须在每次迭代开始时重置Legal样式 true。但是,这并不是您的代码的唯一问题。逻辑不正确。

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

另外,当您打印结果时,您不会正确检查条件。例如,如果Legal是错误的,但是样式是正确的,您的代码将打印

You have to reset legal and style to true at the start of each iteration. However, it is not the only problem with your code. The logic is not correct.

Right now in the for loop you check all the characters being letters or digits. If this condition fails you set style to false. However, you should set legal to false instead, because such identifiers are not allowed.

Also, when you print the result you don't check the conditions correctly. For example, if legal is false, but style is true your code will print Good.

土豪我们做朋友吧 2025-02-05 04:38:17

您忘了重置为true Legal样式布尔变量。

在每次迭代中,Legal样式变量将继续包含上一个输入的结果。例如,如果在您的第​​一个输入上,您立即编写具有非法语法和糟糕样式的变量名称,则您会发现任何以下名称都会显示出相同的结果。即使这些名称是好的,或者它们仅缺乏样式,但输出仍然是相同的(错误),因为两个变量都被误认为是错误的,没有任何变量将它们恢复为true。

此外,打印输出消息的逻辑未正确解释所有组合。

可变逻辑和输出打印均可重写如下:

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

You forgot to reset to true your legal and style boolean variables.

At every iteration, the legal and style variables will keep containing the result of the previous input. For example, if on your first input you immediately write a variable name with an illegal syntax and poor style, you'll see that any following name will show the same result. Even though those names are good or they only lack in style, the output will still be the same (wrong) because both variables have been left to false and nothing sets them back to true.

Besides, the logic to print the output messages didn't account for all combinations correctly.

Both variable logic and output printing could be re-written as follows:

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")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文