标题翻译
Encountering a logic error while looping to find unique numbers in an array
问题
import java.util.Scanner;
public class chapter7e5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter 10 numbers: ");
int[] numberArray = new int[10];
int[] distinctArray = new int[10];
int distinct = 0;
for (int i = 0; i < 10; i++)
numberArray[i] = input.nextInt();
distinctArray[0] = numberArray[0];
for (int i = 1; i < numberArray.length; i++) {
boolean exists = false;
for (int j = 0; j < distinctArray.length; j++) {
if (numberArray[i] == distinctArray[j]) {
exists = true;
break;
}
}
if (!exists) {
distinctArray[distinct] = numberArray[i];
distinct++;
}
}
System.out.println("The number of distinct numbers is " + distinct);
System.out.print("The distinct numbers are: ");
for (int k = 0; k < distinct; k++)
System.out.print(distinctArray[k] + " ");
}
}
英文翻译
This is a problem in my textbook for my Java class where the user enters 10 integers. The program is supposed to read all integers and only display the unique numbers (not duplicated) as the output.
-When run the output is:
Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 5
The distinct numbers are: 1 2 3 0 0
-When it should really be:
Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 5
The distinct numbers are: 1 2 3 6 4 5
Since we are in the early stages of the class and understanding the language, our assignment is to complete this using a nested loop. Any help would be appreciated.
import java.util.Scanner;
public class chapter7e5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//create Scanner
System.out.print("Enter 10 numbers: ");
int[] numberArray = new int[10];
//create array for all numbers
int[] distinctArray = new int[10];
//create array for distinct numbers
int distinct = 0;
for (int i = 0; i < 10; i++)
//for loop to have user enter numbers and put them into array
numberArray[i] = input.nextInt();
distinctArray[0] = numberArray[0];
//first value will be distinct
for (int i = 1; i < numberArray.length; i++) {
//loop to go through remaining values in numberArray
boolean exists = false;
//create boolean
for (int j = 0; j < distinctArray.length; j++) {
//loop to check if value exists already in distinctArray
if (numberArray[i] == distinctArray[j]) {
exists = true;
//if value does already exists, then exist = true
break;
//break out of inner loop
}
}
if (exists == false) {
//if value is unique then add it to the distinct array
distinctArray[i] = numberArray[i];
distinct++;
//increment variable distinct
}
}
//}
System.out.println("The number of distinct numbers is " + distinct);
System.out.print("The distinct numbers are: ");
for (int k = 0; k < distinct; k++)
System.out.print(distinctArray[k] + " ");
}
}
答案1
得分: 0
我有以下建议:
-
将外部循环中的每个数字从
numbArray
赋值给testNumber
<br>
例如:int testNumber = numbArray[i];
-
在内部循环中,检查没有其他数字等于
testNumber
。注意,当i == j
时不进行测试,因为该数字是分配给testNumber
的数字
if (i != j && numberArray[j] == testNumber) {
exists = true;
break;
}
- 然后在用于确定是否分配数字的 if 语句中,您可以这样做
if (!exists) { 如果值是唯一的,则将其添加到 distinct 数组中 stinctArray[distinct++] = testNumber;
请注意,在上面的代码中,通常不会显式地将布尔值与 true 或 false 进行比较。由于 if 语句在 true 或 false 条件下评估,您可以直接使用该值。因此,`exists` 表示存在,而 `!exists` 表示不存在。
<details>
<summary>英文翻译</summary>
I have the following suggestions:
- Assign each number in the outer loop `numbArray` to `testNumber` <br>
e.g. `int testNumber = numbArray[i];`
- in the inner loop, check that no other number is equal to `testNumber`. Note that <br>testing is not done when `i == j` since that number was the one assigned to `testNumber`
if (i != j && numberArray[j] == testNumber) {
exists = true;
break;
}
- Then in your if clause to determine whether to assign the number or not you can do this
if (!exists) {
// if value is unique then add it to the distinct array
distinctArray[distinct++] = testNumber;
}
Note above that is is common practice to not explicitly test boolean against true or <br> false. Since the if statement evaluates on true or false conditions, you can just use the <br>value. So `exists` means it is there and `!exists` means it isn't
</details>
专注分享java语言的经验与见解,让所有开发者获益!
评论