检查布尔数组中的值 (Java)

发布于 2024-12-08 22:45:49 字数 856 浏览 2 评论 0原文

我在解决以下问题时遇到了一些轻微的困难。

我已经初始化了一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

等往事风中吹 2024-12-15 22:45:49
while (numberArray[enteredNumber] = true) {

做到这一点

while (numberArray[enteredNumber] == true) {

或更改为

while (true == numberArray[enteredNumber]) {

或简单地删除 ==true

while (numberArray[enteredNumber]) {
while (numberArray[enteredNumber] = true) {

make that

while (numberArray[enteredNumber] == true) {

or change to

while (true == numberArray[enteredNumber]) {

or simply drop the ==true

while (numberArray[enteredNumber]) {
一桥轻雨一伞开 2024-12-15 22:45:49
while (numberArray[enteredNumber] = true) 

是一个赋值,请使用 == 运算符或简单地 while (numberArray[enteredNumber])

我知道当您仍在学习时很难进入,但是您越早开始在 IDE 中编码,情况就会越好。这是 IDE 会警告您的一个小例子。

while (numberArray[enteredNumber] = true) 

is an assignment, use the == operator or simply 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.

痴者 2024-12-15 22:45:49

将 while 行更改为:

while (numberArray[enteredNumber]) {

因为错误地输入 = 而不是 == 是一个常见错误,所以有些人总是按以下方式编写此类语句:

while (true == numberArray[enteredNumber]) {

使用这种格式,如果使用 = 而不是 ==,则会出现编译器错误。

另外,如果您使用 PMD 等静态分析工具,我相信您会收到针对您最初编写的语句的警告。

Change the while line to:

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.

梓梦 2024-12-15 22:45:49

问题出在 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 :-) .

爱,才寂寞 2024-12-15 22:45:49

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 is true in your case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文