i have a calculator class and i want the operation input to be specific to these strings "+, -, /, *" if its not, i want to print a message to enter the operator again.
and please if someone would lessen my code please do it. thanks
here is my code
import java.util.Scanner;
public class Calculator1 {
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
String operation;
Double fnum, lnum, answer;
System.out.println("Enter first Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter first Number: ");
}
fnum = num.nextDouble();
System.out.println("Enter Operation: ");
//HERE IM BEING CONFUSED
while (!num.equals("+, -, /, *"))
{
num.next();
System.out.print("Enter Operator: ");
}
operation = num.next();
//End
System.out.println("Enter Second Number: ");
while (!num.hasNextDouble())
{
num.next();
System.out.print("Enter Second Number: ");
}
lnum = num.nextDouble();
switch (operation) {
case "+":
answer = fnum + lnum;
System.out.print("Equals= " + answer);
break;
case "-":
answer = fnum - lnum;
System.out.print("Equals= " + answer);
break;
case "*":
answer = fnum * lnum;
System.out.println("Equals= " + answer);
break;
case "/":
answer = fnum / lnum;
System.out.println("Equals= " + answer);
break;
default:
System.out.println("wrong operator");
break;
}
}
}
This is a way to use operators with SWITCH sentence.
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
double v1,v2;
String v3;
System.out.print("Enter first number: ");
v1 = num.nextDouble();
System.out.print("Enter second number: ");
v2 = num.nextDouble();
System.out.print("Enter operation [+] [-] [*] [/]: ");
v3 = num.next();
System.out.println( ("+".equals(v3) ) ? Operators.ADD.calculate(v1, v2) : "....");
}
public enum Operators {
ADD;
double calculate(double x, double y) {
switch (this) {
case ADD:
return x + y;
default:
throw new AssertionError("Unknown operations " + this);
}
}
}
Related
Hello,
I was trying to build a simple calculator using basic Java code and understand OOP better, so, I wrote this:
import java.util.Scanner;
public class Calc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
int num1 = input.nextInt();
System.out.println("Enter an operation: ");
String opr;
opr = input.nextLine();
if(opr == "+") {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.addition();
} else if (opr == "-") {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.subtraction();
} else if (opr == "*") {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.multiplication();
} else if (opr == "/") {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.division();
} else {
System.out.println("Please, Enter a valid operation!");
}
}
}
and another Class for math operations:
import java.util.Scanner;
public class Operation {
static int Num1;
static int Num2;
Scanner input = new Scanner(System.in);
public Operation(int x, int y) {
x = Num1;
y = Num2;
}
public void addition() {
System.out.println(Num1 + Num2);
}
public void subtraction() {
System.out.println(Num1 - Num2);
}
public void multiplication() {
System.out.println(Num1 * Num2);
}
public void division() {
System.out.println(Num1 / Num2);
}
}
but it doesn't take input for the operation, and it goes straight to the next line of code, like so:
Enter a number:
4
Enter an operation:
Please, Enter a valid operation!
Could anyone, please point my mistake?
Note: I'm a newbie in Java and programming in general. so, please don't mind me if my code isn't the best, I'm still learning.
Hello you had a couple of problems. The nextLine() will return an empty line the first time since nextInt consumes the integer only after receiving a new line char but leaves the new line char in the buffer. Additionally the equals comparison needs to be using opr.equals("+") or a switch statement. And finally your Operation class constructor had the variable assignment backwards. I have also modified the Operation private member variables to follow standard naming conventions and scoping practices. You generally want to make member variables private since if not explicitly defined they will be protected. I would recommend using a good IDE since it can catch a lot of those errors for you, try using the free IntelliJ. Here is an updated version of your code.
public class Operation {
private final int num1;
private final int num2;
public Operation(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}
public void addition() {
System.out.println(num1 + num2);
}
public void subtraction() {
System.out.println(num1 - num2);
}
public void multiplication() {
System.out.println(num1 * num2);
}
public void division() {
System.out.println(num1 / num2);
}
}
And your main class
public class Calc {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a number: ");
int num1 = input.nextInt();
System.out.println("Enter an operation: ");
String opr = null;
while(opr == null || opr.length() == 0){
opr = input.nextLine();
}
switch (opr) {
case "+": {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.addition();
break;
}
case "-": {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.subtraction();
break;
}
case "*": {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.multiplication();
break;
}
case "/": {
System.out.println("Enter another Number: ");
int num2 = input.nextInt();
Operation op = new Operation(num1, num2);
op.division();
break;
}
default:
System.out.println("Please, Enter a valid operation!");
break;
}
}
}
Use
input.next
rather than
input.nextLine
as nextInt accepts a number and the new line character \n.
In the switch ts checking for the new line character.
Here's A Java Calculator Program I Just Made Recently, But It Doesn't Meet My Expectations! I Want It In A More Convenient Way Like It Has 6 Classes And Some Exclamation Marks, I Wanna Get A+ So Please Help Me!
1) Can I loop the codes so after displaying the answer, It runs the code again?
2) Can I somehow decrease the number of classes and the length of codes?
3) Can I clear screen in the console like in C++, So it should display a separate view for the Intro and the answer?
Here's The Code:
import java.util.Scanner;
public class javaCalc {
public static void welcome() {
System.out.println("Welcome to Calculator.java v0.1");
System.out.println("(Developed By RAZ0229)");
}
public static void main(String[] args) {
welcome();
System.out.flush();
System.out.println("\n1) Addition");
System.out.println("2) Substraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("\nChoose A Basic Operator:");
Scanner operandOne = new Scanner(System.in);
int inpOperation = operandOne.nextInt();
switch(inpOperation) {
case 1: additionMethod();
break;
case 2: substractionMethod();
break;
case 3: multiplicationMethod();
break;
case 4: divisionMethod();
break;
default: System.out.println("\n(Invalid Argument)");
return;
}
}
public static void additionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne + numTwo;
System.out.println(numOne + " + " + numTwo + " = " + answer);
}
public static void substractionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne - numTwo;
System.out.println(numOne + " - " + numTwo + " = " + answer);
}
public static void multiplicationMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne * numTwo;
System.out.println(numOne + " x " + numTwo + " = " + answer);
}
public static void divisionMethod() {
Scanner operandOne = new Scanner(System.in);
System.out.println("Enter The First Quantity:");
float numOne = operandOne.nextFloat();
System.out.println("Enter The Second Quantity:");
float numTwo = operandOne.nextFloat();
float answer = numOne / numTwo;
System.out.println(numOne + " / " + numTwo + " = " + answer);
}
}
You are asking for two floats in every method and using the same prints many times, so you can just create some method such as this and call it inside your operation method to stop repeating code (constantly repeated blocks of code is a strong indicator that the block can probably be abstracted into its own method):
public static float[] getValues(){
float[] values;
/*Implement your logic here asking user for floats, then put into above array
and do calculations in your methods using float array*/
return values;
}
You can also loop your main by wrapping it in a while loop and adding an extra case to your switch statement like so (if you would like to exit program, enter 5):
public static void main(String[] args) {
welcome();
while (true){
System.out.flush();
System.out.println("\n1) Addition");
System.out.println("2) Substraction");
System.out.println("3) Multiplication");
System.out.println("4) Division");
System.out.println("5) Quit");
System.out.println("\nChoose A Basic Operator:");
Scanner operandOne = new Scanner(System.in);
int inpOperation = operandOne.nextInt();
switch(inpOperation) {
case 1: additionMethod();
break;
case 2: substractionMethod();
break;
case 3: multiplicationMethod();
break;
case 4: divisionMethod();
break;
case 5: System.exit(0);
default: System.out.println("\n(Invalid Argument)");
return;
}
}
}
I'm writing Assignment on basic calculator and my code seems to be perfect but it has one bug.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Define new Scanner
String answer = "yes";
double finalValue;
while (answer.equalsIgnoreCase("Yes")) {
System.out.print("Enter First Number: ");
String firstNumber = scanner.next(); // Define first number, and ask user for input
while (!firstNumber.matches("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")) {
System.out.print("Please enter number only: ");
firstNumber = scanner.nextLine();
}
System.out.print("Enter an operator (+, -, / or *): ");
char operator = scanner.next().charAt(0);
System.out.print("Enter Second Number: ");
String secondNumber = scanner.next();
while (!secondNumber.matches("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")) {
System.out.print("Please enter number only: ");
secondNumber = scanner.nextLine();
}
switch(operator) {
case '+':
finalValue = Double.parseDouble(firstNumber) + Double.parseDouble(secondNumber);
System.out.println(finalValue);
break;
case '-':
finalValue = Double.parseDouble(firstNumber) - Double.parseDouble(secondNumber);
System.out.println(finalValue);
break;
case '*':
finalValue = Double.parseDouble(firstNumber) * Double.parseDouble(secondNumber);
System.out.println(finalValue);
break;
case '/':
if(secondNumber.equals("0")) {
System.out.println("a number can not be divided by 0.");
} else {
finalValue = Double.parseDouble(firstNumber) / Double.parseDouble(secondNumber);
System.out.println(finalValue);
}
break;
default:
System.out.println("Operator entered incorrectly.");
break;
}
System.out.print("Do you want another operation? (type yes to continue): ");
answer = scanner.next();
}
System.out.println("Bye.");
}
}
When I compile it and input some word instead of number in firstNumber or secondNumber, it outputs "Please enter number only: " twice for the first time and then once. I tried my best to solve it but I can't please help :(
I would suggest that you could solve this by using scanner.nextLine() every time, and do not use scanner.next(). I reworked your code and it's working now. May I inquire why you don't make use of scanner.nextDouble()? Anyway, see code below.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Define new Scanner
String answer = "yes";
double finalValue;
while (answer.equalsIgnoreCase("Yes")) {
System.out.print("Enter First Number: ");
Double firstNumber = null;
while (firstNumber == null) {
try {
firstNumber = Double.valueOf(scanner.nextLine());
} catch (NumberFormatException ex) {
System.out.println("Please enter number only.");
}
}
System.out.print("Enter an operator (+, -, / or *): ");
String line = null;
while (line == null || line.length() == 0) {
line = scanner.nextLine();
}
char operator = line.charAt(0);
System.out.print("Enter Second Number: ");
Double secondNumber = null;
while (secondNumber == null) {
try {
secondNumber = Double.valueOf(scanner.nextLine());
} catch (NumberFormatException ex) {
System.out.println("Please enter number only.");
secondNumber = null;
scanner.reset();
}
}
switch(operator) {
case '+':
finalValue = firstNumber + secondNumber;
System.out.println(finalValue);
break;
case '-':
finalValue = firstNumber - secondNumber;
System.out.println(finalValue);
break;
case '*':
finalValue = firstNumber * secondNumber;
System.out.println(finalValue);
break;
case '/':
if(secondNumber == 0d) {
System.out.println("a number can not be divided by 0.");
} else {
finalValue = firstNumber / secondNumber;
System.out.println(finalValue);
}
break;
default:
System.out.println("Operator entered incorrectly.");
break;
}
System.out.print("Do you want another operation? (type yes to continue): ");
answer = scanner.next();
}
System.out.println("Bye.");
}
}
This is happening because you have used scanner.nextLine(); after scanner.next();
Try putting a blank statement as below to avoid the issue:
scanner.nextLine();
OR
Replace scanner.nextLine() with scanner.next()
It is working after that. Please refer below:
class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Define new Scanner
String answer = "yes";
double finalValue;
while (answer.equalsIgnoreCase("Yes")) {
System.out.print("Enter First Number: ");
String firstNumber = scanner.next(); // Define first number, and ask user for input
while (!firstNumber.matches("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")) {
System.out.print("Please enter number only: ");
firstNumber = scanner.next();
}
System.out.print("Enter an operator (+, -, / or *): ");
char operator = scanner.next().charAt(0);
System.out.print("Enter Second Number: ");
String secondNumber = scanner.next();
while (!secondNumber.matches("[-+]?[0-9]*\\.?[0-9]+([eE][-+]?[0-9]+)?")) {
System.out.print("Please enter number only: ");
secondNumber = scanner.nextLine();
}
switch(operator) {
case '+':
finalValue = Double.parseDouble(firstNumber) + Double.parseDouble(secondNumber);
System.out.println(finalValue);
break;
case '-':
finalValue = Double.parseDouble(firstNumber) - Double.parseDouble(secondNumber);
System.out.println(finalValue);
break;
case '*':
finalValue = Double.parseDouble(firstNumber) * Double.parseDouble(secondNumber);
System.out.println(finalValue);
break;
case '/':
if(secondNumber.equals("0")) {
System.out.println("a number can not be divided by 0.");
} else {
finalValue = Double.parseDouble(firstNumber) / Double.parseDouble(secondNumber);
System.out.println(finalValue);
}
break;
default:
System.out.println("Operator entered incorrectly.");
break;
}
System.out.print("Do you want another operation? (type yes to continue): ");
answer = scanner.next();
}
System.out.println("Bye.");
}
}
But for future perspective, the best option is always to scan the expected data type like scanner.nextInt() or scanner.nextDouble()
You need to replace both firstNumber = scanner.nextLine(); with firstNumber = scanner.next(); and secondNumber = scanner.nextLine(); with secondNumber = scanner.next();.
I've made a basic calculator program and I'm getting this exception:
java.util.InputMismatchException
java.util.Scanner.next(Unknown Source)
The code runs just fine but when exception occurs it doesn't allows the user to input using Scanner. What am I doing wrong and how can I fix it?
package string;
import java.util.Scanner;
import java.lang.Exception;
public class Calculator {
double sum(double a,double b)
{
double c =a+b;
return c;
}
double subtract(double a,double b)
{
double c= a-b;
return c;
}
double multiply(double a,double b)
{
double c=a*b;
return c;
}
double divide(double a,double b)
{
double c=a/b;
return c;
}
public static void main(String[] args) {
Calculator f= new Calculator();
int choice;
int z;
Scanner s1 =new Scanner(System.in);
do{
try{
System.out.println("Welcome To Mini Calculator: Which Function Do You Want To Use");
System.out.println("1.Addition");
System.out.println("2.Subtraction");
System.out.println("3.Multiplication");
System.out.println("4.Division");
System.out.println();
System.out.print("Please Enter Your Choice Number: ");
choice = s1.nextInt();
System.out.println();
switch(choice){
case 1:
System.out.print("Please Enter The First Number: ");
double x= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double y= s1.nextDouble();
double u = f.sum(x,y);
System.out.println();
System.out.println("The Sum Of Two Numbers is: " + u);
break;
case 2:
System.out.print("Please Enter The First Number: ");
double q= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double w= s1.nextDouble();
double i= f.subtract(q,w);
System.out.println();
System.out.println("The Substraction Of Two Numbers is: "+i );
break;
case 3:
System.out.print("Please Enter The First Number: ");
double e= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double r= s1.nextDouble();
double o= f.multiply(e, r);
System.out.println();
System.out.println("The Multiplication Of Two Numbers " + o);
break;
case 4:
System.out.print("Please Enter The First Number: ");
double t= s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double k= s1.nextDouble();
double p= f.divide(t,k);
System.out.println();
System.out.println("The Divison of Two Numbers is: "+ p);
break;
default:System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}
}
catch(Exception e) {
System.out.println("Input error: You have entered wrong input");
System.out.println("Please restart the program");
}
System.out.println();
System.out.println("Do You Want To perform Another Functionality?");
System.out.println("Press 1 to Continue and Press 2 to Terminate The Program");
z= s1.nextInt(); // Issue comes here. It runs fine without exception. When exception occurs in above code ,it doesn't take input and shows another exception
}
while(z==1);
System.out.println();
System.out.println("Thank You For Using Calculator");
s1.close();
}
}
When you enter a wrong input, it goes in the catch but the input is still here, so z= s1.nextInt(); throws another exception which is not catched and it crashes
So you need to read the input in the catch, to clear the scanner :
} catch (Exception e) {
System.out.println("Input error: You have entered wrong input");
System.out.println("Please restart the program");
s1.nextLine();
}
Also, you have a lot of code duplicate, and variable names which means nothing, this is not very good compare to standards, I would suggestsomething like this to replace your whole switch{ ... }
System.out.println();
System.out.print("Please Enter The First Number: ");
double numb1 = s1.nextDouble();
System.out.println();
System.out.print("Please Enter The Second Number: ");
double numb2 = s1.nextDouble();
double res;
String operation = "";
switch (choice) {
case 1:
res = f.sum(numb1, numb2);
operation = "Sum";
break;
case 2:
res = f.subtract(numb1, numb2);
operation = "Substraction";
break;
case 3:
res = f.multiply(numb1, numb2);
operation = "Multiplication";
break;
case 4:
res = f.divide(numb1, numb2);
operation = "Divison";
break;
default:
res = 0;
System.out.println();
System.out.println("Please Enter a Valid Choice from 1 to 4");
}
System.out.println();
System.out.println("The " + operation + " Of Two Numbers is: " + res);
so I'm learning JAVA by myself and I have only the basic knowledge about programming languages in general. I wrote this simple calculator program to try and apply what I've been learning this far, but the problem is that it doesn't print the age, instead it prints a 0 and I don't know why:
this is the class
public class userinput {
private String name;
private int age;
public tuna (String name, int age){
name = "dina";
age = 3;
}
public void simpleMessage2(){
System.out.println("hello " + name + " ready to use our calculator?");
}
public void setName(String Uname){
name = Uname;
}
public void setAge(int uage){
uage = age;
}
public String getName(){
return name;
}
public int getAge(){
return age;
}
public void printname(){
System.out.printf("your name is %s", getName());
System.out.println();
}
public void printage(){
System.out.println("your age is");
System.out.println(getAge());
System.out.println();
}}
and this is the main class:
import java.util.Scanner;
class calc {
public static void main (String args[]) {
String name1;
int age1;
Scanner bucky = new Scanner(System.in);
int choice, num1, num2, sum;
System.out.println("Hey, enter your name");
name1 = bucky.nextLine();
System.out.println("Hey, enter your age");
age1 = bucky.nextInt();
tuna objc1 = new userinput(name1, age1);
objc1.setName(name1);
objc1.printname();
System.out.println();
objc1.setAge(age1);
objc1.printage();
System.out.println();
System.out.println("this is a basic calculator, select from the menu:");
System.out.println("Enter 1 for summation");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for module");
System.out.println("Enter 0 to exit");
choice = bucky.nextInt();
while (choice != 0) {
switch(choice){
case 1:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sum is equal to: ");
sum = num1 + num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 2:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sub is equal to: ");
sum = num1 - num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 3:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mul is equal to: ");
sum = num1 * num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 4:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the div is equal to: ");
sum = num1 / num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 5:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mod is equal to: ");
sum = num1 % num2;
System.out.print(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
default:
System.out.println("Invalid entry, please try again");
choice = bucky.nextInt();
break;
}
System.out.println("Bye!");
}
}
}
Where is the tuna class?
tuna objc1 = new userinput(name1, age1);
the userinput's Constructor:
public userinput(String n, int a){
name = n;
age = a;
}
I was just learning
the first error you don't have tuna class change the error to
tuna objc1 = new userinput(name1,age1);
to
userinput objc1 = new userinput();
then i change your code like that the method you use it in first class
and the second class to make operation
the first class
import java.util.Scanner;
public class userinput {
private String name;
private int age;
public void simpleMessage2(){
System.out.println("hello " + name + " ready to use our calculator?");
}
public void setName(String Uname){
name = Uname;
}
public void setAge(int uage){
age= uage; }
public void printname(){
System.out.printf("your name is %s", getName());
System.out.println();
}
public void printage(){
System.out.printf("your age is %s", getAge());
System.out.println();}
public int getAge(){
return age;
}
public String getName(){
return name;
}
}
the second class
class calc {
public static void main (String args[]) {
String name1;
int age1;
Scanner bucky = new Scanner(System.in);
int choice, num1, num2, sum;
System.out.println("Hey, enter your name");
name1 = bucky.nextLine();
System.out.println("Hey, enter your age");
age1 = bucky.nextInt();
userinput objc1 = new userinput();
objc1.setName(name1);
objc1.printname();
System.out.println();
objc1.setAge(age1);
objc1.printage();
System.out.println();
objc1.simpleMessage2();
System.out.println();
System.out.println("this is a basic calculator, select from the menu:");
System.out.println("Enter 1 for summation");
System.out.println("Enter 2 for subtraction");
System.out.println("Enter 3 for multiplication");
System.out.println("Enter 4 for division");
System.out.println("Enter 5 for module");
System.out.println("Enter 0 to exit");
choice = bucky.nextInt();
while (choice != 0) {
switch(choice){
case 1:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sum is equal to: ");
sum = num1 + num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 2:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the sub is equal to: ");
sum = num1 - num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 3:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mul is equal to: ");
sum = num1 * num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 4:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the div is equal to: ");
sum = num1 / num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
case 5:
System.out.println("enter the 1st num");
num1 = bucky.nextInt();
System.out.println("enter the 2nd num");
num2 = bucky.nextInt();
System.out.println("the mod is equal to: ");
sum = num1 % num2;
System.out.println(sum);
System.out.println("Select another operation from the menu or enter 0 to exit");
choice = bucky.nextInt();
break;
default:
System.out.println("Invalid entry, please try again");
choice = bucky.nextInt();
break;
}
}
System.out.println("Bye!");}
}
I hope i help you