我有我正在爲我的大學任務工作的以下代碼。我被要求使用我正在使用的數組。我被要求使用for循環和if語句,我已經在做。我想出了下面的代碼:檢查輸入與Java中的字符串數組中的項目
class HardwareStore2 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("%55s", "**WELCOME TO THE HARDWARE STORE**\n");
System.out.printf("%55s", "=================================\n");
String [] codes = {"G22", "K13", "S21", "I30"};
String [] description = {"STICKY Construction Glue", "CAR-LO Key Ring", "SCREW-DUP Screwy Screws", "LET-IT-RAIN Padlock"};
List<String> codeList = Arrays.asList(codes);
String output = "";
int i = 1000;
String [] userCode = new String[i];
char dolSymb = '$';
int [] pricesAndTax = {10989, 5655, 1099, 4005, 20};
int [] weight = {};
int [] userQuantity = {1};
int [] userPrice = new int[i];
int userStickyPrice = 0;
int userKeyringPrice = 0;
int userScrewyPrice = 0;
int userPadlockPrice = 0;
int userPreWithTax = 0;
int userTotal = 0;
int userPreTotal = 0;
int userShipping = 0;
System.out.printf("%-10s%-40s%-15s%-10s\n", "CODE", "DESCRIPTION", "WEIGHT", "PRICE\n");
System.out.printf("%-10s%-55s%s%d.%02d\n", codes[0], description[0], dolSymb, pricesAndTax[0]/100, pricesAndTax[0]%100);
System.out.printf("%-10s%-55s%s%d.%02d\n", codes[1], description[1], dolSymb, pricesAndTax[1]/100, pricesAndTax[1]%100);
System.out.printf("%-10s%-55s%s%d.%02d\n", codes[2], description[2], dolSymb, pricesAndTax[2]/100, pricesAndTax[2]%100);
System.out.printf("%-10s%-55s%s%d.%02d\n", codes[3], description[3], dolSymb, pricesAndTax[3]/100, pricesAndTax[3]%100);
System.out.println("PLEASE ENTER YOUR ORDER:");
System.out.print("NAME: ");
String username = in.nextLine();
System.out.print("ADDRESS Line 1: ");
String address1 = in.nextLine();
System.out.print("ADDRESS Line 2: ");
String address2 = in.nextLine();
System.out.print("POSTAL CODE: ");
String postalcode = in.nextLine();
for (i = 0;; i++) {
System.out.print("CODE (X to QUIT):");
userCode[i] = in.nextLine();
if (userCode[i].equalsIgnoreCase("x")) {
break;
}
System.out.print("QUANTITY: ");
userQuantity[i] = in.nextInt();
in.nextLine();
if (userCode[i].equalsIgnoreCase(codes[0])) {
userStickyPrice += userQuantity[i]*pricesAndTax[0];
}
else if (userCode[i].equalsIgnoreCase(codes[1])) {
userKeyringPrice += userQuantity[i]*pricesAndTax[1];
}
else if (userCode[i].equalsIgnoreCase(codes[2])) {
userScrewyPrice += userQuantity[i]*pricesAndTax[2];
}
else if (userCode[i].equalsIgnoreCase(codes[3])) {
userPadlockPrice += userQuantity[i]*pricesAndTax[3];
}
else if (!codeList.contains(userCode)) {
i = i - 1;
}
}
}
}
現在,一切都以百萬IFS無縫協作,並別人的,但我想知道是否有替換所有的if-else-if語句wirht方式一個或兩個做這樣的事情:
if (userCode[i].contains(codes[0], codes[1], codes[2], codes[3] {}
或者,也許更好的東西,如:
if (userCode.contains(any.one.item.in.codeList) {
then.get.the.price.of.that.item.and.do.item.specific.operations
}
請讓我知道,如果這個問題不夠清楚。再次,這是一個大學作業,所以我會很感激解釋。
如果「地圖」更好,我確實有時間和精力去尋找那個方向。由於我是一個Java新手,你是指Hashmaps還是我完全在這裏? –
@nickecarlo Hashmap是正確的,只要確保使用通用版本'Hashmap'(它可以提供更好的類型安全性)。您將定義一個局部變量,如下所示:Map codeToPriceAndTax = new HashMap ;'然後,您將在初始化期間向該映射添加代碼/價格+稅收對。在你的主循環中,你將使用'int priceAndTax = codeToPriceAndTax.get(userCode [i])'來通過代碼循環價格+稅。 –
dasblinkenlight
我會檢查一下HashMap,謝謝。 –