I saw the solution. Did you use a function in this?

Functions are yet to be discussed in any video. Did the solution refer to a function?

https://ide.codingblocks.com/s/675717 For example, on an input of * 2 3 it will output 6. However, on an input of * 2 3 + 1 2 - 42; it will still output 6 (only the first function). Is this normal? Also, my code shows an error before running in eclipse IDE but still ran. Its not highlighted in the IDE. Could you please tell me what’s the error?

@sinuscoupon0s_4659768f9d9b587e SInce your code is giving only one output for the below input :

  • 2 3 + 1 2 - 42
    Correct o/p =
    6
    3
    2
    Your output = 6
    To resolve this you have to use a do while Loop and the loop will continue to integrate until you encounter an invalid operation or your encounter ‘x’ or ‘X’. Below is my code for reference.

import java.util.Scanner;
public class Main{
   static Scanner scn=new Scanner(System.in);
    public static void main(String[] args) {
        char ch;
        do {
            ch = scn.next().charAt(0);  // Use cin in case of c++
            if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '%') {
                int a = scn.nextInt(); // Use cin in case of c++
        int b = scn.nextInt(); // Use cin in case of c++
        int res = 0;
        switch (ch) {
        case '+': {
            res = a + b;
            break;
        }
        case '-': {
            res = a - b;
            break;
        }
        case '*': {
            res = a * b;
            break;
        }
        case '/': {
            res = a / b;
            break;
        }
        case '%': {
            res = a % b;
            break;
        }
        }
        System.out.println(res);
				
            } else {
                if (ch != 'x' && ch != 'X')
                    System.out.println("Invalid operation. Try again.");
            }
        } while (ch != 'x' && ch != 'X');
    }
    public static void operation(char ch) {
        
    }
}

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.