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[buyList[0][1]] 而不是 price[i],请改用:

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
}

除此之外,正如建议的那样,不要每次都创建一个 Scanner,此外,您已经在 tempPrice 变量中有价格,您不应该计算 2 次(此代码中的第 2 行和第 4 行)

希望能帮助到你。

于 2013-01-22T16:11:59.860 回答