2012-01-23 6 views
3

私は私の大学の割り当てのために働いている次のコードを持っています。私は使用している配列を使用するように求められています。私はfor-loopsと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を交換する方法があるかどうかを知りたい:私は次のコードが出ています1またはこのような何かを2:

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 
    } 

は、質問が十分に明確でない場合は私に知らせてください。そしてもう一度、これは大学の授業ですので、私は説明を感謝します。より効率的な何かのためにあなたのデータ構造の残りの部分を変更することなく、

答えて

4

(たとえば、Map)あなたは、単一のifおよびネストされたループと同じ効果を得ることができは:

boolean found = false; 
for (int j = 0 ; !found && j != codes.length ; j++) { 
    if (userCode[i].equalsIgnoreCase(codes[j])) { 
     userScrewyPrice += userQuantity[i]*pricesAndTax[j]; 
     found = true; 
    } 
} 
if (!found) { 
    i--; 
} 
+0

"マップ"が優れている場合、私はその方向を見るために時間とエネルギーの両方を持っています。私はJavaを初心者としているので、ハッシュマップを意味するのですか、ここから完全に離れていますか? –

+1

@nickecarlo Hashmapが正しいです。汎用のバージョン 'Hashmap '(より良い型安全性を提供します)を使用していることを確認してください。 'Map codeToPriceAndTax = new HashMap ;'初期化中にコード/価格+税のペアをそのマップに追加します。あなたのメインループの中で、 'int priceAndTax = codeToPriceAndTax.get(userCode [i])'を使用してコードで価格+税をループアップします。 – dasblinkenlight

+0

ハッシュマップをチェックします。ありがとうございます。 –

2

変数codeListListです私はあなたがしようとしていることを理解している場合に役立つ可能性のあるcontains機能を持っています。

また、Java 7以降では、文字列をswitchステートメントの引数として使用することができ、コードがより見栄えがよくなります。

2

私は、これは一部のみの答えかもしれ完全な質問を読んでいない...しかし、あなたが使用することができれば、あなたが尋ねる:

if (userCode[i].contains(codes[0], codes[1], codes[2], codes[3] {} 

をあなたが好きなものを使用することができるかもしれ...

if(new string[]{"a","b","c"}.Contains("a")) {} 

またはカスタムアレイタイプにこれを置くために...基本的に

arrayType[] a = new arrayType[]{item1, item2, item3} 
if (arrayType.Contains(searchItem)) {} 

- あなただけの、あなたが求めて何ができます構文の順序を変更する必要があります。しかし、これは私が確信しているのは、あなたの考えを得るための一部の答えだけです。

1

複数の "if"ステートメントがある場合は、代わりに "switch"ステートメントを使用してください。

switch文のように見えますが、ここにかなりの行が保存されます。

関連する問題