0

I just started with java and its doing realy good till now. But now i startet coding a little bit on my own. dont get the Problem here:

import java.util.Scanner;


public class Taschenrechner {

    public static void main(String[] args) {

        System.out.println ("Mein erster Java Taschenrechner");
        Scanner scan = new Scanner (System.in);
        System.out.println ("Welche Operation ? Möglich sind + - x /");
        String operation = scan.nextLine ();
        System.out.println ("Erste Zahl ?");
        int input1 = scan.nextInt ();
        System.out.println ("Zweite Zahl ?");
        int input2 = scan.nextInt ();
        int output = 0;

        if (operation == "+") {
            output = input1 + input2;
        }

        else if (operation == "-") {
            output = input1 - input2;
        }

        else if (operation == "x") {
            output = input1 * input2;
        }

        else if (operation == "/") {
            output = input1 / input2;
        }

        System.out.println ("Das Ergebnis ist: " + output);

    }

}

The variable output stays 0 and dont change

By the way...have i postet the code sampe the right way ?

brandizzi
  • 26,083
  • 8
  • 103
  • 158
  • In addition to reading the answers on http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java, consider input validation for your program, like ending your else-if chain with `else { System.err.println("Invalid operator"); return -1; }` – jdphenix Aug 20 '14 at 18:05

1 Answers1

3

use "+".equals(operation) instead of operation == "+" and for other conditions

Amit Sharma
  • 1,202
  • 11
  • 26