File tree Expand file tree Collapse file tree 1 file changed +19
-8
lines changed Expand file tree Collapse file tree 1 file changed +19
-8
lines changed Original file line number Diff line number Diff line change 1- public class SumThreeNumbers {
1+ import java .util .Scanner ;
2+
3+ public class SumNumbers {
24 public static void main (String [] args ) {
3- int num1 = 5 ;
4- int num2 = 10 ;
5- int num3 = 15 ;
6-
7- int sum = num1 + num2 + num3 ;
8-
9- System .out .println ("The sum of " + num1 + ", " + num2 + ", and " + num3 + " is: " + sum );
5+ Scanner scanner = new Scanner (System .in );
6+ int sum = 0 ;
7+
8+ System .out .println ("Enter numbers (enter 0 to calculate the sum):" );
9+
10+ while (true ) {
11+ int number = scanner .nextInt ();
12+ if (number == 0 ) {
13+ break ; // Exit the loop if the user enters 0
14+ }
15+ sum += number ;
16+ }
17+
18+ System .out .println ("The sum of the numbers is: " + sum );
19+
20+ scanner .close (); // Don't forget to close the scanner
1021 }
1122}
You can’t perform that action at this time.
0 commit comments