Java Do-While 循环保留之前的循环内容
Java Do-While Loop is keeping the previous loop contents
我将此作为我的第一个 CS class 的家庭作业,所以我是 Java 的新手。目标是创建一个程序来检查指定 Java 变量的用户输入。一共有三个stipulations/outputs:
- “非法”(不允许有空格,必须以字母开头)
- “合法,但风格不佳”(应该只使用字母或数字)
- “好!”
如果我 运行 这个程序并在第一个循环中输入类似“变量”的内容,它将通过并打印“好!”到最后。当程序再次询问并且我输入类似“4variable”的内容时,它将 return “非法” - 有道理。问题就在这之后出现。如果我再次键入下一个变量,如“变量”,它将 return “非法”,因为它仍然认为第一个字符位置有一个数字。当我调试时,它显示“数字”布尔值设置为 'true',即使它是假的。我无法让它放弃循环的前一次迭代。
这是我的代码:
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;
// State what this program does
System.out.println("This program checks to see if a Java variable is legal or not.");
// Get input from user
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;
}
}
// 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(" ")) {
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!");
}
// Ask the user to enter another variable or end the program
System.out.println("Enter another variable or type 'Q' to quit.");
userVariable = in .nextLine();
} while (!userVariable.equalsIgnoreCase("Q"));
}
}
您需要在处理单个变量的do-loop 中重新设置space、数字、大写和样式。
事实上,您也可以将声明移到那里,因为在该循环之外不需要这些值。
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 < variableParse; i++) {
if ((Character.isLetterOrDigit(userVariable.charAt(i)) == false)) {
style = true;
}
}
// Get the first character
char ch = userVariable.charAt(0);
…
我将此作为我的第一个 CS class 的家庭作业,所以我是 Java 的新手。目标是创建一个程序来检查指定 Java 变量的用户输入。一共有三个stipulations/outputs:
- “非法”(不允许有空格,必须以字母开头)
- “合法,但风格不佳”(应该只使用字母或数字)
- “好!”
如果我 运行 这个程序并在第一个循环中输入类似“变量”的内容,它将通过并打印“好!”到最后。当程序再次询问并且我输入类似“4variable”的内容时,它将 return “非法” - 有道理。问题就在这之后出现。如果我再次键入下一个变量,如“变量”,它将 return “非法”,因为它仍然认为第一个字符位置有一个数字。当我调试时,它显示“数字”布尔值设置为 'true',即使它是假的。我无法让它放弃循环的前一次迭代。
这是我的代码:
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;
// State what this program does
System.out.println("This program checks to see if a Java variable is legal or not.");
// Get input from user
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;
}
}
// 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(" ")) {
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!");
}
// Ask the user to enter another variable or end the program
System.out.println("Enter another variable or type 'Q' to quit.");
userVariable = in .nextLine();
} while (!userVariable.equalsIgnoreCase("Q"));
}
}
您需要在处理单个变量的do-loop 中重新设置space、数字、大写和样式。 事实上,您也可以将声明移到那里,因为在该循环之外不需要这些值。
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 < variableParse; i++) {
if ((Character.isLetterOrDigit(userVariable.charAt(i)) == false)) {
style = true;
}
}
// Get the first character
char ch = userVariable.charAt(0);
…