Java计算器开关不工作

时间:2014-02-21 12:10:00

标签: java

我可以输入2个数字,但是当我为“wahl”(开关)输入一个整数时,结果是错误的。

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;

    }

}

也许某些方法泄漏?

我想用方法和return函数来练习一点java。

7 个答案:

答案 0 :(得分:6)

您的方法返回int,但您似乎没有使用结果,而是将其称为void

尝试在switch案例中使用以下内容进行测试:

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

它会将结果打印到sdtout

另请注意,根据Java和SO约定,代码都应该在English中(尽管在这种情况下非常清楚)。

答案 1 :(得分:3)

如果要查看结果,请使用main中新方法中的方法返回的值(例如result),或使用{{1}打印方法内的结果或类似的东西。例如:

System.out.println()

答案 2 :(得分:2)

你只需返回结果...... 你也必须打印结果

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)

答案 3 :(得分:2)

如果要返回结果,则应将其放在某个变量中,或者直接通过s.o.println显示 像...

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

答案 4 :(得分:0)

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)

答案 5 :(得分:0)

好的,这里有几个可能有用的指针:

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;
}
}

我想你将4个操作方法设为静态,因为你在main中调用它,编译器会抱怨静态引用?如果是这样,请阅读static的含义;并考虑通过以下方式创建计算器实例:

  1. 提供构造函数方法,
  2. 在您的main中创建一个实例:

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

    希望有所帮助

答案 6 :(得分:0)

您可以使用此示例

mysql_fetch_array