0

宿題用のショップシミュレーターを書いています。ユーザーは、販売するアイテム (セット名と価格) を入力し、ID (1-5) とカウントを入力してこれらのアイテムを購入します。そして-価格計算(私の問題)。

簡単に行う必要がありますが、何が悪いのかわかりません。最終価格に奇妙な値があり、その理由がわかりません。

このコードでは、プロセスを最もよく理解するために、中間の数値を示す「デバッガー」コードの行をいくつか追加しました。

import java.util.Scanner; 

public class HomeWork3Shop {

    private static Scanner inputAdmin;

    public static void main(String[] args) {

        String[] items = new String[6];
        int[] price = new int[6];

        // The administrator adds the information about the products
        System.out.println("Administrator: add five items - name and price: ");
        for (int i = 1; i < 6; i++) {
            // if int = 0 -- will be "item 0: xxx" - not good
            System.out.print(" item " + i + ": ");
            inputAdmin = new Scanner(System.in);
            items[i] = inputAdmin.next();
            System.out.print("Price " + i + ": ");
            inputAdmin = new Scanner(System.in);
            price[i] = inputAdmin.nextInt();

        }

        int[][] buyList = new int[2][6];
        String yn = null;
        System.out.print("\nAdded. Plese buy - enter ID of item (1-5): ");

        int i = 1;
        for (int i2 = 0; i2 < 5; i2++) {
            // Enter ID of item:
            Scanner inputShoper = new Scanner(System.in);
            buyList[0][i] = inputShoper.nextInt();
            // Insert ID of item to the array - for next price count

            System.out.print("How much? (Enter a number): ");
            buyList[1][i++] = inputShoper.nextInt();
            System.out.print("\nIn bag. Want buy more? [y/n] ");

            Scanner YN = new Scanner(System.in);
            yn = YN.next();
            if (yn.equals("n")) {               
                break;
            }

            System.out.print("Enter ID of next item to buy: ");

        }


        for (int row = 0; row < buyList.length; row++) {
            // paint a table
            for (int col = 0; col < buyList[row].length; col++) {               
                System.out.print(buyList[row][col] + "\t");                   
            }             
            System.out.println();            
        }


        for (int temp = 0; temp < items.length; temp++) {              
            System.out.print(" " + items[temp]);                
        }

        for (int temp = 0; temp < items.length; temp++) {                
            System.out.print(" " + price[temp]);               
        }

        // ----- price count
        int totalPrice = 0;
        int tempPrice = 0;
        for (i = 1; i < buyList[0].length; i++) {                
            tempPrice = buyList[1][i] * price[i];
            System.out.print(" | " + tempPrice);
            totalPrice += buyList[1][i] * price[i];
            System.out.println(totalPrice);
            // count * price             
        }

        System.out.println("Your price is: " + totalPrice);

        // ----- black list -----
        System.out.print("How much money you have? ");

        int cash = 0;
        Scanner Cash = new Scanner(System.in);
        cash = Cash.nextInt();

        if (cash < totalPrice) {                
            System.out.println("You are in our Black List.");            
        }
        else {                
            System.out.println("Thank you for purchasing.");                
        }

    }

}

出力:

 Administrator: add five items - name and price: 
     item 1: Milk
    Price 1: 11
     item 2: Broad
    Price 2: 22
     item 3: Mouse
    Price 3: 33
     item 4: Keyboard
    Price 4: 44
     item 5: Monitor
    Price 5: 55

    Added. Plese buy - enter ID of item (1-5): 1
    How much? (Enter a number): 1

    In bag. Want buy more? [y/n] y
    Enter ID of next item to buy: 2
    How much? (Enter a number): 2

    In bag. Want buy more? [y/n] y
    Enter ID of next item to buy: 5
    How much? (Enter a number): 4

    In bag. Want buy more? [y/n] n
    0 1   2   5   0   0   
    0 1   2   4   0   0   
     null Milk Broad Mouse Keyboard Monitor 0 11 22 33 44 55 | 1111
     | 4455
     | 132187
     | 0187
     | 0187
    Your price is: 187

最終価格 - 187。

(11*1) + (22*2) + (55*4) = 22 + 44 + 220 = 286.

286 - である必要がありますが、このコードは 187 になります。

次のコード行で計算します。

totalPrice += buyList[1][i] * price[i];
4

1 に答える 1

2

計算ループにエラーがあります。price[i] の代わりに price[buyList[0][1]] をカウントしているアイテムの価格にアクセスする必要があります。代わりにこれを使用します。

for (i = 1; i < buyList[0].length; i++) {
    tempPrice = buyList[1][i] * price[buyList[0][i]];
    System.out.print(" | " + tempPrice);
    totalPrice += buyList[1][i] * price[buyList[0][i]];
    System.out.println(totalPrice);
    // count * price
}

これとは別に、提案されているように、毎回スキャナーを作成しないでください。また、tempPrice 変数に既に価格があるため、2 回計算しないでください (このコードの 2 行目と 4 行目)。

それが役に立てば幸い。

于 2013-01-22T16:11:59.860 に答える