英文:
Error when picking out unique numbers in an array
问题
public class ch7e5 {
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++) {
//遍历剩余的numberArray中的值
boolean exists = false;
//创建布尔值
for (int j = 0; j < distinctArray.length; j++) {
//循环检查该值是否已经存在于distinctArray中
if (numberArray[i] == distinctArray[j]) {
exists = true;
break;
//跳出内循环
}
}
if (!exists) {
//如果值是唯一的,则将其添加到distinct数组中
distinct++;
distinctArray[distinct] = numberArray[i];
}
}
System.out.println("The number of distinct numbers is " + (distinct + 1));
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. I am having trouble understanding why my output is not picking up the last unique value in the array (5). Can anyone give some insight to this issue? Any help would be appreciated. (Since we are in the early stages of the class and understanding the language, our assignment is to complete this using a nested loop.)
-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 6 4
-When it should be:
Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 6
The distinct numbers are: 1 2 3 6 4 5
public class ch7e5{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
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++)
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 < numberArray.length; j++) {
//loop to check if value exists already in distinctArray
if (numberArray[i] == distinctArray[j]) {
exists = true;
break;
//break out of inner loop
}
}
if (exists == false) {
//if value is unique then add it to the distinct array
distinct++;
distinctArray[distinct] = distinctArray[i];
}
}
//}
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] + " ");
}
}```
</details>
# 答案1
**得分**: 1
这里有3个不同的数字,1、2和3都不是其中的数字,因为它们各自出现了两次。输出应该是 6 4 5。我不确定你是怎么得到5个不同的数字的,也许输入有误?我建议首先不要使用扫描器,尝试手动将数字放入数组中。另外,我建议创建一个长度为10的布尔数组,初始值都设置为true,用于记录数字是否不同。如果一个数字出现两次,数组中对应的布尔值将被设置为false。一旦我编写完代码,我会在下面更新。
编辑:显然,有重复的数字并不会从不同的数字列表中删除。如果是这种情况,请详细说明一下标题。
以下是你的代码:
```java
public class Main {
public static void main(String[] args) {
int inputs = 3;
int[] numberArray = new int[inputs];
int distinct = 0;
boolean[] mirror = new boolean[inputs];
//只是设置数组
for(int i = 0; i < numberArray.length; i++) {
//numberArray[i] = i;
mirror[i] = true;
}
numberArray[0] = 1;
//找出哪些数字不是不同的
for (int x = 0; x < numberArray.length; x++) {
for (int y = 0; y < numberArray.length; y++) {
System.out.println("x is " + x);
System.out.println("y is " + y);
if(numberArray[x] == numberArray[y] && x != y) {
System.out.println(numberArray[x] + " is not distinct");
mirror[x] = false; //如果数组中的当前位置与其他位置相匹配,数字就不是不同的
}
}
}
//计算有多少是不同的
for(int j = 0; j < inputs; j++) {
if(mirror[j]) {distinct++;}
}
//输出文本
System.out.println("The number of distinct numbers is " + distinct);
System.out.print("The distinct numbers are: ");
for(int k = 0; k < inputs; k++){
if(mirror[k]) {System.out.print(numberArray[k] + " ");}
}
}
}
英文:
There are 3 distinct numbers, 1, 2, and 3 are not any of them as they appear twice. The output should be 6 4 5. I'm not sure how you got 5 distinct numbers here, maybe you inputted them wrong? I would try not using a scanner at first and try putting the numbers in the array manually. Additionally, I would create a boolean array length 10 starting all true to record if numbers are distinct. If a number appears twice, the corresponding boolean in the array will be false. I will update this with code once i have written it.
EDIT: apparently having a duplicate does not delete it from the distinct list. If this is the case, please elaborate on the title.
Here is my code:
public class Main {
public static void main(String[] args) {
int inputs = 3;
int[] numberArray = new int[inputs];
int distinct = 0;
boolean[] mirror = new boolean[inputs];
//just setting up arrays
for(int i = 0; i < numberArray.length; i++) {
//numberArray[i] = i;
mirror[i] = true;
}
numberArray[0] = 1;
//finds out what numbers are not distinct
for (int x = 0; x < numberArray.length; x++) {
for (int y = 0; y < numberArray.length; y++) {
System.out.println("x is " + x);
System.out.println("y is " + y);
if(numberArray[x] == numberArray[y] && x != y) {
System.out.println(numberArray[x] + " is not distinct");
mirror[x] = false;//if current position in array matches any other position, number is not distinct
}
}
}
//calculates how many are distinct
for(int j = 0; j < inputs; j++) {
if(mirror[j]) {distinct++;}
}
//outputs text
System.out.println("The number of distinct numbers is " + distinct);
System.out.print("The distinct numbers are: ");
for(int k = 0; k < inputs; k++){
if(mirror[k]) {System.out.print(numberArray[k] + " ");}
}
}
}
答案2
得分: 0
错误出现在这里
distinct++;
distinctArray[distinct] = distinctArray[i];
1、在将数字添加到distinctArray
后,您应该递增distinct
;2、您应该执行distinctArray[distinct] = numberArray[i]
。目前,您只是将可能是0
的内容放入distinctArray[distinct]
。
更简洁的方法是这样的
int[] distinctArray = Arrays.stream(numberArray).distinct().toArray();
System.out.println("不同数字的数量为:" + distinctArray.length);
System.out.print("不同的数字为:");
for (int d : distinctArray) System.out.print(d + " ");
输出:
输入10个数字:1 2 3 2 1 6 3 4 5 2
不同数字的数量为:6
不同的数字为:1 2 3 6 4 5
英文:
The error was here
distinct++;
distinctArray[distinct] = distinctArray[i];
1, you should increment distinct
after adding a number to distinctArray
, and 2, you should have done distinctArray[distinct] = numberArray[i]
. Right now, you're just putting what is possibly a 0
into distinctArray[distinct]
.
A shorter way to do it would be this
int[] distinctArray = Arrays.stream(numberArray).distinct().toArray();
System.out.println("The number of distinct numbers is " + distinctArray.length);
System.out.print("The distinct numbers are: ");
for (int d : distinctArray) System.out.print(d + " ");
Output:
Enter 10 numbers: 1 2 3 2 1 6 3 4 5 2
The number of distinct numbers is 6
The distinct numbers are: 1 2 3 6 4 5
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论