gpt4 book ai didi

java - POS:如何将每件商品的价格相加得出总价?

转载 作者:行者123 更新时间:2023-12-01 19:03:21 25 4
gpt4 key购买 nike

(还没完。)我不知道在询问用户订单后如何获得总价。示例:我订购了 5 个 Piato,然后输入 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。您只需将 itemone 的最后一个值分配给 itemtotalvisible。因此,由于您选择的最后一个项目是 Piatos,因此 itemtotalvisible 等于一个 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);

关于java - POS:如何将每件商品的价格相加得出总价?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59598699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com