Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- System.out.println();
- Scanner input = new Scanner(System.in);
- int num1 = 0;
- int num2 = 0;
- System.out.print("Please input your first whole number: ");
- if (input.hasNextInt()) {
- num1 = input.nextInt();
- } else {
- System.out.println("\nThis is not a correct number, the program will now terminate");
- System.exit(0);
- }
- System.out.print("Please input your second whole number: ");
- if (input.hasNextInt()) {
- num2 = input.nextInt();
- } else {
- System.out.println("\nThis is not a correct number, the program will now terminate");
- System.exit(0);
- }
- System.out.println("\nFrom the menu below, enter the number of the operation you would like to perform:");
- System.out.println("\t1) Add");
- System.out.println("\t2) Subtract");
- System.out.println("\t3) Multiply");
- System.out.println("\t4) Divide (to the nearest whole number)");
- System.out.print("> ");
- int symbol = input.nextInt();
- System.out.println();
- if (symbol == 1) {
- System.out.println("You have requested: " + num1 + " + " + num2);
- System.out.println("And the answer is: " + (num1 + num2));
- }else if (symbol == 2) {
- System.out.println("You have requested: " + num1 + " - " + num2);
- System.out.println("And the answer is: " + (num1 - num2));
- }else if (symbol == 3) {
- System.out.println("You have requested: " + num1 + " x " + num2);
- System.out.println("And the answer is: " + (num1 * num2));
- } else if (symbol == 4){
- System.out.println("You have requested: " + num1 + " / " + num2);
- if (num2 == 0) {
- System.out.println("You cannot divide by zero, the program will now terminate.");
- System.exit(0);
- } else {
- System.out.println("And the answer is (rounded to the nearest whole number): " + (num1 / num2));
- }
- }else {
- System.out.println("Incorrect option selected, the program will now terminate");
- System.exit(0);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement