Sir, please help me out. i gave so much time to this problem approx 5 hours . still i could not get the correct

sir, i have no idea about the code of the problem . sir please suggest me the correct answer.
Here is my two approach to go towards the answer. so please correct me out. and tell me what i have to do.
1.
import java.util.Scanner;

public class Main{

public static void main(String[] args) {
	Scanner src = new Scanner(System.in);
	char ch = src.next().charAt(0);
	if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%' ) {
		int N1 = src.nextInt();
		int N2 = src.nextInt();
		if(ch == '+') {
			System.out.println(N1 + N2);
		}
		else if(ch == '-') {
			System.out.println(N1 - N2);
		}
		else if(ch == '*') {
			System.out.println(N1 * N2);
		}
		else if(ch == '/') {
			System.out.println(N1 / N2);
		}
		else if(ch == '%') {
			System.out.println(N1 % N2);
		}
	}
	while(ch == 'X' || ch == 'x' ) {
		if(ch =='X') {
			System.out.println();
			break;
		}
		else if(ch =='x') {
			break;
		}
		
	
	}	    while (ch != '+' || ch != '-' || ch != '*' || ch != '/' || ch != '%' || ch!='x' || ch!='X'){
		System.out.println("Invalid operation. Try again");	
		break;
	}
   
}

}

import java.util.Scanner;

public class Main {
public static Scanner src = new Scanner(System.in);
static char ch = src.next().charAt(0);
public static void main(String[] args){
if(ch==’+’||ch==’-’||ch==’’||ch==’/’||ch==’%’||ch==β€˜X’||ch==β€˜x’) {
cal();
}
else {
System.out.println(β€œInvalid operation.Try again”);
cal();
}
}
public static void cal() {
int N1 = src.nextInt();
int N2 = src.nextInt();
while(ch==’+’||ch==’-’||ch==’
’||ch==’/’||ch==’%’||ch==β€˜X’||ch==β€˜x’) {
switch(ch){
case’+’:{
System.out.println(N1+N2);
break;
}
case’-’:{
System.out.println(N1-N2);
break;
}
case’’:{
System.out.println(N1
N2);
break;
}
case’/’:{
System.out.println(N1/N2);
break;
}
case’%’:{
System.out.println(N1%N2);
break;
}
case’X’:{
break;
}
case’x’:{
break;
}

	  }
	 
  }

}
}

Hey @amartyasinghkushwaha,

We have a program which is like a simple calculator, ie. it performs the all the tasks of a calculator.

It takes an input and we have 3 possible cases-

  1. If input character is any operator ie β€˜+’, β€˜-’, β€˜*’, β€˜/’ or β€˜%’ , then we need to take 2 numbers inputs and perform the operation on them and display the result.
  2. If input character is β€˜x’ or β€˜X’ we need to terminate the program. (We need to keep taking inputs until this case is encountered).
  3. If input character is any other character then we need to display the message. 'Invalid operation. Try again.'

We need to do these tasks infinite times(until we obtain the 2nd condition). To do this, you need to place all your code inside a do-while loop and we need to stop once we get ch==β€˜x’ || ch=='X.

Do it like this:

char ch;
do{

Step 1 - input ch

Step 2 - check for above conditions

Step 3 - Do the needful printing.

}while(ch!='x' && ch!='X)

Try writing the code for one last time in the above mentioned format, If you are still unable to get it correctly, I’ll share the code with you.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.