基本数组-Java
我需要制作两个数组,分别称为A和B,都是int类型,大小为100。每个索引应该是一个介于0和100之间(含0和100)的随机数,然后比较两个数组,并说出相同数字的2在两个数组中出现的次数 这就是我目前所拥有的基本数组-Java,java,arrays,Java,Arrays,我需要制作两个数组,分别称为A和B,都是int类型,大小为100。每个索引应该是一个介于0和100之间(含0和100)的随机数,然后比较两个数组,并说出相同数字的2在两个数组中出现的次数 这就是我目前所拥有的 int count = 0; int [] A = new int [100]; int [] B = new int [100]; for(int i = 0; i < A.length; i++){ A [i] = (int)(Math.random()*101);
int count = 0;
int [] A = new int [100];
int [] B = new int [100];
for(int i = 0; i < A.length; i++){
A [i] = (int)(Math.random()*101);
System.out.println("Array A: " + i);
}
for(int i = 0; i < B.length; i++){
B [i] = (int)(Math.random()*101);
System.out.println("Array B: " + i);
}
if(A [i] == B [i]){
count++;
}
int count=0;
int[]A=新的int[100];
int[]B=新的int[100];
for(int i=0;i
我不知道如何显示两个数组中出现了多少次相同数字的2。您需要在两个数组中循环:
int count = 0;
int [] A = new int [100];
int [] B = new int [100];
for(int i = 0; i < A.length; i++){
A [i] = (int)(Math.random()*101);
System.out.println("Array A: " + i);
}
for(int i = 0; i < B.length; i++){
B [i] = (int)(Math.random()*101);
System.out.println("Array B: " + i);
}
// Loop through the first array
for(int i = 0; i < A.length; i++) {
// For each element in the first array, loop through the whole second one
for (int j = 0; j < B.length; j++) {
// If it's a match
if(A[i] == B[j])
count++;
}
}
System.out.println("Count: " + count);
int count=0;
int[]A=新的int[100];
int[]B=新的int[100];
for(int i=0;i
您需要在两个阵列之间循环:
int count = 0;
int [] A = new int [100];
int [] B = new int [100];
for(int i = 0; i < A.length; i++){
A [i] = (int)(Math.random()*101);
System.out.println("Array A: " + i);
}
for(int i = 0; i < B.length; i++){
B [i] = (int)(Math.random()*101);
System.out.println("Array B: " + i);
}
// Loop through the first array
for(int i = 0; i < A.length; i++) {
// For each element in the first array, loop through the whole second one
for (int j = 0; j < B.length; j++) {
// If it's a match
if(A[i] == B[j])
count++;
}
}
System.out.println("Count: " + count);
int count=0;
int[]A=新的int[100];
int[]B=新的int[100];
for(int i=0;i
或者,如果您不需要这两个阵列,只需执行以下操作:
int count = Random.ints(100, 0, 101).boxed().collect(toSet())
.retainAll(Random.ints(100, 0, 101).boxed().collect(toSet()))
.size();
或者,如果您不需要这两个阵列,您可以简单地执行以下操作:
int count = Random.ints(100, 0, 101).boxed().collect(toSet())
.retainAll(Random.ints(100, 0, 101).boxed().collect(toSet()))
.size();
开始时很好,但需要一个嵌套的for循环来检查每个索引 另外--确保在数组中打印出A[i]和B[i],否则只打印出索引的编号,而不是索引中的编号
int count = 0;
int[] A = new int[100];
int[] B = new int[100];
//Create the first array
for (int i = 0; i < A.length; i++) {
A[i] = (int)(Math.random() * 101);
System.out.println("Array A: " + A[i]);
}
//Create the second array
for (int i = 0; i < B.length; i++) {
B[i] = (int)(Math.random() * 101);
System.out.println("Array B: " + B[i]);
}
//Check the indexes and make sure they are all compared-- 10,000 comparisons are made
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B.length; j++) {
if(A[i] == B[j])
count++;
}
}
int count=0;
int[]A=新的int[100];
int[]B=新的int[100];
//创建第一个数组
for(int i=0;i
希望贴的是正确的。。。第一次在这个网站上发布代码,但我希望我能有所帮助
请记住,如果同一个数字被发布两次,您将获得超过100次的计数
祝你好运 开始时很好,但是需要一个嵌套的for循环来检查每个索引 另外--确保在数组中打印出A[i]和B[i],否则只打印出索引的编号,而不是索引中的编号
int count = 0;
int[] A = new int[100];
int[] B = new int[100];
//Create the first array
for (int i = 0; i < A.length; i++) {
A[i] = (int)(Math.random() * 101);
System.out.println("Array A: " + A[i]);
}
//Create the second array
for (int i = 0; i < B.length; i++) {
B[i] = (int)(Math.random() * 101);
System.out.println("Array B: " + B[i]);
}
//Check the indexes and make sure they are all compared-- 10,000 comparisons are made
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < B.length; j++) {
if(A[i] == B[j])
count++;
}
}
int count=0;
int[]A=新的int[100];
int[]B=新的int[100];
//创建第一个数组
for(int i=0;i
希望贴的是正确的。。。第一次在这个网站上发布代码,但我希望我能有所帮助
请记住,如果同一个数字被发布两次,您将获得超过100次的计数
祝你好运 您将需要第三个数组来保存结果。对于循环,您需要一个复合
,外循环将循环第一个数组中的每个元素,内循环将循环第二个数组中的每个元素,比较每个元素,每次找到匹配项时,您需要在第三个数组中增加结果。您需要第三个数组来保存结果。循环需要一个复合,外循环将循环第一个数组中的每个元素,内循环将循环第二个数组中的每个元素,比较每个元素,每次找到匹配项时,需要增加第三个数组中的结果如果a和B中的所有元素值都为零,计数将为10000。如果A和B中的所有元素值均为零,则计数将为10000。