检查布尔数组中的值 (Java)
我在解决以下问题时遇到了一些轻微的困难。
我已经初始化了一个名为 numberArray 的布尔数组,它有 31 个索引。用户应该输入 1 到 30 之间的 5 个数字,每次输入一个数字,程序都应该将正确的索引设置为 true。例如,如果我输入 5,则:
numberArray[5] = true;
然而,如果用户第二次输入值5,则应向用户给出一条消息,表明该数字已被输入,因此用户必须选择不同的值。我尝试创建一个循环,如下所示:
public void enterArrayValues() {
for(int i = 1; i < 6; i++) {
System.out.print("Give " + i + ". number: ");
int enteredNumber = input.nextInt();
while (numberArray[enteredNumber] = true) {
System.out.println("This number has already been chosen.");
System.out.print("Give " + i + ". number again: ");
enteredNumber = input.nextInt();
}
numberArray[enteredNumber] = true;
}
}
问题是,当我运行程序时,无论我输入什么,我都会自动收到消息“该号码已被选择”。即使我第一次输入数字。我不明白这一点。布尔数组中的值不是默认都是 false 吗?
如果有人能帮助我解决这个问题,我将不胜感激!
I am having som slight difficulties with the following problem.
I have initialized a boolean array called numberArray with 31 indexes. The user is supposed to enter 5 digits between 1 and 30, and each time a digit is entered, the program is supposed to set the proper index to true. For instance, if I enter 5 then:
numberArray[5] = true;
However, if the user enters the value 5 a second time, a message should be given to the user that this number has already been entered, and so the user has to choose a different value. I have tried to create a loop as follows:
public void enterArrayValues() {
for(int i = 1; i < 6; i++) {
System.out.print("Give " + i + ". number: ");
int enteredNumber = input.nextInt();
while (numberArray[enteredNumber] = true) {
System.out.println("This number has already been chosen.");
System.out.print("Give " + i + ". number again: ");
enteredNumber = input.nextInt();
}
numberArray[enteredNumber] = true;
}
}
The problem is that when I run the program, I automatically get the message "The number has already been chosen" no matter what I enter. Even the first time I enter a number. I don't get this. Isn't all the values in the boolean array false by default?
I would greatly appreciate it if someone could help me with this!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
做到这一点
或更改为
或简单地删除
==true
make that
or change to
or simply drop the
==true
是一个赋值,请使用
==
运算符或简单地while (numberArray[enteredNumber])
。我知道当您仍在学习时很难进入,但是您越早开始在 IDE 中编码,情况就会越好。这是 IDE 会警告您的一个小例子。
is an assignment, use the
==
operator or simplywhile (numberArray[enteredNumber])
.I know its hard to get into while you are still learning, but the earlier you start coding in an IDE the better off you will be. This is one tiny example of something an IDE will warn you about.
将 while 行更改为:
因为错误地输入
=
而不是==
是一个常见错误,所以有些人总是按以下方式编写此类语句:使用这种格式,如果使用
=
而不是==
,则会出现编译器错误。另外,如果您使用 PMD 等静态分析工具,我相信您会收到针对您最初编写的语句的警告。
Change the while line to:
Because mistakenly entering
=
instead of==
is a common mistake, some people always code this type of statement in the following manner:With this format, if you use
=
instead of==
, you will get a compiler error.Also, if you use a type of static analysis tool such as PMD, I believe you get a warning for the statement that you originally wrote.
问题出在 while 循环的条件下 - 您正在使用赋值运算符 (=),而您应该使用相等比较器 (==)。这样,循环条件始终为 true,因为您将 true 分配给索引字段。
我希望这会起作用:-)。
Thde problem is in the condition of the while loop - you are using the assignment operator (=), whereas you are supposed to use the equality comparer (==). This way the loop condition is always true, because you are assigning true to the indexed field.
I hope this will work :-) .
while循环中的条件应该是
while (numberArray[enteredNumber] == true)
。您使用的是赋值运算符=
,而不是比较运算符==
。赋值是一个返回指定值的表达式,在您的情况下为true
。The condition in the while loop should be
while (numberArray[enteredNumber] == true)
. You're using the assignment operator=
, not the comparison operator==
. Assignment is an expression that returns the assigned value, which istrue
in your case.