2013-06-16 13 views

Odpowiedz

13

potrzebował:

public int sumAll(int...numbers){ 

    int result = 0; 
    for(int i = 0 ; i < numbers.length; i++) { 
     result += numbers[i]; 
    } 
    return result; 
} 

Następnie wywołujemy metodę i dać jak najwięcej wartości int, jak trzeba:

int result = sumAll(1,4,6,3,5,393,4,5);//..... 
System.out.println(result); 
+0

Dziękujemy! Musiałem użyć publicznego statycznego int sumAll – browngirl

+0

Czy możemy to zrobić w czasie krótszym niż O (n)? –

2

Zastosowanie var args

public long sum(int... numbers){ 
     if(numbers == null){ return 0L;} 
     long result = 0L; 
     for(int number: numbers){ 
     result += number; 
     } 
     return result; 
} 
+0

Brakujące coś '...' – user2246674

+0

Co powiesz na var args? –

+0

Co więc, jeśli suma przepełnia wartość całkowitą ;-) –

9
public int sumAll(int... nums) { //var-args to let the caller pass an arbitrary number of int 
    int sum = 0; //start with 0 
    for(int n : nums) { //this won't execute if no argument is passed 
     sum += n; // this will repeat for all the arguments 
    } 
    return sum; //return the sum 
} 
+1

+1 dla czystszej pętli. –

+0

Dziękuję. Bardziej mi odpowiada inna notacja dla pętli – browngirl

0
public static void main(String args[]) 
{ 
System.out.println(SumofAll(12,13,14,15));//Insert your number here. 
    { 
     public static int SumofAll(int...sum)//Call this method in main method. 
      int total=0;//Declare a variable which will hold the total value. 
      for(int x:sum) 
      { 
      total+=sum; 
      } 
    return total;//And return the total variable. 
    } 
} 
0

Można zrobić, zakładając, że tablica zawierająca wartości tablicy i długości: arrayVal[i], arrayLength:

int sum = 0; 
for (int i = 0; i < arrayLength; i++) { 
    sum += arrayVal[i]; 
} 

System.out.println("the sum is" + sum); 

Mam nadzieję, że to pomaga.

+0

Zastanów się nad dodaniem podpisu metody, aby poprawić jakość odpowiedzi. –

+0

Nie potrzebujesz zmiennej dla długości tablicy, masz tę wartość w swojej tablicy. –

12

Jeśli używasz Java8 można użyć IntStream:

int[] listOfNumbers = {5,4,13,7,7,8,9,10,5,92,11,3,4,2,1}; 
System.out.println(IntStream.of(listOfNumbers).sum()); 

wyniki:

zaledwie 1 wiersz kodu, który podsumuje tablicę.

1
import java.util.Scanner; 

public class SumAll { 
    public static void sumAll(int arr[]) {//initialize method return sum 
     int sum = 0; 
     for (int i = 0; i < arr.length; i++) { 
      sum += arr[i]; 
     } 
     System.out.println("Sum is : " + sum); 
    } 

    public static void main(String[] args) { 
     int num; 
     Scanner input = new Scanner(System.in);//create scanner object 
     System.out.print("How many # you want to add : "); 
     num = input.nextInt();//return num from keyboard 
     int[] arr2 = new int[num]; 
     for (int i = 0; i < arr2.length; i++) { 
      System.out.print("Enter Num" + (i + 1) + ": "); 
      arr2[i] = input.nextInt(); 
     } 
     sumAll(arr2); 
    } 

} 
Powiązane problemy