For each of the following exercises please write complete Ja
For each of the following exercises, please write complete Java program with a header and proper in-line comments. Names the programs as indicated below. Design and implement a Java program (name it inputSum) 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 Sun: 48 Entered Number: 1, 1, 1, 1, 100 The Sun: 104 Entered Number: 0, 0, 0, 0, 0, 100 The Sun: 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.
Solution
InputSum.java
import java.util.Scanner;
public class InputSum {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(\"Enter the numbers: \");
String s = \"\";
int sum = 0;
while(true){
int n = scan.nextInt();
if(n == -1){
break;
}
else{
sum = sum + n;
s = s + n + \", \";
}
}
System.out.println(\"Entered Number: \"+s.substring(0, s.length()-2));
System.out.println(\"The sum: \"+sum);
}
}
Output:
Enter the numbers:
10 2 13 50 100 -1
Entered Number: 10, 2, 13, 50, 100
The sum: 175
