user3333587
user3333587

Reputation: 65

Java calculator switch dont work

I can enter 2 numbers but when I enter an integer for "wahl" (the switch) the result is wrong.

import java.util.Scanner;


public class taschenrechner {

    public static void main(String[] args) {

        Scanner s = new Scanner(System.in);

        System.out.println("Bitte erste Zahl eingeben:");
        int a = s.nextInt();
        System.out.println("Bitte zweite Zahl eingeben:");
        int b = s.nextInt();

        System.out.println("1.+ \n 2.- \n 3.* \n 4. /");
        int wahl = s.nextInt();

        switch(wahl){
            case 1:
                addieren(a,b);
                break;
            case 2:
                subtrahieren(a,b);
                break;
            case 3:
                multiplizieren(a,b);
                break;
            case 4:
                dividieren(a,b);
                break;
            }
        System.out.println("Bye Bye World");
    }

    private static int addieren(int a, int b){
        int c = a + b;
        return c;
    }

    private static int subtrahieren(int a, int b){
        int c = a - b;
        return c;
    }

    private static int multiplizieren(int a, int b){
        int c = a * b;
        return c;
    }

    private static int dividieren(int a , int b){
        int c = a / b;
        return c;

    }

}

Maybe some method leaks?

I wanted to do this with methods and the return function to practice a bit java.

Upvotes: 0

Views: 179

Answers (7)

U.vishnu
U.vishnu

Reputation: 1

You can use this example

 package com.alindal.calc;
import java.util.Scanner;

public class Calc {

    public static void main(String[] args) {
        System.out.println("Select an option : \n 1:Addition 2:Subtraction 3:Multiplication 4: Division");
        // TODO Auto-generated method stub
Scanner read=new Scanner(System.in);
int x=read.nextInt();
    switch(x)
    {
    case 1:
        add();
        break;
    case 2:
        sub();
        break;
    case 3:
        multi();
        break;
    case 4:
        div();
        break;
        default:
            System.out.println("Invalid choice");
    }

    }
    public static void add()
    {
        Scanner read=new Scanner(System.in);
        System.out.println("Enter the values a and b");
        int a=read.nextInt();
        int b=read.nextInt();
    int c=a+b;
    System.out.println("The sum is "+c);
    }
    public static void sub()
    {
        System.out.println("Enter the values a and b");
        Scanner read=new Scanner(System.in);
        int a=read.nextInt();
        int b=read.nextInt();
    int c=a-b;
    System.out.println("The difference is "+c);
    }
    public static void multi()
    {
        System.out.println("Enter the values a and b");
        Scanner read=new Scanner(System.in);
        int a=read.nextInt();
        int b=read.nextInt();
    int c=a*b;
    System.out.println("The product is "+c);
    }
    public static void div()
    {
        System.out.println("Enter the values a and b");
        Scanner read=new Scanner(System.in);
        int a=read.nextInt();
        int b=read.nextInt();
    int c=a/b;
    System.out.println("The division is "+c);
    }

}

Upvotes: 0

posdef
posdef

Reputation: 6532

OK here are a couple of pointers that might be useful:

import java.util.Scanner;

// Class names typically start with a capital letter, good practice to get accustomed to
public class Taschenrechner { 

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.println("Bitte erste Zahl eingeben:");
int a = s.nextInt();
System.out.println("Bitte zweite Zahl eingeben:");
int b = s.nextInt();

System.out.println("1.+ \n 2.- \n 3.* \n 4. /");
int wahl = s.nextInt();

switch(wahl){
case 1:
    addieren(a,b); // <- nothing happens to the result!! 
    break;
case 2:
    subtrahieren(a,b); // <- nothing happens to the result!! 
    break;
case 3:
    multiplizieren(a,b); // <- nothing happens to the result!! 
    break;
case 4:
    dividieren(a,b); // <- nothing happens to the result!! 
    break;
default: // <- always good to have a default in a switch
    // warn user that invalid option is entered
}

System.out.println("Bye Bye World");
}

// dont need the integer "c" as you never use it locally. 
private static int addieren(int a, int b){
    retrun a + b;
}

private static int subtrahieren(int a, int b){
    return a - b;
}

private static int multiplizieren(int a, int b){
    return a * b;
}

private static int dividieren(int a , int b){
    return a / b;
}
}

I suppose you made the 4 operation methods static, since you call it in main, and the compiler complains about static reference? If so, read a bit about what static means; and consider creating an instance of your calculator by:

  1. supplying a constructor method, and
  2. creating an instance of it in your main something like:

Taschenrechner t = new Taschenrechner(); 
t.addieren(a,b); //the methods don't need to be static anymore :) 

Hope it helps

Upvotes: 0

slaveCoder
slaveCoder

Reputation: 537

int result = 0
switch(wahl){
        case 1:
            result = addieren(a,b);
            break;
        case 2:
            result = subtrahieren(a,b);
            break;
        case 3:
            result = multiplizieren(a,b);
            break;
        case 4:
            result = dividieren(a,b);
            break;
        }
//Print the result using a syso
System.out.println(result)

Upvotes: 0

Skabdus
Skabdus

Reputation: 230

If you are returning the result then you should put it in some valriable or can directly display by s.o.println like...

    switch(wahl){
case 1:
   system.out.println(addieren(a,b));
or   
int result = addieren(a,b)
system.out.println(result);

Upvotes: 2

Matthias Burger
Matthias Burger

Reputation: 5946

you just return the result... you have to print the result, too

int result = 0
    switch(wahl){
            case 1:
                result = addieren(a,b);
                break;
            case 2:
                result = subtrahieren(a,b);
                break;
            case 3:
                result = multiplizieren(a,b);
                break;
            case 4:
                result = dividieren(a,b);
                break;
            }

System.out.println(result)

Upvotes: 2

Martin Dinov
Martin Dinov

Reputation: 8825

If you want to see the result, use the returned value from the methods in a new variable in main (say, result) or print out the result inside the methods using System.out.println() or something of the sort. For example like this:

    int result = 0;
    case 1:
        result = addieren(a,b);
        break;
    case 2:
        result = subtrahieren(a,b);
        break;
    case 3:
        result = multiplizieren(a,b);
        break;
    case 4:
        result = dividieren(a,b);
        break;
    }

    System.out.println("Result = " + result);

Upvotes: 3

Mena
Mena

Reputation: 48404

Your methods return int, but you don't seem to use the result and call them as void instead.

Try testing in your switch cases with something like:

System.out.println(multiplizieren(a,b));

It will print the result to sdtout.

Also note that as per both Java and SO convention, code should all be in English (although it's quite clear in this case).

Upvotes: 6

Related Questions