|
| 1 | +import java.util.Scanner; // importing the scanner from java.util |
| 2 | + |
| 3 | +class simplecalculator{ // Creating a class |
| 4 | + public static void main(String[] args) { |
| 5 | + Scanner userinput = new Scanner(System.in); // Creating an object for userinput. |
| 6 | + |
| 7 | + while(true){ // Infinite loop to allow multiple calculations until user exits. |
| 8 | + |
| 9 | + // displaying the calculator header |
| 10 | + System.out.println(" \n -------------------"); |
| 11 | + System.out.println(" | Simple Calculator |"); |
| 12 | + System.out.println(" -------------------"); |
| 13 | + |
| 14 | + // Displaying the available operations |
| 15 | + System.out.println("\nselect your choice of operation from the below options (1 - 4)numbers only:"); |
| 16 | + System.out.println("----------------------------------------------------------------------------"); |
| 17 | + System.out.println("1.)Addition\n2.)substraction\n3.)multplication\n4.)division"); |
| 18 | + System.out.println("enter your choice from (1- 4) : "); |
| 19 | + |
| 20 | + // looking for the invalid input for choice ( Ensuring that the user can only select the numeric only values) |
| 21 | + if(!userinput.hasNextInt()){ |
| 22 | + System.out.println(" Invalid Input : you cannot select non - numeric values "); |
| 23 | + System.out.println("\nselect your choice of operation from the options (1 - 4)numbers only:"); |
| 24 | + userinput.next(); |
| 25 | + continue; // Restart the loop again. |
| 26 | + } |
| 27 | + |
| 28 | +// Exit the loop here by seledcting number "0" |
| 29 | + int choice = userinput.nextInt(); |
| 30 | + if(choice == 0){ |
| 31 | + System.out.println(" Thankyou for using simple calculator. !!! GOODBYE !!!"); |
| 32 | + break; |
| 33 | + } |
| 34 | + |
| 35 | +// checking for the invalidation of choice. |
| 36 | + if (choice >4) { |
| 37 | + System.out.println("Invalid choice! Please select a valid option."); |
| 38 | + continue; // Restart the loop again. |
| 39 | + } |
| 40 | + |
| 41 | +// Reading the first number |
| 42 | + System.out.println("Enter your first number:"); |
| 43 | + if (!userinput.hasNextDouble()) { // Checking if input is a valid number |
| 44 | + System.out.println("Invalid input! Please enter a valid number."); |
| 45 | + userinput.next(); // Clear invalid input |
| 46 | + continue; // Restart the loop |
| 47 | + } |
| 48 | + double num1 = userinput.nextDouble(); // Reading first number |
| 49 | + |
| 50 | + |
| 51 | + // Reading the second number |
| 52 | + System.out.println("Enter your Second number:"); |
| 53 | + if (!userinput.hasNextDouble()) { // Checking if input is a valid number |
| 54 | + System.out.println("Invalid input! Please enter a valid number."); |
| 55 | + userinput.next(); // Clear invalid input |
| 56 | + continue; // Restart the loop |
| 57 | + } |
| 58 | + double num2 = userinput.nextDouble(); // Reading Second number |
| 59 | + double result; |
| 60 | + |
| 61 | + // Performing the selected operation |
| 62 | + if(choice==1){ // ADDITION |
| 63 | + result = num1+num2; |
| 64 | + System.out.println("The addition of "+num1+" + "+num2+" = "+result); |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + else if(choice==2){ //SUBSTRACTION |
| 69 | + result = num1-num2; |
| 70 | + System.out.println("The Substraction of "+num1+" + "+num2+" = "+result); |
| 71 | + } |
| 72 | + |
| 73 | + else if(choice==3){ //MULTIPLICATION |
| 74 | + result = num1*num2; |
| 75 | + System.out.println("The Multiply of "+num1+" + "+num2+" = "+result); |
| 76 | + } |
| 77 | + |
| 78 | + else if(choice==4){ // DIVISION |
| 79 | + if(num1==0 || num2==0){ // Checking the num1 and num2 for input "0". Because the division is not possible with zero. |
| 80 | + System.out.println("Error : Division by zero is not allowed."); |
| 81 | + } |
| 82 | + |
| 83 | + else{ |
| 84 | + result = num1/num2; |
| 85 | + System.out.println("The Division of "+num1+" + "+num2+" = "+result); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + userinput.close(); // Closing the Scanner to prevent resource leaks |
| 90 | +} |
| 91 | +} |
0 commit comments