0% found this document useful (0 votes)
13 views2 pages

01VK092008003190094 1

The document describes a Java program for a robust calculator that handles user input, performs basic arithmetic operations, and includes error handling for invalid inputs and division by zero. It allows continuous calculations until the user decides to stop. The program utilizes a loop and a switch statement to manage operations and user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views2 pages

01VK092008003190094 1

The document describes a Java program for a robust calculator that handles user input, performs basic arithmetic operations, and includes error handling for invalid inputs and division by zero. It allows continuous calculations until the user decides to stop. The program utilizes a loop and a switch statement to manage operations and user interactions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

/*A robust Java calculator program can be implemented by combining user input handling,

conditional logic for operations, looping for continuous calculations, and error handling for invalid
inputs or operations.*/

import java.util.Scanner;

public class RobustCalculator1


{

public static void main(String[] args)


{
Scanner scanner = new Scanner(System.in);
boolean continueCalculation = true;

System.out.println("Welcome to the Robust Java Calculator!");

while (continueCalculation)
{

System.out.print("Enter the first number: ");


double num1 = scanner.nextDouble();

System.out.print("Enter the operator (+, -, *, /): ");


char operator = scanner.next().charAt(0);

System.out.print("Enter the second number: ");


double num2 = scanner.nextDouble();

double result;

switch (operator)
{
case '+':
result = num1 + num2;
System.out.println("Result: " + num1 + " + " + num2 + " = " + result);
break;
case '-':
result = num1 - num2;
System.out.println("Result: " + num1 + " - " + num2 + " = " + result);
break;
case '*':
result = num1 * num2;
System.out.println("Result: " + num1 + " * " + num2 + " = " + result);
break;
case '/':
if (num2 == 0)
{
System.out.println("Division by zero is not allowed.");
}
else
{
result = num1 / num2;
System.out.println("Result: " + num1 + " / " + num2 + " = " + result);
}
break;
default:
System.out.println("Invalid operator. Please use +, -, *, or /.");
} //Closing of switch case

System.out.print("Do you want to perform another calculation? (yes/no): ");


String choice = scanner.next();
if (!choice.equals("yes"))
{
continueCalculation = false;
}
} //Closing of while loop
scanner.close();
System.out.println("Thank you for using the calculator!");
}
}

You might also like