2015-05-25 115 views
-1

我試圖讓用戶輸入元件的數量arrAarrB應該有,也使用戶選擇他們想要在這兩個arrB每個對應的元素,arrA和INT數。然後,使用arrAarrB中的對應元素的總和創建第三個arrC,然後打印arrAarrBarrC添加兩個數組的相應元素到第三陣列

輸出應該是這樣的:

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 

這是到目前爲止,我寫的代碼,我需要幫助,我怎麼會用掃描儀來讓用戶選擇的arrAarrB輸入長度以及arrAarrB中的元素。這是代碼的樣子至今: -

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]); 
    } 
} 

回答

1

讓假設你只有2個陣列,可以很容易和不嵌套循環,當你可以理解這些代碼,你可以用一個新的循環包裝所有的方法,並創建無限的數組來總結結果,如果你想...但你必須先了解基礎知識:

創建一個掃描儀,並要求用戶陣列的長度:

Scanner sc = new Scanner(System.in); 
// ask user! 
System.out.println("Input the length:"); 
int arrayLength = in.nextInt(); 

給定lenght創建陣列

int[] fistArray = new int[arrayLength]; 
int[] secondArray = new int[arrayLength]; 
int[] totals = new int[arrayLength]; 

填充拳頭陣列迭代位置從0到由用戶輸入的號碼:

for (int i = 0; i < arrayLength; i ++) { 
    System.out.println("Enter a value for first array, position "+ i); 
    try { 
     firstArray[i] = Integer.parseInt(sc.nextLine()); 
    } catch (Exception e) { 
     System.out.println("Not a valid number!!!); 
     i --; 
    } 
} 

從0填充第二陣列迭代位置到由輸入的數字用戶並獲得每個pos的總和:

for (int i = 0; i < in.nextInt(); i ++) { 
    System.out.println("Enter a value for second array, position "+ i); 
    try { 
     secondArray[i] = Integer.parseInt(sc.nextLine()); 
     totals[i] = fistArray[i] + secondArray[i]; 
    } catch (Exception e) { 
     System.out.println("Not a valid number!!!); 
     i --; 
    } 
} 

個並打印結果:

System.out.println(Arrays.toString(firstArray));   
System.out.println(Arrays.toString(secondArray));  
System.out.println(Arrays.toString(totalsArray)); 

最後,不要忘記關閉您的掃描儀,以避免內存泄漏是指出drgPP這樣:

sc.close(); 
+1

掃描儀應該關閉以避免內存泄漏。 – drgPP

-1

也許這是你的問題:

Scanner scan = new Scanner(System.in); 

System.out.println("Enter size: "); 
int size =scan.nextInt(); 

Integer[] arrA = new Integer[size]; 
ArrayList<Integer> arrB = new ArrayList<Integer>(size); 
0

下面的代碼應該做你想要的東西:

import java.util.*; 

class ArrayArithmetic 
{ 
    public static void main (String[] args) 
    { 
     Scanner in = new Scanner(System.in); 
     System.out.print("Input the length "); 
     int len = in.nextInt(); 

     int[] arrA = new int[len]; 
     int[] arrB = new int[len]; 
     int[] sum = new int[len]; 
     for (int i = 0; i < len; i++){ 
     System.out.print("Enter a value for first array, position " + i + ": "); 
     arrA[i] = in.nextInt(); 
    } 
    for (int i = 0; i < len; i++){ 
     System.out.print("Enter a value for second array, position " + i + ": "); 
     arrB[i] = in.nextInt(); 
    } 

    for(int i = 0; i < arrA.length; i++) 
    { 
    for(int j = 0; i < arrB.length; i++) 
    { 
     sum[i] = arrA[i] + arrB[i]; 

    } 


} 

System.out.println("sum: " + sum[0]+"," + sum[1] + "," + sum[2] + "," + sum[3]); 


} } 
0
public static void main (String[] args){ 
      Scanner sc = new Scanner(System.in); 
      System.out.println("Length of arrays: "); 
      try { 
       //Initializing length of array 
       int length = sc.nextInt(); 
       //Constructing our arrays based on length 
       int[] arrA = new int[length]; 
       int[] arrB = new int[length]; 
       int[] arrSum = new int[length]; 

       //Populating our array A via a loop 
       for (int i=0; i<arrA.length; i++) { 
        System.out.println("Values for arrA at index: "+i); 
        int value = sc.nextInt(); 
        arrA[i]=value; 
       } 
       //Populating our array B via a loop 
       for (int i=0; i<arrB.length; i++) { 
        System.out.println("Values for arrB at index: "+i); 
        int value = sc.nextInt(); 
        arrB[i]=value; 
       } 
       //Call the method to calcualte our sum which will be in sum array 
       arrSum = makeSum(arrA, arrB, length); 

       System.out.println(Arrays.toString(arrSum)); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } finally { 
        sc.close(); 
       } 

    } 

    // Method to calculate our Sum Array based on the length and the Array A and B 
    public static int[] makeSum (int[] arrA, int[] arrB, int length) { 
     int[] arrSum = new int[length]; 
     for (int i=0; i<arrA.length; i++) { 
      for (int j=0; j<arrB.length; j++) { 
       arrSum[j]=arrA[i]+arrB[j]; 
      } 
     } 
     return arrSum; 
    }