Note that there are some explanatory texts on larger screens.

plurals
  1. POWrong results while calculate sum
    primarykey
    data
    text
    <p>I am writing a shop simulator for homework. A user inputs items for sale (set name and price) and then the user buys these items - entering ID (1-5) and count. And then - price counting (my problem). </p> <p>It must be done simply, but I can't find what is wrong. The final price has strange values, and I can't understand why.</p> <p>In this code I added some lines of "debugger" code, that show intermediate numbers - for best understanding of process.</p> <pre><code>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 &lt; 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 &lt; 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 &lt; buyList.length; row++) { // paint a table for (int col = 0; col &lt; buyList[row].length; col++) { System.out.print(buyList[row][col] + "\t"); } System.out.println(); } for (int temp = 0; temp &lt; items.length; temp++) { System.out.print(" " + items[temp]); } for (int temp = 0; temp &lt; items.length; temp++) { System.out.print(" " + price[temp]); } // ----- price count int totalPrice = 0; int tempPrice = 0; for (i = 1; i &lt; 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 &lt; totalPrice) { System.out.println("You are in our Black List."); } else { System.out.println("Thank you for purchasing."); } } } </code></pre> <p>Output:</p> <blockquote> <pre><code> 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 </code></pre> </blockquote> <p>Final price - 187. But:</p> <p>(11*1) + (22*2) + (55*4) = 22 + 44 + 220 = 286.</p> <p>286 - must be, but this code get 187.</p> <p>Its calculate in this line of code:</p> <pre><code>totalPrice += buyList[1][i] * price[i]; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload