角色类方法问题
,我目前正在从事一项学校作业,该分配是设计一个程序,该程序允许用户输入一些文本,然后检查程序检查:
- 第一个单词的第一个字母是大写字母(需要是小写)
- 因此 整个用户输入中只有字母和数字
- ,用户输入中没有空格,
因此程序确实有效,但是我遇到的问题是打印语句,我将在下面发布我的代码并说明:
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变量名称的适当性。
请输入一个变量名(q to退出): StreetAddress2 好。
请输入一个变量名(q to退出): StreetAddress2 合法,但使用糟糕的风格。
请输入一个变量名(q to退出): StreetAddress2 合法,但使用糟糕的风格。
请输入一个变量名(q to退出): 街道地址2 非法。
请输入一个变量名(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:
- The first letter of the first word is a capital letter (it needs to be lowercase)
- That there are only letters and numbers in the entire user input
- 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.
Please enter a variable name (q to quit):
streetAddress2
Good.Please enter a variable name (q to quit):
StreetAddress2
Legal, but uses poor style.Please enter a variable name (q to quit):
streetAddress2
Legal, but uses poor style.Please enter a variable name (q to quit):
Street Address2
Illegal.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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须在每次迭代开始时重置
Legal
和样式
true
。但是,这并不是您的代码的唯一问题。逻辑不正确。现在,在循环的
中,您检查所有字符是字母或数字。如果此条件失败,则将
样式
设置为false
。但是,您应该将Legal
设置为false
,因为不允许使用此类标识符。另外,当您打印结果时,您不会正确检查条件。例如,如果
Legal
是错误的,但是样式
是正确的,您的代码将打印好
。You have to reset
legal
andstyle
totrue
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 setstyle
tofalse
. However, you should setlegal
tofalse
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, butstyle
is true your code will printGood
.您忘了重置为true
Legal
和样式
布尔变量。在每次迭代中,
Legal
和样式
变量将继续包含上一个输入的结果。例如,如果在您的第一个输入上,您立即编写具有非法语法和糟糕样式的变量名称,则您会发现任何以下名称都会显示出相同的结果。即使这些名称是好的,或者它们仅缺乏样式,但输出仍然是相同的(错误),因为两个变量都被误认为是错误的,没有任何变量将它们恢复为true。此外,打印输出消息的逻辑未正确解释所有组合。
可变逻辑和输出打印均可重写如下:
You forgot to reset to true your
legal
andstyle
boolean variables.At every iteration, the
legal
andstyle
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: