Java 数组:在一组 10 个输入数字中查找唯一数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18885162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Arrays: Finding Unique Numbers In A Group of 10 Inputted Numbers
提问by Doestovsky
I'm at a loss here.
我在这里不知所措。
I have this homework assignment where I have to enable the user to input 10 numbers, place them in an array, and figure out which inputted numbers are unique.
我有这个家庭作业,我必须让用户输入 10 个数字,将它们放在一个数组中,并找出哪些输入的数字是唯一的。
This is my workflow right now: Input number> If number has not been inputted before, store in array; if number has been inputted before, ignore> Display the numbers inputted> Display the unique numbers
这是我现在的工作流程:输入数字>如果之前没有输入数字,则存储在数组中;如果之前输入过数字,忽略>显示输入的数字>显示唯一的数字
ex: Inputting 1 2 3 5 1 2 4 6 would find the unique numbers and show "1 2 3 4 5 6"
例如:输入 1 2 3 5 1 2 4 6 会找到唯一的数字并显示“1 2 3 4 5 6”
So far my code looks like this:
到目前为止,我的代码如下所示:
public class HwChapter6 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] count = new int[10];
int number = 0;
int x = 0;
boolean unique = false;
int length = count.length;
System.out.println("Insert 10 single digit numbers in any order your heart desires:");
for (int i = 0; i < count.length; i++) {
count[i] = input.nextInt();
number = count[i];
for (int j = 0; j < count.length; j++) {
Thanks for the help guys.
谢谢你们的帮助。
采纳答案by Techwolf
Check the numbers at they are entered, then keep track of which ones are unique by marking the same positions in a second (boolean) array with true
if they are unique and false
otherwise.
检查输入的数字,然后通过在第二个(布尔)数组中标记相同的位置来跟踪哪些是唯一的,true
如果它们是唯一的,false
否则。
Then, when you print out the unique values, only print the value from each position in numbers[]
if that position in uniques[]
contains true
.
然后,当您打印出唯一值时,numbers[]
如果该位置uniques[]
包含,则仅打印来自每个位置的值true
。
Scanner input = new Scanner(System.in);
int[] numbers = new int[10];
boolean[] uniques = new boolean[10];
for(int i = 0; i < 10; i++) {
System.out.println("Please enter a value: \n" + "[" + (i + 1) + "]: ");
numbers[i] = input.nextInt();
uniques[i] = true;
for(int j = 0; j < 10; j++) {
if(numbers[i] == numbers[j] && i != j) {
uniques[i] = false;
}
}
}
System.out.println("\nThe numbers you entered were: \n");
for(int i = 0; i < 10; i++) {
System.out.println(numbers[i] + ", ");
}
System.out.println("done.\n\n");
System.out.println("\nThe uniqe numbers you entered were: \n");
for(int i = 0; i < 10; i++) {
if(uniques[i]) {
System.out.println(numbers[i] + ", ");
}
}
System.out.println("done.\n\n");
回答by NRitH
Instead of an array of input values, put them in a Set
of Integers
. Sets, by definition, store only unique values. If you add 3 'foos', there will be only one 'foo' in the set.
而不是输入值的数组,将它们放在 a Set
of 中Integers
。根据定义,集合只存储唯一值。如果添加 3 个 'foos',则集合中将只有一个 'foo'。
// Add this to your top-level loop
Set<Integer> uniqueValues = new TreeSet<Integer>;
uniqueValues.add(number);
// Add this after the loop to write all unique values on one line
for (Integer value : uniqueValues) {
System.out.print(value.toString() + " ");
}
// Now end the line.
System.out.println();
回答by ThimoKl
Store all numbers in an array. For each stored number: check if number was inserted before and save that in a boolean array. Print all numbers that are not marked in the boolean array.
将所有数字存储在一个数组中。对于每个存储的数字:检查之前是否插入了数字并将其保存在布尔数组中。打印所有未在布尔数组中标记的数字。
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
boolean[] usedBefore = new boolean[10];
// Insert all numbers
for (int i = 0; i < numbers.length; i++) {
// Read number from console
numbers[i] = input.nextInt();
// Check if number was inserted before
usedBefore[i] = false;
for(int k = 0; k < i; k++) {
if(numbers[k] == numbers[i]) {
usedBefore[i] = true;
break;
}
}
}
// Print all numbers that were not inserted before
for(int j = 0; j < numbers.length; j++) {
if(!usedBefore[i]) {
System.out.print(String.valueOf(numbers[j])+" ");
}
}
回答by Techwolf
The fastest and most concise and efficient way is to destructively parse the array for uniques using its first number as a magic value, after all other operations on it are complete:
最快、最简洁和有效的方法是在对数组的所有其他操作完成后,使用它的第一个数字作为魔法值破坏性地解析数组的唯一性:
Scanner input = new Scanner(System.in);
int magic = 0;
int[] numbers = new int[10];
for(int i = 0; i < 10; i++) {
System.out.println("Please enter a value: \n" + "[" + (i + 1) + "]: ");
numbers[i] = input.nextInt();
}
System.out.println("\nThe numbers you entered were: \n");
for(int i = 0; i < 10; i++) {
System.out.println(numbers[i] + ", ");
}
System.out.println("done.\n\n");
System.out.println("The unique numbers are: ");
magic = numbers[0];
System.out.println(magic + ", ");
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(numbers[i] == numbers[j] && j != i) {
numbers[j] = magic;
}
}
if(numbers[i] != magic) {
System.out.println(numbers[i] + ", ");
}
}
System.out.println("done.\n\n");
Yes, I have two answers - this one is significantly different from the other one, and is better, though it is much more difficult for beginners to understand. Both solutions are valid, however.
是的,我有两个答案——这个与另一个有很大的不同,而且更好,尽管初学者理解起来要困难得多。然而,这两种解决方案都是有效的。