Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How do I solve this? Design and implement a Java program that prompts the user t

ID: 3757740 • Letter: H

Question

How do I solve this?

Design and implement a Java program that prompts the user to enter a positive integer number. The program should accept integers until the user enters the value -1 (negative one). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output.

       Entered Number: 10, 2, 13, 50, 100

       The Sum:        175

       Entered Number: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

       The Sum:         48

       Entered Number: 1, 1, 1, 1, 100

       The Sum:         104

       Entered Number: 0, 0, 0, 0, 0, 100

       The Sum:         100

Make sure the program validates each entered number before processing it as the user may enter negative numbers other than the sentential value -1. Design your program such that it allows the user to re-run the program with a different set of inputs as shown above. Document your code, and organize and space out your outputs as shown above.

Explanation / Answer

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Numbers
{
   public static void main (String[] args) throws java.lang.Exception
   {
       // your code goes here
       List<Integer> a1 = new ArrayList<Integer>();
       char again;
       Scanner input = new Scanner(System.in);
       int number,sum = 0;
       while(true)
       {
       while(true)
       {
           System.out.println("Enter a positive number (-1 to exit) : ");
           number = input.nextInt();
           if(number == -1)
               break;
           else if(number>=0)
               a1.add(number);
       }
       System.out.print("Entered Numbers : ");
       for (int temp : a1) {
           System.out.print(temp+",");
           sum = sum + temp;
       }
       System.out.print(" ");
       System.out.println(" The Sum : " +sum);
      
       System.out.println("Do you want to continue ? (y/n)");
       again = input.next().charAt(0);
      
       if(again == 'y')
           continue;
       else
           break;
       }   
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote