Frage

This is my second programming class so please bear with me. I have an assignment that requires me to create an RPN Calculator with functions. It's my first time learning about functions so I'm still confused.

My question is: The operator (+, -, *, /, etc.) when entered by the user should do what's asked. But I was thinking of using "If statement" for each operator. I tried "if (oper == '+') but it wouldn't work. Second time around, I tried as seen underneath. Fail! Illegal start of expression. Frustrating! Does anyone have any ideas of how I could start? And the worse part is I have no idea of how to put the operators in functions. Any help would be appreciated! Thank you.

    String oper;
    double x, y;
    double sum;

    System.out.print("X: ");
    x = keyboard.nextDouble();

    System.out.print("Y: ");
    y = keyboard.nextDouble();

    System.out.print("Op: ");
    oper = keyboard.nextLine();

    if (oper.equals(+))
        System.out.print("add");
        System.out.println();
War es hilfreich?

Lösung

oper = keyboard.nextLine(); is not right because .nextLine skip the next line of code so the thing you put oper would be empty. Try: oper = keyboard.next();

For the if statement try:

if(oper.charAt(0)=='+'){
  //the functions and whatever else you need
};
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top