2016-10-09 5 views
-2

스위치 내부의 변수는 case 문 범위 밖에서 선언해야하지만 스위치 범위를 벗어난 모든 계산을 던지려고 시도했지만 여전히 "전체" 결과이 스크립트에서 원합니다. 누구든지 제발 도와 주실 래요? 이 스크립트의 맨 끝에있는 마지막 블록 주석문을 사용하면됩니다. 당신이 (당신의 주석 처리 된 코드가 제안 보인다) 루프 후, 말, wineTotal을 사용하려는 경우 그것은 또한 어떤 경우에 값을 가지도록작동하지 않을 수 있습니다.

import java.util.Scanner; 

public class script 
{ 
    public static void main(String[] args) 
    { 
    //initialization 
    Scanner key = new Scanner(System.in); 
    final double COST_WINE = 13.99; // What is the size of the bottle of wine? *********************************** 
    final double COST_BEER_6 = 11.99, COST_BEER_12 = 19.99, COST_BEER_24 = 34.99; 
    final double spirits_750 = 25.99, spirits_1000 = 32.99; 
    final double gst = 0.05; //confirm if this number is right*************************************************** 
    final double deposit750 = 0.10, deposit1000 = 0.25; 

    double wineSubTotal, wineTotal , total_cost; 
    double beer_6_SubTotal, beer_6_Total; 
    double beer_12_SubTotal, beer_12_Total; 
    double beer_24_SubTotal, beer_24_Total; 
    double spirits_750_SubTotal, spirits_750_Total; 
    double spirits_1000_SubTotal, spirits_1000_Total; 
    int wineQuantity, beer_6_Quantity, beer_12_Quantity; 
    int beer_24_Quantity, spirits_750_Quantity, spirits_1000_Quantity; 
    int beer_choice, spirits_choice; 
    char choice; 
    String input, again; 

    //loop for fun 
    for(int i = 0 ; i < 40; i++) 
    { 
    System.out.print("-"); 
    } 
    System.out.printf("\n*** Welcome to Yoshi's Liquor Mart ***\n"); 
    System.out.printf("Today's date is \n"); // Reserved for date*********************************************** 

    //another loop for fun      
    for(int i = 0 ; i < 40; i++) 
    { 
    System.out.print("-"); 
    } 

    //first actual loop, not for fun anymore 

      do 
      { 
      System.out.printf("\nWine is $13.99" + "\nBeer 6 pack is $11.99\n" + "Beer 12 pack is $19.99\n" + 
      "Beer 24 pack is $34.99\n" + "Spirits 750 mL is $25.99\n" + "Spirits 1000 mL is $32.99\n" + 
      "What is the item being purchased?\n" + "W for Wine, B for Beer and S for Spirits, or X to quit: "); 
      input = key.nextLine(); 
      choice = input.charAt(0); 

       switch(choice) 
       { 
       case('w'): 
       case('W'): 
         System.out.printf("\nHow many bottles of wine is being purchased? "); 
         wineQuantity = key.nextInt(); 
         wineSubTotal = wineQuantity * COST_WINE; 
         wineTotal = (wineSubTotal * gst) + wineSubTotal + (deposit750 * wineQuantity); 
         System.out.printf("\nThe cost of " + wineQuantity + 
         " bottles of wine including GST and deposit is %.2f\n", wineTotal); 
         break; 
       case('b'): 
       case('B'): 
         System.out.printf("What is size of the beer pack?\n" + "6, 12 or 24?"); 
         beer_choice = key.nextInt(); 
          if(beer_choice == 6) 
          { 
          System.out.printf("\nHow many 6 pack of beer is being purchased?"); 
          beer_6_Quantity = key.nextInt(); 
          beer_6_SubTotal = beer_6_Quantity * COST_BEER_6; 
          beer_6_Total = (beer_6_SubTotal * gst) + beer_6_SubTotal; 
          System.out.printf("\nThe cost of %d cases of 6 pack of beer" + 
          "including GST and deposit is %.2f\n", beer_6_Quantity, beer_6_Total); 
          } 
          else if(beer_choice == 12) 
          { 
          System.out.printf("How many 12 pack of beer is being purchased?"); 
          beer_12_Quantity = key.nextInt(); 
          beer_12_SubTotal = beer_12_Quantity * COST_BEER_12; 
          beer_12_Total = (beer_12_SubTotal * gst) + beer_12_SubTotal; 
          System.out.printf("\nThe cost of %d cases of 12 pack of beer" + 
          "including GST and deposit is %.2f\n", beer_12_Quantity, beer_12_Total);       
          } 
          else if(beer_choice == 24) 
          { 
          System.out.printf("How many 24 pack of beer is being purchased?"); 
          beer_24_Quantity = key.nextInt(); 
          beer_24_SubTotal = beer_24_Quantity * COST_BEER_24; 
          beer_24_Total = (beer_24_SubTotal * gst) + beer_24_SubTotal; 
          System.out.printf("\nThe cost of %d cases of 24 pack of beer" + 
          "including GST and deposit is %.2f\n", beer_24_Quantity, beer_24_Total); 
          } 
          else 
          { 
          System.out.println("Invalid number"); 
          } 
         break; 
       case('s'): 
       case('S'): 
         System.out.printf("What is size of Spirits?\n" + "750ml or 1000ml?"); 
         spirits_choice = key.nextInt(); 
          if(spirits_choice == 750) 
          { 
          System.out.printf("How many bottles are being purchased?"); 
          spirits_750_Quantity = key.nextInt(); 
          spirits_750_SubTotal = spirits_750_Quantity * spirits_750; 
          spirits_750_Total = (spirits_750_SubTotal * gst) + spirits_750_SubTotal + 
          (deposit750 * spirits_750_Quantity); 
          System.out.printf("\nThe cost of %d bottle(s) of Spirits 750ml " + 
          "including GST and deposit is %.2f\n", spirits_750_Quantity, spirits_750_Total);        
          } 
          else if(spirits_choice == 1000) 
          { 
          System.out.printf("How many bottles are being purchased?"); 
          spirits_1000_Quantity = key.nextInt(); 
          spirits_1000_SubTotal = spirits_1000_Quantity * spirits_1000; 
          spirits_1000_Total = (spirits_1000_SubTotal * gst) + spirits_1000_SubTotal + 
          (deposit1000 * spirits_1000_Quantity); 
          System.out.printf("\nThe cost of %d bottle(s) of Spirits 1000ml " + 
          "including GST and deposit is %.2f\n", spirits_1000_Quantity, spirits_1000_Total);        
          } 
          else 
          { 
          System.out.println("Invalid number"); 
          } 
         break; 
        case('x'):  
        case('X'): 

         break; 
        default: 
          System.out.printf("Invalid choice"); 
         break; 

       } 

      System.out.println("Is this customer's order complete? "); 
      again = key.nextLine(); 
      } 
      while(again.equals("n")); 


     //Total cost 

     /*total_cost = wineTotal + beer_6_Total + beer_12_Total + beer_24_Total + 
     spirits_750_Total + spirits_1000_Total; 

     System.out.printf("The total cost for this customer is %f", total_cost);*/ 

    key.close(); //There was a warning message(not error), so I found out on StackOverFlow that this would close the Scanner 
    } 

} 
+3

붙여 넣은 코드는 매우 길며 정확합니다. 처음부터 문제를 보여주는 최소한의 파일을 만들면 더 나은 답변을 얻을 수 있습니다. 읽기 http://stackoverflow.com/help/mcve –

+0

@ Cnunes 입력 및 예상 출력 말할 수 있습니까 ?? – cody123

답변

0

, 당신은 선언에서 0으로 초기화 안 와인을 사고있다. (나는 종종처럼) 당신이 당신의 switch 문장의 각 case에서 지역 변수를 선언하려는 경우

, 단지 {} 내부를 가지고 :

 case ('w'): 
     case ('W'): { 
      System.out.printf("\nHow many bottles of wine is being purchased? "); 
      int wineQuantity = key.nextInt(); 
      double wineSubTotal = wineQuantity * COST_WINE; 
      wineTotal = (wineSubTotal * gst) + wineSubTotal + (deposit750 * wineQuantity); 
      System.out.printf(
        "\nThe cost of " + wineQuantity + " bottles of wine including GST and deposit is %.2f\n", 
        wineTotal); 
     } 
      break; 

귀하의 프로그램은 루프를 통해 미세 처음 실행 . n에서 Is this customer's order complete?으로 대답해도 프로그램이 종료됩니다. 나는 이것이 key.nextInt()으로 많은 병을 읽은 후에 프로그램이 그 번호 다음에 줄 바꿈을 읽지 않았기 때문이라고 생각한다. 따라서 주문이 완료되었는지를 묻고 대답을 key.nextLine()으로 읽을 때, 프로그램은 양과 함께 라인의 나머지 부분을 취합니다. 일반적으로 빈 문자열은 "n"이 아니므로 do 루프를 종료합니다.

다음 질문에 대해서는 스택 오버플로 (있는 경우), 제임스 K의 제안은 여전히 ​​좋은 방법입니다. 게시하기 전에 최소한의 완전하고 검증 가능한 예를 만드십시오.

관련 문제