0

宿題で困っています。基本的なロジックはダウンしていますが、めちゃくちゃです。目的は、ユーザーが入力した買い物リストからレシートを作成することです。たとえば、ユーザーは次のように入力します。

Apples

OraNgeS // also it's not case sensitive

Oranges

Bananas

!checkout //this is to indicate the list is over

出力:

Apples x1

Oranges x2

Bananas x1

私は立ち往生しています。これまでの私のコード:

public static void main(String[] args) {
    Scanner  keyboard    = new Scanner(System.in);

    System.out.printf("Enter the items you wish to buy:"); 
    String[] input = new String [keyboard.nextLine()];
    keyboard.nextLine(); //consuming the <enter> from input above

    for (int i = 0; i < input.length; i++) {
        input[i] = keyboard.nextLine();
    }

    System.out.printf("\nYour input:\n");
    for (String s : input) {
        System.out.println(s);
    }
}

最終的には if ステートメントを追加する必要があることはわかっているので、「!checkout」と入力するとリストが終了します。しかし、私はまだこれを乗り越えることができません。

ヒントやアドバイスはありますか?

4

2 に答える 2

-1

次のコードは、探していることを正確に実行します。

       Scanner keyboard = new Scanner(System.in);
       List<String> inputItems = new ArrayList<String>();       
       List<String> printedResults = new ArrayList<String>();
       String input = keyboard.nextLine();       

       while(!"!checkout".equals(input)) 
       {           
           inputItems.add(input);           
           input = keyboard.nextLine();
       }

       for(int i=0; i<inputItems.size();i++)
       {
           Integer thisItemCount = 0;
           String currentItem = inputItems.get(i);

           for(int j=0; j<inputItems.size();j++)
           {
                if(inputItems.get(j).toLowerCase().equals(currentItem.toLowerCase()))    
                    thisItemCount++;
           }

           if(!printedResults.contains(currentItem.toLowerCase()))
           {
               System.out.println(currentItem.toLowerCase() + " x" + thisItemCount);
               printedResults.add(currentItem.toLowerCase());
           }               
        }
于 2016-11-11T12:26:33.837 に答える