计算总和时错误的结果

时间:2013-01-22 15:53:33

标签: java sum calculator

我正在为家庭作业写一个商店模拟器。用户输入待售物品(设置名称和价格),然后用户购买这些物品 - 输入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];

1 个答案:

答案 0 :(得分:2)

计算循环中存在错误。您需要访问您正在计算价格[buyList [0] [1]]而不是价格[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
}

除此之外,正如建议的那样,每次都不要创建一个扫描仪,而且,你已经在tempPrice变量中有了价格,你不应该计算2次(这段代码中的第2行和第4行)

希望它有所帮助。