/home/caleb/ASDV-Java/Assignments/lab11_CalebFontenot/src/lab11_calebfontenot/DoWhileEvent3.java |
nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt
nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java
package lab11_calebfontenot;
import java.util.Scanner;
public class DoWhileEvent3 {
public static void main(String[] args) {
int sum = 0, numInt = 0;
String num = "This can be literally anything, it just needs to be initialized";
Scanner input = new Scanner(System.in);
do {
System.out.print("Enter an integer to add to the sum (type x to exit): ");
num = input.nextLine();
try {
numInt = Integer.parseInt(num);
} catch (Exception NumberFormatException) {
if (num.toLowerCase().equals("x")) {
break;
} else;
System.out.println("Invalid input!");
}
sum += numInt;
System.out.println("Current sum: " + sum);
} while (!(num.toLowerCase().equals("x")));
System.out.println("Exited loop!");
System.out.println("Final sum: " + sum);
}
}