Java数组:寻找独特的数字在一组10个输入的号码 [英] Java Arrays: Finding Unique Numbers In A Group of 10 Inputted Numbers

查看:113
本文介绍了Java数组:寻找独特的数字在一组10个输入的号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里的损失。

我有这样的家庭作业,我必须让用户输入10个号码,将它们放置在一个数组,并找出哪些输入的号码是唯一的。

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.

这是我的工作流程现在:输入号码>如果号码没有前阵输入,存储;如果一些之前已经输入,忽略>显示输入的号码>显示唯一的编号

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

恩:输入1 2 3 5 1 2 4 6会发现独特的数字,显示1 2 3 4 5 6

ex: Inputting 1 2 3 5 1 2 4 6 would find the unique numbers and show "1 2 3 4 5 6"

到目前为止,我的code是这样的:

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.

推荐答案

检查数字在他们进入,然后跟踪哪些的是在第二个(布尔)阵列<$ C $标记相同的位置独一无二C>真正如果他们是独一无二的,其他。

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.

然后,当你打印出来的独特的价值观,只打印在每个位置值号码[] 如果的唯一身份那个位置[] 包含真正

Then, when you print out the unique values, only print the value from each position in numbers[] if that position in uniques[] contains 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");

这篇关于Java数组:寻找独特的数字在一组10个输入的号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆