2013-07-02 1 views
0

나는 책, 자기 연구를 연습하고있다. 그것은 내 마음을 불고있다. 누군가 나를 도울 수 있다면?모듈러 분할을 사용하는 방법?

첫 번째, 두 번째 및 세 번째 가방의 양을 빼고 총 가방 수를 개 수집하려면 어떻게합니까?

프로젝트는 이러한 기준을 묻습니다> 배정 세부 정보 이 회사는 2 파운드짜리 가방에만 커피를 판매합니다. 각 봉지의 가격은 5.50입니다. 고객이 주문을하면 상자에 커피가 배송됩니다. 상자의 크기는 3 가지입니다. 대형 (20 봉지를 포함), 매체 (10 봉지를 포함하고 작은 (5 봉지가 들어 있습니다.

큰 상자의 비용은 $ 1.80 매체 $ 1.00 작은 $ 0.60 순서가, 가장 저렴한 방법으로 제공된다 예 : 포장용 고리는 대용량 상자를 완전히 채우는 것입니다. 즉, 상자가 두꺼운 경우에만 포장 할 수 있습니다. 작은 상자에만 빈 칸을 넣을 수 있지만 세 번째 상자를 완전히 포장 한 상태로 두지는 마십시오. 주문의 총 비용을 계산합니다 :

주문한 가방 수 : 52 - $ 286.00

박스 2 대 을 사용 - 3.60 1 매체 - 1.00 1 작은 - 0.60

귀하의 총 비용은 : $ 291.20

샘플 코드

import javax.swing.*; 
    import java.lang.*; 
    import java.text.DecimalFormat; 
    import java.util.*; 
    import java.io.*; 
    import java.math.*; 

    public class CoffeeBags { 

    public static void main(String [] args) throws IOException 
    { 
     DecimalFormat df = new DecimalFormat ("#,##0.00"); 

     BufferedReader bufReader = new BufferedReader(new InputStreamReader(System.in));     

     int numberOfBags; 
     int largeBags = 2; 
     int mediumBags = 1; 
     int smallBags = 1; 
     double costOfLargeBags = 3.60; 
     double costOfMediumBags = 1.00; 
     double costOfSmallBags = 0.60; 
     double costPerBag = 5.50; 
     double totalCost; 
     double totalCostWithBags; 

     System.out.print("Enter number of bags to order: "); 
     String numberOfBagsStr = bufReader.readLine(); 
     numberOfBags = Integer.parseInt(numberOfBagsStr); 


     totalCost = numberOfBags * costPerBag; 
     System.out.println("Number of bags ordered: " + numberOfBags + " - $" +  df.format(totalCost)); 

     System.out.println("Boxes used:"); 
     System.out.println(" " + largeBags + " Large - $" +  df.format(costOfLargeBags)); 
     System.out.println(" " + mediumBags + " Medium - $" + df.format(costOfMediumBags)); 
     System.out.println(" " + smallBags + " Small - $" + df.format(costOfSmallBags)); 
     //System.out.print("Your total cost is: $ " + (totalCostWithBags)); 
     //String numberOfUnitsStr = bufReader.readLine(); 
     //numberOfUnits = Integer.parseInt(numberOfUnitsStr); 

     System.out.println("\nPress any key to continue. . ."); 

     System.exit(0); 
    } 
} 

답변

2

당신은 기반, 운동을 오해하고 변수 이름에. 크기에 따라 20/10/5 봉지에 들어갈 수있는 상자가 있습니다. 큰 상자와 중간 상자는 가득 차 있어야하며, 작은 상자는 가득 차지 않아야합니다.

가방의 전체 개수를 20 개로 나눠서 큰 상자에 넣으십시오. 나머지는 10으로 나누고 그 나머지는 작은 상자로 나눕니다.

운영자를 위해 here을 찾으십시오. int을 다른 int으로 나누면 항상 반올림됩니다.

1

나는 목표를 달성하는 다음 코드를 생각해 냈습니다. 이 상황에서 정수를 사용하면 매우 유용합니다. 또한 변수의 이름을 올바르게 지정해야합니다. 그들은 가방에 들어가는 가방이 아니라 상자에 들어가는 가방입니다.

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.text.DecimalFormat; 

public class CoffeeBags { 
public static void main(String [] args) throws IOException { 
    DecimalFormat df = new DecimalFormat ("#,##0.00"); 

    BufferedReader bufReader = new BufferedReader( 
         new InputStreamReader(System.in)); 

    int    numberOfBags = 0; 
    int    numberOfBoxes = 0; 
    int    numberLargeBoxes = 0; 
    int    numberMediumBoxes = 0; 
    int    numberSmallBoxes = 0; 
    double   costOfLargeBox = 1.80; 
    double   costOfMediumBox = 1.00; 
    double   costOfSmallBox = 0.60; 
    double   totalCostLargeBoxes; 
    double   totalCostMediumBoxes; 
    double   totalCostSmallBoxes; 
    double   totalBoxCost; 
    double   costPerBag = 5.50; 
    double   totalBagCost; 
    double   totalCostWithBags; 

    System.out.print("Enter number of bags to order: "); 
    String numberOfBagsStr = bufReader.readLine(); 
    try { 
     numberOfBags = Integer.parseInt(numberOfBagsStr); 
    } catch (NumberFormatException e) { 
     System.out.println("Error: Enter digits only"); 
    } 


    totalBagCost = numberOfBags * costPerBag; 
    if (numberOfBags > 20) { 
     numberLargeBoxes = numberOfBags/20; 
    } 
    if (numberOfBags - (numberLargeBoxes*20) < 20 || numberLargeBoxes == 0) { 
     numberMediumBoxes = (numberOfBags - (numberLargeBoxes*20))/10; 
    } 
    if (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) < 10 || 
      numberLargeBoxes == 0 || numberMediumBoxes == 0) { 
     numberSmallBoxes = (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10))/5; 
    } 
    if (numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) - 
      (numberSmallBoxes*5) < 5 && numberOfBags - (numberLargeBoxes*20) - (numberMediumBoxes*10) - 
      (numberSmallBoxes*5) > 0 || numberLargeBoxes == 0 || numberMediumBoxes == 0) { 
     numberSmallBoxes++; 
    } 

    totalCostLargeBoxes = numberLargeBoxes*costOfLargeBox; 
    totalCostMediumBoxes = numberMediumBoxes*costOfMediumBox; 
    totalCostSmallBoxes = numberSmallBoxes*costOfSmallBox; 
    totalBoxCost = totalCostLargeBoxes + totalCostMediumBoxes + totalCostSmallBoxes; 
    totalCostWithBags = totalBoxCost + totalBagCost; 



    System.out.println("Number of bags ordered: " + numberOfBags + " - $" + df.format(totalBagCost)); 

    System.out.println("Boxes used: "); 
    System.out.println(" " + numberLargeBoxes + " Large - $" +  df.format(totalCostLargeBoxes)); 
    System.out.println(" " + numberMediumBoxes + " Medium - $" + df.format(totalCostMediumBoxes)); 
    System.out.println(" " + numberSmallBoxes + " Small - $" + df.format(totalCostSmallBoxes)); 
    System.out.println("Your total cost is: $ " + df.format(totalCostWithBags)); 

} 
} 
+0

이것은 확실하게 작동하지만 각 if 내부에서 변경 한 가방 수에 변수를 사용하면 코드가 훨씬 멋지게 보입니다. 모든 거대한 조건문을 없앨 수 있습니다. (그가 배운 이래로 나는 생각했습니다. 완전한 코드 응답을 제공해서는 안되며, 저에게 도움이되지 않습니다.) – Marconius

+0

그래, 충분히 공정해. 더 복잡한 것이 필요한 경우 또는 기능을 추가해야하는 경우 항상 미래에 대한 고개를 끄덕이며 앱을 개발합니다. 그래서 거대한 조건문을 사용하기로 결정했습니다. 나는 완전한 코드 응답도 고려해 봤지만 때로는 앞에서 답을 얻고 그 해결책을 찾던 방법을 찾는 방법을 찾는 데 도움이됩니다. 나는 내가 그렇게 잘 배우는 것을 발견한다. –

관련 문제