英文:
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 < 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();
}
}
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("Enter 10 numbers: ");
// Get 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;
}
}
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<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);
}
Also if you need to modify your code to show number of times every number repeated, you could use hashmap.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论