나는 책, 자기 연구를 연습하고있다. 그것은 내 마음을 불고있다. 누군가 나를 도울 수 있다면?모듈러 분할을 사용하는 방법?
첫 번째, 두 번째 및 세 번째 가방의 양을 빼고 총 가방 수를 개 수집하려면 어떻게합니까?
프로젝트는 이러한 기준을 묻습니다> 배정 세부 정보 이 회사는 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);
}
}
이것은 확실하게 작동하지만 각 if 내부에서 변경 한 가방 수에 변수를 사용하면 코드가 훨씬 멋지게 보입니다. 모든 거대한 조건문을 없앨 수 있습니다. (그가 배운 이래로 나는 생각했습니다. 완전한 코드 응답을 제공해서는 안되며, 저에게 도움이되지 않습니다.) – Marconius
그래, 충분히 공정해. 더 복잡한 것이 필요한 경우 또는 기능을 추가해야하는 경우 항상 미래에 대한 고개를 끄덕이며 앱을 개발합니다. 그래서 거대한 조건문을 사용하기로 결정했습니다. 나는 완전한 코드 응답도 고려해 봤지만 때로는 앞에서 답을 얻고 그 해결책을 찾던 방법을 찾는 방법을 찾는 데 도움이됩니다. 나는 내가 그렇게 잘 배우는 것을 발견한다. –