【问题标题】:POS: How do I sum the price per item to get the total price?POS:如何将每件商品的价格相加得到总价?
【发布时间】:2020-04-23 04:56:13
【问题描述】:

(还没完结。) 在向用户询问订单后,我不知道如何获得总价。示例:我订购了 5 个 Piatos,然后输入 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);

    }
}

【问题讨论】:

    标签: java pos


    【解决方案1】:

    每次在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);
    

    【讨论】:

    • 成功了!现在总结一下。但是我的这部分代码又遇到了另一个问题。我做了你所做的,但如果我输入 3 a's(即 3 Piatos ......所以 20+20+20)它说 80,但它应该是 60。它的作用是增加了额外的 20再次。如果我执行 20+20+30 则为 100,假设为 70,但将最后一个数字添加为额外数字,则为 100。
    猜你喜欢
    • 2020-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    相关资源
    最近更新 更多