Explore Westonci.ca, the top Q&A platform where your questions are answered by professionals and enthusiasts alike. Join our Q&A platform and get accurate answers to all your questions from professionals across multiple disciplines. Discover detailed answers to your questions from a wide network of experts on our comprehensive Q&A platform.

How to add up multiple user inputs for example: If i ask the user How many numbers do you want to add? they say 5 then i out put Enter 5 numbers: how can i get it to add up those 5 numbers that were input.In JAVA

Sagot :

Answer:

import java.util.Scanner;

class Main {

 public static void main(String[] args) {

   Scanner scanner = new Scanner(System.in);

   System.out.println("How many numbers do you want to add?");

   int numberOfNumbers = scanner.nextInt();

   int arrayNumbers[] = new int[numberOfNumbers];

   System.out.println("Enter " + numberOfNumbers + " numbers:\n");

   for(int i = 0; i < numberOfNumbers; i++){

     int addToArray = scanner.nextInt();

     arrayNumbers[i] = addToArray;

   }

   int sum = 0;

   for(int j = 0; j < arrayNumbers.length; j++){

     sum+=arrayNumbers[j];

   }

   System.out.println("Sum: " + sum);

 }

}

Explanation: