((尚未完成)我是编码新手,我有问题...我不知道如何获得总价。示例:我订购了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(){
System.out.println("====================================");
System.out.println("Point of Sale System");
System.out.println("WELCOME TO GRILLBY'S MINI MART!!!");
System.out.println("Snowdin town, Underground city");
System.out.println("====================================");
System.out.println("");
System.out.println("");
int Piatos = 20, Vcut = 20, Coke = 15, Pepsi = 15, Hiro = 8, Dewberry = 10, CheeseB = 25, GarlicB = 26, Small = 10, Medium = 18, CheeseF = 15, BBQF = 16;
System.out.println(" MENU ");
System.out.println("_____________________________________________________________________________");
System.out.println(" Chips Soda Biscuit");
System.out.println("");
System.out.println("[a]Piatos = Php 20.0 [c]Coke = Php 15.0 [e]Hiro = Php 8.0 ");
System.out.println("[b]V-cut = Php 20.0 [d]Pepsi = Php 15.0 [f]Dewberry = Php 10.0");
System.out.println("_____________________________________________________________________________");
System.out.println(" Burger Water Fries");
System.out.println("");
System.out.println("[g]Cheese = Php 25.0 [h]Small = Php 10.0 [i]Cheese = Php 15.0");
System.out.println(" Burger fries");
System.out.println("");
System.out.println("[j]Garlic = Php 26.0 [k]Medium = Php 18.0 [l]BBQ = Php 16.0");
System.out.println(" Glazed fries");
System.out.println(" Burger ");
double itemtotal = 0, itemtotalvisible, itemlone = 0;
String Ia = "a";
String Ib = "b";
String Ic = "c";
String Ie = "e";
String If = "f";
String Ig = "g";
String Ih = "h";
String Ii = "i";
String Ij = "j";
String Ik = "k";
String Il = "l";
System.out.println("====================================================================");
System.out.println("*Choose from the menu*");
System.out.println("Enter the letter that matches the item here: ");
System.out.println("Type \"End\" to stop selecting from the menu." );
//Pls work bro
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
。因此,因为您选择的最后一项是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);