[英]Subtracting elements of an array from another array
我对此相当陌生,因此将不胜感激。
如果数组 'a' 的元素大于数组 'b' 的相应元素,我正在尝试从数组 'a' 中减去数组 'b' 的元素(不是删除而是减去)。
我没有得到所需的输出,它只是打印了我输入的数组
Scanner sc = new Scanner(System.in);
short n = sc.nextShort();
short a[] = new short[n];
short b[] = new short[n];
for (short i = 0; i < n; i++) {// taking elements input
a[i] = sc.nextShort();
}
for (short i = 0; i < n; i++) {// taking elements input
b[i] = sc.nextShort();
}
short m = 0;
for (short i = 0; i < n; i++) {// finding smallest element in array 'a'
for (short j = 0; j < n; j++) {
if (a[i] < a[j]) {
m = a[i];
}
}
}
boolean allequal = false;
while (!allequal) {
for (short i = 0; i < n; i++) {// subtracting elements
if (a[i] == m)
continue;
if (a[i] >= b[i]) {
a[i] -= b[i];
}
}
for (short i = 0; i < n; i++) {
for (short j = 0; j < n; j++) {
if (a[i] == a[j]) {
allequal = true;
} else {
allequal = false;
}
}
}
}
for (int i = 0; i < n; i++) {// printing array 'a'
System.out.print(a[i] + " ");
}
5
5 7 10 5 15
2 2 1 3 5
5 5 9 5 10
您的程序没有进入 while 循环,因为您在while (allequal = false) {
错误地使用了=
运算符,这是赋值,而不是比较。 正确的形式是allequal == false
,它重写为!allequal
。 我没有检查剩余的代码。
请注意,您应该使用良好的 IDE,它可以防止您犯此类错误,并提供可以轻松发现自己的调试器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.