2014-02-21 61 views
0

我可以輸入2個數字,但是當我輸入「wahl」(開關)的整數時,結果是錯誤的。Java計算器開關不工作

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; 

    } 

} 

也許某種方法泄漏?

我想用方法和返回函數來練習一下java。

+2

您究竟在做什麼?你根本沒有使用返回值'c',那麼輸出怎麼會是錯的呢? – skiwi

+4

如果你不打印任何結果,你怎麼知道結果是錯誤的? –

+1

它顯示了什麼結果?它每次都會顯示Bye Bye World。 – Adarsh

回答

6

你的方法返回int,但你似乎並沒有使用結果,而是將它們改爲void。在switch案件

嘗試測試的東西,如:

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

這將打印結果sdtout

另請注意,按照Java和SO慣例,代碼都應該在English(儘管在這種情況下很明顯)。

3

如果你想看到的結果,使用來自於一個新的變量的方法main返回值(比方說,result),或者打印出來使用System.out.println()或類似的東西在裏面方法的結果。例如像這樣:

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); 
+1

謝謝,我期待的!!!! – user3333587

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) 
2

如果要返回的結果,那麼你應該把它放在一些valriable或者可以soprintln 像直接顯示由...

switch(wahl){ 
case 1: 
    system.out.println(addieren(a,b)); 
or 
int result = addieren(a,b) 
system.out.println(result); 
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) 
0

OK這裏有幾個三分球,可能是有用的:

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種操作方法靜態的,因爲你把它在主,編譯器會抱怨靜態參考?如果是這樣,請仔細閱讀一下static的含義;並考慮通過創建計算器的一個實例:


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

  1. 提供一個構造函數方法和
  2. main像創建它的一個實例

    希望它有幫助

0

您可以使用此示例

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

}