【发布时间】:2019-04-21 23:42:26
【问题描述】:
我有两个数组。我必须显示两个数组,然后在其中找到共同的整数。如果有,请显示它们。如果没有显示0或者写一个输出
我有 for 循环来执行此操作,但由于 else 在循环内,它循环该语句。我将如何使它只显示该语句一次?顺便说一句,没有通用整数,因此该语句可以正确显示...只是由于某种原因而循环。
static void PrintF(int[] MyNumbers, int[] OtherNumbers) {
System.out.println("");
System.out.println("The first array's numbers are "+(Arrays.toString(MyNumbers))+" and the second array's numbers are "+(Arrays.toString(OtherNumbers))+".");
for(int i = 0; i < MyNumbers.length; i++) {
for(int j = 0; j < OtherNumbers.length; j++) {
if(MyNumbers[i] == OtherNumbers[j]) {
System.out.println(MyNumbers[i]);
}
else {
System.out.print("There are no common intergers between MyNumbers and OtherNumbers.");
}
}
}
}
【问题讨论】:
-
你总是得到 System.out.print("MyNumbers 和 OtherNumbers 之间没有共同的整数。"); ?
-
是的,数组是 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 和 [11, 12, 13, 14, 15]。该代码循环“MyNumbers 和 OtherNumbers 之间没有共同的整数。”
-
没错,有什么问题?
-
我只希望它显示 system.out.print 一次。
-
查看答案.....
标签: arrays loops for-loop if-statement