2013-12-16 2 views
-4

두 답변이 모두 수정 됨으로 게시되었습니다. 고맙습니다. 얼마나 간단한 지 당혹 스럽네요.메서드 호출에서 구문 오류가 발생했습니다.

이런 기본적인 질문을 게시 해 주셔서 죄송하며 시간 내 주셔서 감사합니다.

상단의 메뉴 방법에서 입력을 기반으로 작업을 완료하기 위해 2 가지 방법을 호출하고 싶습니다. 나는 void 메소드를 호출하는 방법을 찾을 수 없다.

이전에 비슷한 것을 사용했기 때문에 두 번째 이유가 작동하지 않는 이유를 모르겠습니다.

public void CheckOutMenu(ArrayList basket) 
{ 
    choice = 0; 
    while ( choice !=4) { 

     Scanner in = new Scanner (System.in); 

     { 
      switch(in.nextInt()){ 
       //print the number of items in the basket 
       case 1: 
       //working 
       break; 

       case 2: 
         //dont know how to call on option 2 the listBasket method 
       break; 

       case 3: 

         //to call the method 
        //error and wont compile 
       double totalCost = CalcTotalCost(totalCost = 0); 

        //printing what the method returns 
       System.out.print("The total price of your basket is £" ); 

       break; 

       case 4: 
       choice = 4; 
       break; 

       default: 
       System.out.println("please enter a whole number that represents you're choice"); 

      } 

     } 
    } 
} 


protected void listBasket(ArrayList basket) 
{ 
    //code inside works fine 


} 


public double CalcTotalCost(double total, ArrayList basket) 
{ 
//code inside works fine 
return total; 
} 
+0

두 가지 : 1) 코드를 형식화하십시오. 그것은 당신과 다른 사람들의 시간을 절약 할 것입니다. 2) 정확히 오류가 무엇입니까? 오류 메시지는 항상 잘못된 것을 알려줍니다. –

답변

0

반환 값이 없으므로 반환 값을 사용하지 않아도된다는 점을 제외하면 다른 방법과 마찬가지로 void 메서드를 호출 할 수 있습니다. 세 번째 경우에 대한

case 2: 
    listBasket(basket); 
    break; 

, 당신은에 통과해야 모두 double 당신은 당신이 단지 double 문자를 게시 할 수있는 것보다, 당신의 double0을 전달하려는 경우 ArrayList.

그러나
double totalCost = CalcTotalCost(totalCost = 0); 

, 당신의 CalcTotalCost 방법에 따라서 정의됩니다 :

case 3: 
    /* if you don't want totalCost to go out of scope right away, 
    * declare it above the switch statement */ 
    double totalCost = CalcTotalCost(0.0, basket); 
    ... 
    break; 
0

귀하의 오류가 나는 확신 당신도 알다시피, 여기 당신은 그것에게 총을 보낸

public double CalcTotalCost(double total, ArrayList basket) 

,하지만 당신은 바구니를 보내지 않았습니다.