POS:如何将每件商品的价格相加以获得总价?

POS: How do I sum the price per item to get the total price?

(还没完。) 在向用户询问订单后,我不知道如何获得总价。示例:我订购了 5 个皮亚托,然后我输入 end 来显示结果或总数,但它只显示 20,但它应该是 100,因为 20+20+20+20+20 = 100。我如何计算这些个别价格,以便在不更改订购方式的情况下显示正确的总价? (仅选择为每个项目提供的字母。)

import java.util.Scanner;
public class Point_Of_Sale_System_Real {

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

    public static void Intro(){  

        int Piatos = 20, Vcut = 20;

        double itemtotal = 0, itemtotalvisible, itemlone = 0;

        String Ia = "a";
        String Ib = "b";

        System.out.println("Enter the letter that matches the item here: ");
        System.out.println("Type \"End\" to stop selecting from the menu." );

        String itemselect = "";
        do {
            itemselect = sc.nextLine();   
            if (itemselect.equalsIgnoreCase(Ia)){
                itemlone = 0 + Piatos;
            }
            else if (itemselect.equalsIgnoreCase(Ib)){
                itemlone = 0 + Vcut;
            }
        }
        while (!itemselect.equalsIgnoreCase("end"));

        itemtotalvisible = itemlone + 0;
        System.out.println("Total" + itemtotalvisible);

    }
}

每次在 do-while 循环中进行选择时,都需要更新 itemtotalvisible。您只是将 itemlone 的最后一个值分配给 itemtotalvisible。因此,因为您选择的最后一个项目是 Piatositemtotalvisible 等于一个 Piatos 项目的价值。

以下代码不是完整的答案,但希望足以帮助您修复代码。

double itemtotalvisible = 0;
String itemselect = "";
do {
    itemselect = sc.nextLine();
    if (itemselect.equalsIgnoreCase(Ia)){
        itemlone = 0 + Piatos;
    }
    else if (itemselect.equalsIgnoreCase(Ib)){
        itemlone = 0 + Vcut;
    }
    itemtotalvisible += itemlone;
}while (!itemselect.equalsIgnoreCase("end"));

System.out.println("Total" + itemtotalvisible);