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的布尔数组,其中包含31个索引。用户应该在1到30之间输入5位数,并且每次输入数字时,程序应该将正确的索引设置为true。例如,如果我输入5则:
numberArray[5] = true;
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:
但是,如果用户第二次输入值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;
}
}
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?
问题是,当我运行程序时,无论我输入什么,我都会自动收到消息“已经选择了数字”。即使我第一次输入数字。我不懂。默认情况下,布尔数组中的所有值都不是false吗?
I would greatly appreciate it if someone could help me with this!
如果有人能帮助我,我将不胜感激!
5 个解决方案
#1
5
while (numberArray[enteredNumber] = true) {
make that
做那个
while (numberArray[enteredNumber] == true) {
or change to
或改为
while (true == numberArray[enteredNumber]) {
or simply drop the ==true
或者简单地删除== true
while (numberArray[enteredNumber]) {
#2
2
while (numberArray[enteredNumber] = true)
is an assignment, use the ==
operator or simply while (numberArray[enteredNumber])
.
是一个赋值,使用==运算符或简单地while(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.
我知道在你还在学习的时候很难进入,但是你越早开始在IDE编写代码就会越好。这是IDE会警告你的一个小例子。
#3
1
Change the while line to:
将while行更改为:
while (numberArray[enteredNumber]) {
Because mistakenly entering =
instead of ==
is a common mistake, some people always code this type of statement in the following manner:
因为错误输入=而不是==是一个常见的错误,有些人总是以下列方式编写这种类型的语句:
while (true == numberArray[enteredNumber]) {
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.
此外,如果您使用一种静态分析工具(如PMD),我相信您会收到最初编写的语句的警告。
#4
1
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循环的条件下 - 你正在使用赋值运算符(=),而你应该使用相等比较器(==)。这样循环条件始终为true,因为您将索引字段赋值为true。我希望这会奏效:-)。
#5
1
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 is true
in your case.
while循环中的条件应为while(numberArray [enteredNumber] == true)。您正在使用赋值运算符=,而不是比较运算符==。赋值是一个返回指定值的表达式,在您的情况下为true。
#1
5
while (numberArray[enteredNumber] = true) {
make that
做那个
while (numberArray[enteredNumber] == true) {
or change to
或改为
while (true == numberArray[enteredNumber]) {
or simply drop the ==true
或者简单地删除== true
while (numberArray[enteredNumber]) {
#2
2
while (numberArray[enteredNumber] = true)
is an assignment, use the ==
operator or simply while (numberArray[enteredNumber])
.
是一个赋值,使用==运算符或简单地while(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.
我知道在你还在学习的时候很难进入,但是你越早开始在IDE编写代码就会越好。这是IDE会警告你的一个小例子。
#3
1
Change the while line to:
将while行更改为:
while (numberArray[enteredNumber]) {
Because mistakenly entering =
instead of ==
is a common mistake, some people always code this type of statement in the following manner:
因为错误输入=而不是==是一个常见的错误,有些人总是以下列方式编写这种类型的语句:
while (true == numberArray[enteredNumber]) {
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.
此外,如果您使用一种静态分析工具(如PMD),我相信您会收到最初编写的语句的警告。
#4
1
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循环的条件下 - 你正在使用赋值运算符(=),而你应该使用相等比较器(==)。这样循环条件始终为true,因为您将索引字段赋值为true。我希望这会奏效:-)。
#5
1
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 is true
in your case.
while循环中的条件应为while(numberArray [enteredNumber] == true)。您正在使用赋值运算符=,而不是比较运算符==。赋值是一个返回指定值的表达式,在您的情况下为true。