2014-02-21 3 views
0

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; 

    } 

} 

일부 누수가있을 수 있습니까?

나는 이것을 자바에서 연습하기위한 메소드와 리턴 함수로하고 싶었다.

+2

실제로하고있는 것은 무엇입니까? 반환 값'c'을 전혀 사용하지 않으므로 결과가 어떻게 잘못 될 수 있습니까? – skiwi

+4

아무 결과도 인쇄하지 않으면 결과가 잘못되었다는 것을 어떻게 알 수 있습니까? –

+1

어떤 결과가 표시됩니까? 매번 바이 바이 월드를 보여줄 것입니다. – 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 :) 

:처럼 main 뭔가의 인스턴스를 만드는 생성자 메서드를 제공

  1. :에 의해 계산기의 인스턴스를 만드는 것이 좋습니다

    희망 하시겠습니까?

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

} 
관련 문제