How to exit return loop in java? - java

I'm not new to programming per se, I have studied C# for quite a while now, but I haven't really done a lot of exercises by myself, I'm just now starting with java because I want to do Android apps and to test my newly acquired knowledge I wanted to do a basic console calculator, here's what I got so far:
package calculatorSource;
import java.util.*;
public class calculatorJava {
private static Scanner input;
public static void main(String[] args) {
System.out.println("Select an operation");
String choice = null;
input = new Scanner(System.in);
while(choice == null) {
System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module.");
choice = input.next();
}
while(choice != null) {
if(choice.equals("a")) {
System.out.println(adition(0, 0));
choice = null;
} else if(choice.equals("s")) {
System.out.println(subtraction(0, 0));
choice = null;
} else if(choice.equals("m")) {
System.out.println(multiplication(0, 0));
choice = null;
} else if(choice.equals("d")) {
System.out.println(division(0, 0));
choice = null;
} else if(choice.equals("mo")) {
System.out.println(module(0, 0));
choice = null;
} else {
System.out.println("Option not available, please try again.");
choice = null;
}
}
}
public static float adition(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 + n2;
System.out.println("Result is: " + result);
return adition(result, result);
}
public static float subtraction(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 - n2;
System.out.println("Result is: " + result);
return subtraction(result, result);
}
public static float multiplication(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 * n2;
System.out.println("Result is: " + result);
return multiplication(result, result);
}
public static float division(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 / n2;
System.out.println("Result is: " + result);
return division(result, result);
}
public static float module(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 % n2;
System.out.println("Result is: " + result);
return module(result, result);
}
}
I know it probably is not the best or more efficient calculator out there, but as I said, I just started with java and this is pretty much all I know so far, the program works, as I can make an addition, a division or whatever else I choose, but right after that I want it to give me the option of selecting a different operation, I put the "choice = null" right after the return but it doesn't not seems to be working, I've tried several stuff to this point, but I'm starting to think I may have misunderstood what return actually does, so I thought it would be better to turn to you guys for help.
Any advice is appreciated, thanks!

Instead of
return adition(result, result);
in your adition method, return result.
Otherwise you're just going to repeat the same method until the stack overflows. (Same for other methods also).
Your while (choice != null) { loop isn't necessary, as you always set choice = null;. Since you also don't otherwise change the value of choice in that loop, you may as well just remove the loop.
There is no point in passing parameters into the adition (etc) methods, since you always overwrite them. Just declare them as local variables in those methods:
public static float adition() { // No parameters
// ...
float n1 = input.nextFloat();
// ...
float n2 = input.nextFloat();

You are recursively calling all methods infinitely. choice = null is ok. it will terminate if you will correct your code :
Instead of
MethodaName(result, result);
just write
return result;
Also you need not pass any parameters if taking input from user in method itself.

It seems you have misunderstood the return statement inside the function.
Whenever we call a function/method in java it should have a return type and the returned value should be what the function has to actually return after processing the arguments.
Hence in your case the addition function should be like this
public static float adition(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 + n2;
System.out.println("Result is: " + result);
return result;
}
Here the result variable is the processed value of your function for which it was created.
Where as in your case you were calling the same function in return statement and it was becoming a recursive function and in recursive function we always need a break statement to come out of it.

There are 2 things you could do.
If you want to run the operation just once then instead of
return addition(result,result)
use
return result.
2 . Suppose you want to run the loop of addition until user says , he want to do some other operation, then use a global variable and suppose you add the condition when user enters -999 then you return.
Hence , in second case the loop of adition runs till user enters -999.
import java.util.*;
public class calculatorJava {
private static Scanner input;
static int flag = 1;
public static void main(String[] args) {
System.out.println("Select an operation");
String choice = null;
input = new Scanner(System.in);
while (choice == null) {
System.out.println(
"Type 'a' for adition, 's' for subtraction, 'm' for multiplication,"
+ " 'd' for division, or 'mo' for module.");
choice = input.next();
}
while (choice != null) {
if (choice.equals("a")) {
flag = 1;
System.out.println(adition(0, 0));
//choice = null;
} else if (choice.equals("s")) {
flag = 1;
System.out.println(subtraction(0, 0));
//choice = null;
} else if (choice.equals("m")) {
flag = 1;
System.out.println(multiplication(0, 0));
//choice = null;
} else if (choice.equals("d")) {
flag = 1;
System.out.println(division(0, 0));
//choice = null;
} else if (choice.equals("mo")) {
flag = 1;
System.out.println(module(0, 0));
//choice = null;
} else {
System.out.println("Option not available, please try again.");
choice = null;
}
}
}
public static float adition(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 + n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return adition(result, result);
}
public static float subtraction(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 - n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return subtraction(result, result);
}
public static float multiplication(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 * n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return multiplication(result, result);
}
public static float division(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 / n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return division(result, result);
}
public static float module(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
if(n1 == -999){
flag = 0;
}
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 % n2;
System.out.println("Result is: " + result);
if(flag==0)
return result;
return module(result, result);
}
}

To ask again
First, your while loop for the addition is not usefull, an IF could be enough, but useless to since you check the value before (in the first loop)
This is one possibility to loop on you code.
boolean letDoMath = true;
while(letDoMath ) { // aka while(letDoMath == true){
while(choice == null) {
System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module.");
choice = input.next();
}
//HERE YOU KNOW CHOISE ISN'T NULL BECAUSE OF THE WHILE LOOP, NO NEED TO CHECK AGAIN.
if(choice.equals("a")) {
System.out.println(adition(0, 0));
choice = null;
} else if(choice.equals("s")) {
System.out.println(subtraction(0, 0));
choice = null;
} else if(choice.equals("m")) {
System.out.println(multiplication(0, 0));
choice = null;
} else if(choice.equals("d")) {
System.out.println(division(0, 0));
choice = null;
} else if(choice.equals("mo")) {
System.out.println(module(0, 0));
choice = null;
} else if(choice.equlas("end"){
letDoMath = false;
}
}
The boolean while give you the possibility to loop again an again until you input end. This in not perfect of course but this is a beginning. (And I didn't try or even write it in IDE so might contains errors.
Operation methods error
As precise everywhere, your operation methods are recursive, addition call itself at the end. Remove the return addition(x,y) and just return the result of the addition you do.
Some improvment idea :
Create a method to get a number an return it (usefull in every method).
The best would be to ask number outside the operation to pass the value to the methods, not reading the scanner in the methods. (of course, you need to be sure of the choice value (actualy, you could ask for the number before the choice). And ask again ONLY if a operation has been done.

Related

Not getting correct result while giving input as runtime input

I am new to java. i have tried input as arguments at that time there is execution is perfect. When i am try to give input as runtime input it gives only "else" result. what is the mistake i have done? and How to solve this?
public void h(){
Scanner s = new Scanner(System.in);
System.out.println("What you Want to do = ");
String k = s.nextLine();
System.out.println("Enter the value of a = ");
int a = s.nextInt();
System.out.println("Enter the value of b = ");
int b = s.nextInt();
if(("p").equals(k) || (("+").equals(k))){
sum = a+b;
System.out.println("Sum = "+sum);
}
else if(("s").equals(k) || (("-").equals(k))){
sum = a-b;
System.out.println("Sum = "+sum);
}
else if(("m").equals(k) || (("*").equals(k))){
sum = a*b;
System.out.println("Sum = "+sum);
}
else if(("d").equals(k) || (("/").equals(k))){
sum = a/b;
System.out.println("Sum = "+sum);
}else{
System.out.println("Enter the correct work to do");
}

How to get a looping program in Java

I am new to Java programming and I wanted to try to make a calculator. I made the code, but the only problem is that it will only run once. I was wondering if there was any way to get it to "loop". By that I mean, add a question to go to the beginning of the main method that way I can run the calculator again. Here is the code I have...
import java.util.Scanner;
public class calculatorFull {
public static void main(String[] args){
int op;
double num1, num2;
Scanner operation = new Scanner(System.in);
System.out.println("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide");
System.out.print("Which operation would you like to perform? ");
op = operation.nextInt();
if((op != 1) && (op != 2) && (op != 3) && (op != 4)){
System.out.println("That wasn't an option...");
}else{
System.out.print("First number: ");
num1 = operation.nextDouble();
System.out.print("Second number: ");
num2 = operation.nextDouble();
if(op==1){
Add(num1, num2);
}else if(op==2){
Sub(num1, num2);
}else if(op==3){
Mult(num1, num2);
}else if(op==4){
Div(num1, num2);
}
}
}
public static void Add(double x, double y){
double numsum;
numsum = x + y;
System.out.printf("%s + %s = %s", x, y, numsum);
}
public static void Sub(double x, double y){
double numsum;
numsum = x - y;
System.out.printf("%s - %s = %s", x, y, numsum);
}
public static void Mult(double x, double y){
double numsum;
numsum = x * y;
System.out.printf("%s * %s = %s", x, y, numsum);
}
public static void Div(double x, double y){
double numsum;
numsum = x / y;
System.out.printf("%s / %s = %s", x, y, numsum);
}
}
Thanks in advance for the help!
Btw, I think this might be different from this question (User input to repeat program in Java). I have different methods that I call from and that was changed to call from the main to just 1 method. Don't know if that changes any of the solutions or if it is the same problem but thank you for pointing out that it might be.
You can set a sentinel value to exit the program (i.e., -1 in your option) and keep track of a variable that we will constantly check in a while loop. If op ever becomes -1, it exits. Otherwise, it will restart the logic inside the while loop.
int op = 0;
while(op != -1) {
double num1, num2;
Scanner operation = new Scanner(System.in);
System.out.println("1 - Add\n2 - Subtract\n3 - Multiply\n4 - Divide");
System.out.print("Which operation would you like to perform? ");
op = operation.nextInt();
if((op != 1) && (op != 2) && (op != 3) && (op != 4)){
System.out.println("That wasn't an option...");
}else{
System.out.print("First number: ");
num1 = operation.nextDouble();
System.out.print("Second number: ");
num2 = operation.nextDouble();
if(op==1){
Add(num1, num2);
}else if(op==2){
Sub(num1, num2);
}else if(op==3){
Mult(num1, num2);
}else if(op==4){
Div(num1, num2);
}
}
int continue = 0;
while(continue == 0){
// Put your calculator code here
System.out.println("Enter 1 to exit or 0 to continue");
choice = Integer.parseInt(br.readLine());
}
Try this.
UPDATE:
Use this:
import java.io.*;
public class StackOverFlow
{
// arguments are passed using the text field below this editor
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int cont = 0;
while(cont == 0){
System.out.println("Inside");
// Put your calculator code here
System.out.println("Enter 1 to exit or 0 to continue");
cont = Integer.parseInt(br.readLine());
}
System.out.println("Outside");
}
}
The simplest way to do this, is to just call the main method when the calculation is complete and printed. You can achieve this by using main(null). Just add it right after your if-else block. Code below:
if(op==1){
Add(num1, num2);
}else if(op==2){
Sub(num1, num2);
}else if(op==3){
Mult(num1, num2);
}else if(op==4){
Div(num1, num2);
}
System.out.println(); // prints blank line
main(null);// calls main method (re-runs program)
This will complete the operation, then repeat the process.

A simple console calculator

I have designed a simple console calculator as follows but i need to create a loop where I can re run the program:
So that I won't need to run the program again and again and it can take the input from the user and run by itself.
private static Scanner op;
private static Scanner input;
private static Scanner cont;
public static void main(String args[]) {
double num1;
double num2;
double ans = 0;
double l1;
double l2;
String operation;
input = new Scanner(System.in);
System.out.println("Please input your first number:");
num1= input.nextDouble();
System.out.println("Please input the second numer:");
num2= input.nextDouble();
op = new Scanner(System.in);
System.out.println("Select your operation:");
operation = op.next();
if (operation.equals("+")){
System.out.println("Your Result is :" + (num1+num2));
}
if (operation.equals("-")){
System.out.println("Your Result is:" + (num1-num2));
}
if (operation.equals("/")){
if (num2 == 0){
System.out.println("Your Input is Invalid");}
else {
System.out.println("Your result is :" + (num1/num2));
}
}
if (operation.equals("*")){
System.out.println("Your result is:" + (num1*num2));
}
if (operation.equals("%")){
System.out.println("Your result is:" + (num1*100/num2));
}
if (operation.equals("^")){
ans= Math.pow(num1, num2);
System.out.println("Your result is:"+ans);
}
if (operation.equals("log")){
l1=Math.log(num1);
l2=Math.log(num2);
System.out.println("Your result is:"+l1/l2);
}
operation = cont.next();
System.out.println("Do you wish to perform any other operation?");
if (operation.equals("Yes")){
else{
System.out.println("Thank You");
}
}
}
you can create a function and call it with a loop like this:
private static void myfunction(){
//put your code here
}
public static void main(String[] args) {
//call your function until your condition is false
while(myconditionistrue){
myfunction();
}
}
if you want to ask your user if continue or not use this way:
private static void myfunction() {
//put your code here
}
public static void main(String[] args) {
String exit;
Scanner scan = new Scanner(System.in);
do {
//call your function or you can put all your code here
myfunction();
System.out.println("You want to continue? y : n :");
exit = scan.next();
} while (exit.equals("y"));
}
You can use a while(true) and then just exit at command:
import java.util.Scanner;
class Main {
private static Scanner op;
private static Scanner input;
private static Scanner cont;
public static void main(String args[]) {
double num1;
double num2;
double ans = 0;
double l1;
double l2;
String operation;
while (true) {
input = new Scanner(System.in);
System.out.println("Please input your first number:");
num1 = input.nextDouble();
System.out.println("Please input the second numer:");
num2 = input.nextDouble();
op = new Scanner(System.in);
System.out.println("Select your operation:");
operation = op.next();
if (operation.equals("+")) {
System.out.println("Your Result is :" + (num1 + num2));
}
if (operation.equals("-")) {
System.out.println("Your Result is:" + (num1 - num2));
}
if (operation.equals("/")) {
if (num2 == 0) {
System.out.println("Your Input is Invalid");
} else {
System.out.println("Your result is :" + (num1 / num2));
}
;
}
if (operation.equals("*")) {
System.out.println("Your result is:" + (num1 * num2));
}
if (operation.equals("%")) {
System.out.println("Your result is:" + (num1 * 100 / num2));
}
if (operation.equals("^")) {
ans = Math.pow(num1, num2);
System.out.println("Your result is:" + ans);
}
if (operation.equals("log")) {
l1 = Math.log(num1);
l2 = Math.log(num2);
System.out.println("Your result is:" + l1 / l2);
}
//cont = new Scanner(System.in);
//operation = cont.next();
System.out.println("Do you wish to perform any other operation?");
cont = new Scanner(System.in);
operation = cont.next();
if (operation.equals("Yes")) {
} else {
System.out.println("Thank You");
System.exit(0);
}
}
}
}
Test
Please input your first number:
5
Please input the second numer:
5
Select your operation:
*
Your result is:25.0
Do you wish to perform any other operation?
Yes
Please input your first number:
20
Please input the second numer:
10
Select your operation:
+
Your Result is :30.0
Do you wish to perform any other operation?
No
Thank You
As RealSkeptic stated in his comment, what you need is a loop. You can learn about loops, for example, here. The best choice for this situation is do-while loop.
I have a few more advices to your code:
You don't need to create many scanners to get more inputs than just one. One instance suffices.
When you use many if statements whose conditions are disjoint (by that I mean that if a single condition is true then the rest of the conditions is false), use else-if statements. It is more efficient. See this for more information.
Don't use empty branches in if statement. In this particular example, you can use if (!operation.equals("Yes")). The exclamation mark means negation. So if operation equals "Yes", the whole condition is evaluated as false.
Learn how to divide a complex code into functions (or methods). Generally, one function should provide one simple functionality.

Why does my calculator automatically answer "0" before i can put my sign in?

Somewhere around line 15 it gives me issues.
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in); //scanner object created
System.out.println("Enter your first number");
int nr1 = sc.nextInt();
System.out.println("Enter your second number");
int nr2 = sc.nextInt();
System.out.println("Enter your sign (+ , - , /, *)");
String anvin = sc.nextLine();
int ans = 0;
//somewhere around this line is where it is having the problems. it gives me the answer before i can put in my sign;
if(anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
}
else if(anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
}
else if(anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
}
else if(anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
}
System.out.println(ans);
System.out.println("To continue type yes");
String yes= sc.nextLine();
if(yes.equalsIgnoreCase("yes")) {
return;
}
}
}
it answers "0" whatever I enter before I can put in my sign
Enter your first number
9
Enter your second number
9
Enter your sign (+ , - , /, *)
0
To continue type yes
please tell me what I did wrong and possibly correct it so I can understand further
Try changing your sc.nextInt() lines to Integer.parseInt(sc.nextLine()). This should make your code work correctly.
EDIT: updated the code to include a while loop to make it so you can do multiple runs per your comment. This would also require you changing your last if statement to break; instead of return;
Scanner sc = new Scanner(System. in ); //scanner object created
while (true) {
System.out.println("Enter your first number");
int nr1 = Integer.parseInt(sc.nextLine());
System.out.println("Enter your second number");
int nr2 = Integer.parseInt(sc.nextLine());
System.out.println("Enter your sign (+ , - , /, *)");
String anvin = sc.nextLine();
int ans = 0;
//somewhere around this line is where it is having the problems. it gives me the answer before i can put in my sign;
if (anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
} else if (anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
} else if (anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
} else if (anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
}
System.out.println(ans);
System.out.println("To continue type yes");
String yes = sc.nextLine();
if (!yes.equalsIgnoreCase("yes")) {
break;
}
}
Change
String anvin = sc.nextLine();
to
String anvin = sc.next();
Also keep in mind that you might divide through zero ;-)
Edit:
also change
String yes= sc.nextLine();
to
String yes= sc.next();
Instead of sc.nextLine(); use sc.next();
I would suggest you this, you can not only learn using objects but learn a better way of writing managed codes too,
Calculator.java -> a class
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
//Instantiate
Scanner input = new Scanner(System.in);
Calculations calc = new Calculations();
// Variable declarations
double answer = 0, entry1 , entry2 ;
char operator;
// Start
System.out.println("***** Welcome to the Command line calculator program *****");
System.out.print("Please enter the first number :");
entry1 = input.nextDouble();
System.out.print("Please enter the second number:");
entry2 = input.nextDouble();
System.out.println("Please enter the operation : ");
System.out.println("***** Operations :- + -> Add ; - ->Substract ; / -> Divide ; * -> Multiply ; ^ : Power *****");
operator = input.next().charAt(0);
// Switch case
switch (operator){
case '+' : answer = calc.add(entry1, entry2);
break;
case '-' : answer = calc.substract(entry1, entry2);
break;
case '/' : answer = calc.divide(entry1, entry2);
break;
case '*' : answer = calc.multiply(entry1, entry2);
break;
case '^' : answer = calc.power(entry1, entry2);
break;
}
System.out.println(entry1 + " " + operator + " " + entry2 + " = " + answer);
}
}`
Calculations.java -->another class holding calculations
import java.math.*;
public class Calculations {
// Addition Method
double add (double first, double second){
double answer = first + second;
return answer;
}
// Substraction Method
double substract (double first, double second){
double answer = first - second;
return answer;
}
// Multiplication Method
double multiply (double first, double second){
double answer = first * second;
return answer;
}
// Division Method
double divide (double first, double second){
double answer = first / second;
return answer;
}
// Power Method
double power(double a, double b){
double answer =Math.pow(a, b);
return answer;
}
}

Java: Ask for continue

I'm trying to make a calculator. These operators: x, +, -, / work fine.
But I want the user to be able to do 2 things after he gets the answer on his math problem.
Ask user if he wants to continue.
If user types in yes he gets to put in 2 numbers that it counts again.
If the user types no just shut down.
Here's my code:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner Minscanner = new Scanner(System.in);
int nr1 = Integer.parseInt(Minscanner.nextLine());
int nr2 = Integer.parseInt(Minscanner.nextLine());
int yes = Integer.parseInt(Minscanner.nextLine());//trying to fix reset
int ans =0;
int reset = J;/trying to make it reset if user types in yes
String anvin = Minscanner.nextLine();
if(anvin.equalsIgnoreCase("+")) {
ans = nr1 + nr2;
}
else if(anvin.equalsIgnoreCase("-")) {
ans = nr1 - nr2;
}
else if(anvin.equalsIgnoreCase("*")) {
ans = nr1 * nr2;
}
else if(anvin.equalsIgnoreCase("/")) {
ans = nr1 / nr2;
System.out.println(ans);
}
if(anvin.equalsIgnoreCase("yes")) {
return;
}
}
}
Put your code in a
do {
...
} while (condition);
loop, and in your case the condition would be something like wantToContinue if user say "yes".
Then the program will not end unless user no longer wants to calculate.
You can refactor your code as bellow. This may help you
boolean status=true;
while (status){
Scanner scanner = new Scanner(System.in);
Scanner scanner1 = new Scanner(System.in);
System.out.println("Enter your two numbers one by one :\n");
int num1 = scanner.nextInt();
int num2 = scanner.nextInt();
System.out.println("Enter your operation you want to perform ? ");
int ans =0;
String option = scanner1.nextLine();
if(option.equalsIgnoreCase("+")) {
ans = num1 + num2;
}
else if(option.equalsIgnoreCase("-")) {
ans = num1 - num2;
}
else if(option.equalsIgnoreCase("*")) {
ans = num1 * num2;
}
else if(option.equalsIgnoreCase("/")) {
ans = num1 / num2;
}
System.out.println(ans);
System.out.println("you want to try again press y press j for shutdown\n");
Scanner sc = new Scanner(System.in);
String input=sc.nextLine();
if (input.equalsIgnoreCase("J")) {
System.exit(0);
} else if (input.equalsIgnoreCase("Y")) {
status = true;
}
}

Categories