我試圖讓用戶輸入元件的數量arrA
和arrB
應該有,也使用戶選擇他們想要在這兩個arrB
每個對應的元素,arrA
和INT數。然後,使用arrA
和arrB
中的對應元素的總和創建第三個arrC
,然後打印arrA
,arrB
和arrC
。添加兩個數組的相應元素到第三陣列
輸出應該是這樣的:
Input the length: 5
Enter a value for first array, position 0: 1
Enter a value for first array, position 1: 6
Enter a value for first array, position 2: 13
Enter a value for first array, position 3: -3
Enter a value for first array, position 4: 8
Enter a value for second array, position 0: 9
Enter a value for second array, position 1: -4
Enter a value for second array, position 2: 1
Enter a value for second array, position 3: 65
Enter a value for second array, position 4: 18
first: 1 6 13 -3 8
second: 9 -4 1 65 18
result: 10 2 14 62 26
這是到目前爲止,我寫的代碼,我需要幫助,我怎麼會用掃描儀來讓用戶選擇的arrA
和arrB
輸入長度以及arrA
和arrB
中的元素。這是代碼的樣子至今: -
class ArrayArithmetic
{
public static void main (String[] args)
{
int[] arrA = { 11, -27, 89, 17};
int[] arrB = {-3, 24, -9, -16};
int[] sum = { 0, 0, 0, 0};
for(int i = 0; i < arrA.length - 1; i++)
{
for(int j = 0; i < arrB.length - 1; i++)
{
sum[i] = arrA[i] + arrB[i];
}
}
System.out.println("sum: " + sum[0]+"," + sum[1] + "," + sum[2] + "," + sum[3]);
}
}
掃描儀應該關閉以避免內存泄漏。 – drgPP