Westonci.ca is the best place to get answers to your questions, provided by a community of experienced and knowledgeable experts. Find reliable answers to your questions from a wide community of knowledgeable experts on our user-friendly Q&A platform. Experience the convenience of finding accurate answers to your questions from knowledgeable experts on our 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:

Thank you for visiting our platform. We hope you found the answers you were looking for. Come back anytime you need more information. Thanks for stopping by. We strive to provide the best answers for all your questions. See you again soon. Thank you for choosing Westonci.ca as your information source. We look forward to your next visit.