怎样才能知道哪个数字是重复的呢?

huangapple go评论135阅读模式
英文:

How can I know what number is repeated?

问题

package exercici10;

import java.util.Scanner;

public class isRepeatedOrNot {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int[] myArray = new int[10];

        int[] myArray2 = new int[10];
        for (int i = 0; i < 10; i++) {
            myArray2[i] = myArray[i];
        }

        System.out.print("Enter 10 numbers: ");

        // Get myArray.
        for (int i = 0; i < 10; i++) {
            myArray[i] = sc.nextInt();
        }

        // Print myArray.
        for (int i = 0; i < myArray.length; i++) {
            for (int j = 0; j < myArray2.length; j++) {
                if (myArray[i] == myArray2[j]) {
                    System.out.println("Is repeated.");
                    return;
                }
            }
        }
        System.out.println("No numbers repeated.");

        sc.close();
    }
}

谢谢。

祝好,
Alex。

英文:

I'm trying to know what number is repeated in Java.

I'm using 1.8JDK and never enter in IF.
I don't know how can I do for program works, I'm hardstucked.

Can you check my code?

package exercici10;

import java.util.Scanner;

public class isRepeatedOrNot {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		int[] myArray = new int[10];

		int[] myArray2 = new int[10];
		for (int i = 0; i &lt; 10; i++) {
			myArray2[i] = myArray[i];
		}

		System.out.print(&quot;Enter 10 numbers: &quot;);

		// Get myArray.
		for (int i = 0; i &lt; 10; i++) {
			myArray[i] = sc.nextInt();
		}

		// Print myArray.
		for (int i = 0; i &lt; myArray.length; i++) {
			for (int j = 0; j &lt; myArray2.length; j++) {
				if (myArray[i] == myArray2[j]) {
					System.out.println(&quot;Is repeated.&quot;);
					return;
				}
			}
		}
		System.out.println(&quot;No numbers repeated.&quot;);

		sc.close();
	}
}

Thank you.

Regards,
Alex.

答案1

得分: 1

以下是翻译好的部分:

你不需要有两个数组,只需要使用一个数组和一个函数来检查是否有数字重复:

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] myArray = new int[10];

    System.out.print("输入10个数字:");

    // 获取 myArray 数组。
    for (int i = 0; i < 10; i++) {
        myArray[i] = sc.nextInt();
    }

    System.out.println(isNumberRepeated(myArray));

    sc.close();
}

public static boolean isNumberRepeated(int[] array) {
    for (int i = 0; i < array.length; i++) {
        for (int j = i + 1; j < array.length; j++) {
            if (array[i] == array[j]) {
                return true;
            }
        }
    }
    return false;
}

输入:

输入10个数字:1
2
3
4
5
6
7
8
9
10
false

输入2:

输入10个数字:1
2
3
4
5
54
6
7
54
9
true
英文:

You do not need to have two arrays, it is enough to use one array and a function that will check if there is a repetition of a number:

public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
        int[] myArray = new int[10];

        System.out.print(&quot;Enter 10 numbers: &quot;);

        // Get myArray.
        for (int i = 0; i &lt; 10; i++) {
            myArray[i] = sc.nextInt();
        }

        
        System.out.println(isNumberRepeated(myArray));

        sc.close();
	}
	
	public static boolean isNumberRepeated(int[] array)
	{
		for (int i=0; i&lt;array.length; i++) {
		    for (int j=i+1; j&lt;array.length; j++) {
		        if (array[i]==array[j]) {
		            return true;
		            }
		    }  
	}
		return false;
	}
}

Input :

Enter 10 numbers: 1
2
3
4
5
6
7
8
9
10
false

Input 2 :

Enter 10 numbers: 1
2
3
4
5
54
6
7
54
9
true

答案2

得分: 1

你可以使用HashSet,它不允许重复的值。

public static void main(String[] args) {
    Integer[] myArray = {1, 2, 3, 1, 3, 4, 5, 7, 6, 3, 2};
    HashSet<Integer> uniqueNumbers = new HashSet<>();
    HashSet<Integer> repeatedNumbers = new HashSet<>();

    for (Integer integer : myArray) {
        if (uniqueNumbers.contains(integer)) {
            repeatedNumbers.add(integer);
        } else {
            uniqueNumbers.add(integer);
        }
    }
    System.out.println(repeatedNumbers);
}

另外,如果你需要修改代码以显示每个数字重复的次数,你可以使用hashmap。

英文:

You could use HashSet which doesn't allow duplicate values.

public static void main(String[] args) {
    Integer[] myArray = {1, 2, 3, 1, 3, 4, 5, 7, 6, 3, 2};
    HashSet&lt;Integer&gt; uniqueNumbers = new HashSet&lt;&gt;();
    HashSet&lt;Integer&gt; repeatedNumbers = new HashSet&lt;&gt;();

    for (Integer integer : myArray) {
        if (uniqueNumbers.contains(integer)) {
            repeatedNumbers.add(integer);
        } else {
            uniqueNumbers.add(integer);
        }
    }
    System.out.println(repeatedNumbers);
}

Also if you need to modify your code to show number of times every number repeated, you could use hashmap.

huangapple
  • 本文由 发表于 2020年10月16日 23:12:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/64391856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定