[ SOLVED ]
So, I'm trying to have the program display " You can not divide by zero " When the user divides by zero. ive read my book that i have ( purchased from school) - And i cant figure it out.
Once the user Enters " 4 " as the menuNumber - It goes into the calculator as being a division problem.
Im not really sure how to approach this, thus why i need some help.
I will be posting the full code below. Im not sure if i shouldnt, but i really cant figure this out D:
Now everything works- I just cant figure out the diving by zero issue." import java.util.Scanner; public class BasicCalculator { // Main menu public static void main(String[] args) { //Create Scanner Scanner input = new Scanner(System.in); System.out.println("Menu: "); System.out.println("1. Add "); System.out.println("2. Sub "); System.out.println("3. Multiply "); System.out.println("4. Divide "); System.out.println("5. Generate a Random Number"); // Complete the input on what you would like to do. System.out.println(" What would you like to do?"); int menuNumber = input.nextInt(); if (menuNumber < 1 ) { System.out.println("Im Sorry, You must choose Between 1 and 5."); } if (menuNumber > 5 ) { System.out.println("Im Sorry, You must choose Between 1 and 5."); } if (menuNumber == 1) { System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); int answer = number1 + number2; System.out.print("Answer : "+answer ); } if (menuNumber == 2) { System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); int answer = number1 - number2; System.out.print("Answer : " +answer ); } if (menuNumber == 3){ System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); int answer = number1 * number2; System.out.print("Answer : " + answer ); } if (menuNumber == 4){ System.out.println("What is the first Number?:"); int number1 = input.nextInt(); System.out.println("What is the Second Number?:"); int number2 = input.nextInt(); System.out.print("Answer : " + number1 / number2); if (number2 <= 0) { System.out.println("You can Not divide by zero."); } } if (menuNumber == 5){ System.out.println("What is the lower limit?:"); int min = input.nextInt(); System.out.println("What is the upper limit?:"); int max = input.nextInt(); int answer = (int) (Math.ceil(Math.random() * (max - min)) + min); System.out.println("Answer: " + answer); } } } "
If you have a link to a page that will help me, please do. I would like to understand what i need to do.