Java的Do-While循环保留了先前的循环内容。

huangapple go评论128阅读模式
英文:

Java Do-While Loop is keeping the previous loop contents

问题

我正在为我的第一门计算机科学课程做这个作业,所以对Java还不太熟悉。目标是创建一个程序,检查用户输入的Java变量是否符合规定。有三个规定/输出:

  1. "非法"(不允许空格,必须以字母开头)
  2. "合法,但风格不佳"(应只使用字母或数字)
  3. "好!"

如果我运行这个程序,并在第一次循环中输入类似"variable"的内容,它会在结束时打印"好!"。当程序再次询问并我输入类似"4variable"的内容时,它会返回"非法" - 这是有道理的。问题出现在这之后。如果我再次输入"variable"这样的变量,它会返回"非法",因为它仍然认为第一个字符位置有一个数字。当我调试时,显示"number"布尔值设置为 'true',尽管它应该是 'false'。我无法让它放弃前一次循环。

以下是我的代码:

import java.util.Scanner;

public class IdentifierTestThree {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);

        String userVariable = "";
        char ch = ' ';
        boolean space = false;
        boolean number = false;
        boolean capital = false;
        //boolean specialCharacter = false;
        boolean style = false;


        // 说明这个程序的作用
        System.out.println("This program checks to see if a Java variable is legal or not.");

        // 从用户获取输入
        System.out.println("Enter a variable: ");
        userVariable = in.nextLine();

        do {
            int variableParse = userVariable.length();
            for (int i = 0; i < variableParse; i++) {
                if ((Character.isLetterOrDigit(userVariable.charAt(i)) == false)) {
                    style = true;
                }
            }

            // 获取第一个字符
            ch = userVariable.charAt(0);

            // 检查每个规定
            if (Character.isUpperCase(ch)) {
                capital = true;
            }
            if (Character.isDigit(ch)) {
                number = true;

            }
            if (userVariable.contains(" ")) {
                space = true;
            }

            if (space || number || capital) {
                System.out.println("Illegal");
            } else if (style) {
                System.out.println("Legal, but uses poor style.");
            } else {
                System.out.println("Good!");
            }

            // 要求用户输入另一个变量或结束程序
            System.out.println("Enter another variable or type 'Q' to quit.");
            userVariable = in.nextLine();

        } while (!userVariable.equalsIgnoreCase("Q"));

    }

}

希望这可以帮助你解决问题。如果你有其他问题或需要进一步的帮助,请随时告诉我。

英文:

I'm doing this as a homework assignment for my first CS class, so I'm pretty new to Java. The goal is to create a program that checks a user input for a specified Java variable. There are three stipulations/outputs:

  1. "Illegal" (no spaces allowed, must begin with a letter)
  2. "Legal, but uses poor style" (should only use letters or digits)
  3. "Good!"

If I run this program and type something like "variable" for the first loop, it will go through and print "Good!" by the end. And when the program asks again and I type something like "4variable", it will return "Illegal" - makes sense. The problem comes right after this. If I type the next variable like "variable" again, it will return "Illegal" because it still thinks there is a digit in the first character position. When I debug, it shows the "number" boolean set to 'true' even though it's false. I can't get it to drop the previous iteration of the loop.

Here is my code:

import java.util.Scanner;
public class IdentifierTestThree {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String userVariable = &quot;&quot;;
char ch = &#39; &#39;;
boolean space = false;
boolean number = false;
boolean capital = false;
//boolean specialCharacter = false;
boolean style = false;
// State what this program does
System.out.println(&quot;This program checks to see if a Java variable is legal or not.&quot;);
// Get input from user
System.out.println(&quot;Enter a variable: &quot;);
userVariable = in .nextLine();
do {
int variableParse = userVariable.length();
for (int i = 0; i &lt; variableParse; i++) {
if ((Character.isLetterOrDigit(userVariable.charAt(i)) == false)) {
style = true;
}
}
// Get the first character
ch = userVariable.charAt(0);
// Check each stipulation
if (Character.isUpperCase(ch)) {
capital = true;
}
if (Character.isDigit(ch)) {
number = true;
}
if (userVariable.contains(&quot; &quot;)) {
space = true;
}
if (space || number || capital) {
System.out.println(&quot;Illegal&quot;);
} else if (style) {
System.out.println(&quot;Legal, but uses poor style.&quot;);
} else {
System.out.println(&quot;Good!&quot;);
}
// Ask the user to enter another variable or end the program
System.out.println(&quot;Enter another variable or type &#39;Q&#39; to quit.&quot;);
userVariable = in .nextLine();
} while (!userVariable.equalsIgnoreCase(&quot;Q&quot;));
}
}

答案1

得分: 0

你需要在处理单个变量的do循环内重置空格、数字、大写和样式。实际上,你可以将声明也移动到那里,因为这些值在该循环外部不需要。

do { 
    boolean space = false;
    boolean number = false;
    boolean capital = false;
    //boolean specialCharacter = false;
    boolean style = false;

    int variableParse = userVariable.length();
    for (int i = 0; i &lt; variableParse; i++) {
       if ((Character.isLetterOrDigit(userVariable.charAt(i)) == false)) {
           style = true;
       }
    }

    // 获取第一个字符
    char ch = userVariable.charAt(0);

              
英文:

You need to reset space, number, capital and style inside the do-loop which is processing a single variable.
In fact you can move the declarations there as well, since the values are not needed outside that loop.

do { 
boolean space = false;
boolean number = false;
boolean capital = false;
//boolean specialCharacter = false;
boolean style = false;
int variableParse = userVariable.length();
for (int i = 0; i &lt; variableParse; i++) {
if ((Character.isLetterOrDigit(userVariable.charAt(i)) == false)) {
style = true;
}
}
// Get the first character
char ch = userVariable.charAt(0);
…

huangapple
  • 本文由 发表于 2020年8月5日 09:19:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63257102.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定