- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
创建一个在下订单之前需要密码的程序。成功输入密码后,客户应该会看到一条欢迎消息。然后是菜单。客户应该能够看到菜单和子菜单中的选项。订单(含价格)应在客户退出计划之前完成。
在我下面的代码中,它仍然要求输入密码,我不知道下一步该怎么做。另外,订单应该被统计并出现在客户退出之前的最后部分。请引导我完成这个!
欢迎来到 Favor Stuff(派对赠品)
请从可用菜单中选择。
A.万宝路灯
1. Soft Pack 70.00
2. Flip Top 80.00
B.万宝路红
1. Soft Pack 70.00
2. Flip Top 80.00
C.登喜路
1. Soft Pack 60.00
2. Flip Top 70.00
D.菲利普
1. Soft Pack 70.00
2. Flip Top 80.00
import java.util.Scanner;
import java.io.*;
public class MenuORDER {
public static void main(String[] args) {
int attempts = 0;
String Order;
Scanner keyboard = new Scanner(System.in);
String password = null;
String CORRECT_PWORD = "1234";
do {
System.out.println("Enter Password: ");
password = keyboard.next();
if (!password.equals(CORRECT_PWORD)) {
System.out.println("Password is incorrect!");
attempts++;
}
} while (!password.equals(CORRECT_PWORD) && attempts <3);
if (password.equals(CORRECT_PWORD)) {
System.out.println("Welcome to Favor Stuff (Party Giveaways)!");
do {
System.out.println("Please select from the available options below! ");
System.out.println("A. Marlboro Lights");
System.out.println("B. Marlboro Red");
System.out.println("C. Dunhill");
System.out.println("D. Phillip");
System.out.print("Sellect your brand: ");
System.out.println("");
String Price = keyboard.nextLine();
String Price1 = "";
String Price2 = "";
switch (Price) {
case "A": Price1 = "Soft Pack 70.00";
break;
case "B": Price2 = "Flip Top 80.00";
break;
case "C": Price1 = "Soft Pack 70.00";
break;
case "D": Price2 = "Flip Top 80.00";
break;
case "E": Price1 = "Soft Pack 70.00";
break;
case "F": Price2 = "Flip Top 80.00";
break;
case "G": Price1 = "Soft Pack 70.00";
break;
case "H": Price2 = "Flip Top 80.00";
break;
case "I": Price1 = "Soft Pack 70.00";
break;
case "J": Price2 = "Flip Top 80.00";
break;
default:
System.out.println("Invalid Input");
break;
}
System.out.println("Thank you! Your order is " + brand + Price);
}while (!password.equals(CORRECT_PWORD));
}while (!password.equals(CORRECT_PWORD) && attempts <3);
}
}
最佳答案
首先分解您的要求,首先您需要验证用户......
int attempts = 0;
Scanner keyboard = new Scanner(System.in);
String password = null;
String CORRECT_PWORD = "1234";
String Options = null;
do {
System.out.print("Enter Pin Code: ");
password = keyboard.next();
if (!password.equals(CORRECT_PWORD)) {
System.out.println("Password is incorrect!");
attempts++;
}
} while (!password.equals(CORRECT_PWORD) && attempts < 3);
他们要么输入正确的密码,要么失败。根据结果,您需要决定下一步该做什么...
if (password.equals(CORRECT_PWORD)) {
System.out.println("Welcome to Favor Stuff (Party Giveaways)!");
// Only valid user options should appear here...
} else {
System.out.println("Sorry but you can no longer continue!:");
}
// Program is allowed to terminate naturally
所以,在“有效用户”部分,您想显示主菜单...
if (password.equals(CORRECT_PWORD)) {
System.out.println("Welcome to Favor Stuff (Party Giveaways)!");
String option = null;
do {
System.out.println("Please select from the available options below! ");
System.out.println("A. Truffled Boxes");
System.out.println("B. Candy Jars");
System.out.println("C. Jellies");
System.out.println("D. Cupcakes");
System.out.println("E. Bride and Groom");
System.out.println("X. Exit");
option = keyboard.nextLine();
if (option.equals("A")) {
String subOption = null;
do {
System.out.println("1. Truffled Boxes with two (2) truffles and personalized thank you tags");
System.out.println("2. Truffled Boxes with four (4) truffles and personalized thank you tags");
System.out.println("3. Truffled Boxes with six (6) truffles and personalized thank you tags");
System.out.println("0. Return");
subOption = keyboard.nextLine();
if (subOption.equals("1")) {
} else if (subOption.equals("2")) {
} else if (subOption.equals("3")) {
} else if (subOption.equals("0")) {
// Do nothing, but don't want to display
// invalid entry message
} else {
System.out.println("Invalid entry!");
}
} while (!subOption.equalsIgnoreCase("0"));
} else if (option.equals("B")) {
// Candy jars...
} else if (option.equals("C")) {
// Jellies...
} else if (option.equals("D")) {
// Candy Cupcakes...
} else if (option.equals("E")) {
// Bride groom...
} else if (option.equalsIgnoreCase("x")) {
// Do nothing, but don't want to display
// invalid entry message
} else {
System.out.println("Invalid entry!");
}
} while (!option.equalsIgnoreCase("x"));
}
该主循环负责主菜单中的所有选项。每个子部分也将有它自己的输入循环。如果您对方法做了任何工作,我会将所有这些分解为它自己的方法,以使生活更轻松。
要跟踪订单,有许多可能的解决方案,例如,您可以创建一个 Product
类,其中包含有关每个产品的信息(名称、价格),然后创建一个 Order
类,其中包含有关已订购的 Product
和数量的信息。这可能是首选方式。但更简单、更黑客的方法是定义一系列键......
public static final String TRUFFLE_BOXES_2 = "Truffle Boxes with (2) truffles and personalized thank you tags";
public static final String TRUFFLE_BOXES_4 = "Truffle Boxes with (4) truffles and personalized thank you tags";
public static final String TRUFFLE_BOXES_6 = "Truffle Boxes with (6) truffles and personalized thank you tags";
public static final String CANDY_MARSHMELLOW = "Marshmallow";
public static final String CANDY_CHOCOLATE = "Chocolate";
public static final String CANDY_COOKIES = "Cookies";
public static final String JELLIES_JELLY_BEANS = "Jelly Beans";
public static final String CUP_CAKES_REGULAR = "Regular cupcake box";
public static final String CUP_CAKES_REGULAR_BULK = "Regular cupcake box by bulk";
public static final String BRIDE_AND_GROOM_BOX = "Bride and groom box (per pair)";
这些表示对 Map
执行查找的快速且简单的键
然后,您可以定义每个产品/ key 的价格
Map<String, Double> prices = new HashMap<>(10);
prices.put(TRUFFLE_BOXES_2, 45.00);
prices.put(TRUFFLE_BOXES_4, 50.00);
prices.put(TRUFFLE_BOXES_6, 60.00);
prices.put(CANDY_MARSHMELLOW, 35.00);
prices.put(CANDY_CHOCOLATE, 50.00);
prices.put(CANDY_COOKIES, 45.00);
prices.put(JELLIES_JELLY_BEANS, 45.00);
prices.put(CUP_CAKES_REGULAR, 60.00);
prices.put(CUP_CAKES_REGULAR_BULK, 40.00);
prices.put(BRIDE_AND_GROOM_BOX, 70.00);
然后您可以使用诸如...之类的方式跟踪订单
Map<String, Integer> order = new HashMap<>(25);
order.put(CANDY_CHOCOLATE, 10);
order.put(CUP_CAKES_REGULAR, 5);
order.put(CANDY_COOKIES, 12);
并在需要时提供统计
double total = 0;
for (Map.Entry<String, Integer> entry : order.entrySet()) {
int quanity = entry.getValue();
double price = prices.get(entry.getKey());
total += quanity * price;
System.out.println(String.format(
"%-20s x %4d @ %7s = %10s",
entry.getKey(),
quanity,
NumberFormat.getCurrencyInstance().format(price),
NumberFormat.getCurrencyInstance().format(quanity * price)));
}
System.out.printf("%-20s %4s %7s ==========%n", "", "", "");
System.out.printf("%-20s %4s %7s %10s%n", "", "", "", NumberFormat.getCurrencyInstance().format(total));
其中打印
Chocolate x 10 @ $50.00 = $500.00
Cookies x 12 @ $45.00 = $540.00
Regular cupcake box x 5 @ $60.00 = $300.00
==========
$1,340.00
I want to change the options into Cigar brands, and use switch so it would be better... but add the prices at the end just like what you did with the previous one.. I'm confused
我又要把你搞糊涂了...
基本上,这是您的整个(更新)计划、项目、价格、菜单、计数。
它利用商品、订单、价格和组的 map
来生成整个程序。
“数据”都是可变的,但是当您创建/添加/删除产品时,菜单和订购系统的工作方式不需要更改
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
public class TestShop {
public static final String MALBORO_LIGHTS = "Malboro Lights";
public static final String MALBORO_LIGHTS_SOFT = "Marlboro Lights, Soft Pack";
public static final String MALBORO_LIGHTS_FLIP = "Marlboro Lights, Flip Pack";
public static final String MALBORO_RED = "Malboro Reds";
public static final String MALBORO_RED_SOFT = "Marlboro Reds, Soft Pack";
public static final String MALBORO_RED_FLIP = "Marlboro Reds, Flip Pack";
public static final String DUNHILL = "Dunhill";
public static final String DUNHILL_SOFT = "Dunhill, Soft Pack";
public static final String DUNHILL_FLIP = "Dunhill, Flip Pack";
public static final String PHILLIP = "Phillip";
public static final String PHILLIP_SOFT = "Phillip, Soft Pack";
public static final String PHILLIP_FLIP = "Phillip, Flip Pack";
public static void main(String[] args) {
new TestShop();
}
public TestShop() {
Scanner keyboard = new Scanner(System.in);
/**************************************************************************/
/* This part is dynamic, change this when you want new items, prices or */
/* groups */
/**************************************************************************/
// A list of items that have been ordered
Map<String, Integer> orders = new HashMap<>(25);
// A list of items keyed to prices
Map<String, Double> prices = new HashMap<>(25);
prices.put(MALBORO_LIGHTS_SOFT, 70d);
prices.put(MALBORO_LIGHTS_FLIP, 80d);
prices.put(MALBORO_RED_SOFT, 70d);
prices.put(MALBORO_RED_FLIP, 80d);
prices.put(DUNHILL_SOFT, 60d);
prices.put(DUNHILL_FLIP, 70d);
prices.put(PHILLIP_SOFT, 70d);
prices.put(PHILLIP_FLIP, 80d);
// Mapping items to groups...
Map<String, List<String>> groups = new HashMap<>(25);
groups.put(MALBORO_LIGHTS, new ArrayList<>(Arrays.asList(new String[]{MALBORO_LIGHTS_SOFT, MALBORO_LIGHTS_FLIP})));
groups.put(MALBORO_RED, new ArrayList<>(Arrays.asList(new String[]{MALBORO_RED_SOFT, MALBORO_RED_FLIP})));
groups.put(DUNHILL, new ArrayList<>(Arrays.asList(new String[]{DUNHILL_SOFT, DUNHILL_FLIP})));
groups.put(PHILLIP, new ArrayList<>(Arrays.asList(new String[]{PHILLIP_SOFT, PHILLIP_FLIP})));
/**************************************************************************/
/**************************************************************************/
/* The rest of this is pretty static and is driven by the data from above */
/* This means, you don't need change anything below here, when the stuff */
/* changes */
/**************************************************************************/
boolean done = false;
do {
System.out.println("Welcome to my shop");
List<String> keys = new ArrayList<>(groups.keySet());
for (int index = 0; index < keys.size(); index++) {
System.out.println("[" + (index + 1) + "] " + keys.get(index));
}
System.out.println("[0] Exit");
String input = keyboard.nextLine();
try {
int selectedIndex = Integer.parseInt(input);
if (selectedIndex == 0) {
done = true;
} else if (selectedIndex > 0 && selectedIndex <= keys.size()) {
String key = keys.get(selectedIndex - 1);
List<String> items = groups.get(key);
boolean subDone = false;
do {
System.out.println("Items for " + key + "....");
for (int index = 0; index < items.size(); index++) {
System.out.println(" [" + (index + 1) + "] " + items.get(index));
}
System.out.println(" [0] Return");
input = keyboard.nextLine();
try {
int index = Integer.parseInt(input);
if (index > 0 && index <= items.size()) {
index--; // The items in the list are 0 indexed
String item = items.get(index);
Integer quanity = orders.get(item);
if (quanity == null) {
quanity = 1;
} else {
quanity++;
}
orders.put(item, quanity);
} else if (index == 0) {
subDone = true;
} else {
System.out.println("Invalid selection, please try again");
}
} catch (NumberFormatException exp) {
System.out.println("!! " + input + " is not a valid selection");
}
} while (!subDone);
} else {
System.out.println("Invalid selection, please try again");
}
} catch (NumberFormatException exp) {
System.out.println("!! " + input + " is not a valid selection");
}
} while (!done);
double total = 0;
for (Map.Entry<String, Integer> entry : orders.entrySet()) {
int quanity = entry.getValue();
double price = prices.get(entry.getKey());
total += quanity * price;
System.out.println(String.format(
"%-20s x %4d @ %7s = %10s",
entry.getKey(),
quanity,
NumberFormat.getCurrencyInstance().format(price),
NumberFormat.getCurrencyInstance().format(quanity * price)));
}
System.out.printf("%-20s %4s %7s ==========%n", "", "", "");
System.out.printf("%-20s %4s %7s %10s%n", "", "", "", NumberFormat.getCurrencyInstance().format(total));
}
}
关于java - 带密码和简单订单的菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33206299/
我正在努力实现以下目标, 假设我有字符串: ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ( z ) ( A ) ) ) ) ) 我想编写一个正则
给定: 1 2 3 4 5 6
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
大家好,我卡颂。 Svelte问世很久了,一直想写一篇好懂的原理分析文章,拖了这么久终于写了。 本文会围绕一张流程图和两个Demo讲解,正确的食用方式是用电脑打开本文,跟着流程图、Demo一
身份证为15位或者18位,15位的全为数字,18位的前17位为数字,最后一位为数字或者大写字母”X“。 与之匹配的正则表达式: ?
我们先来最简单的,网页的登录窗口; 不过开始之前,大家先下载jquery的插件 本人习惯用了vs2008来做网页了,先添加一个空白页 这是最简单的的做法。。。先在body里面插入 <
1、MySQL自带的压力测试工具 Mysqlslap mysqlslap是mysql自带的基准测试工具,该工具查询数据,语法简单,灵活容易使用.该工具可以模拟多个客户端同时并发的向服务器发出
前言 今天大姚给大家分享一款.NET开源(MIT License)、免费、简单、实用的数据库文档(字典)生成工具,该工具支持CHM、Word、Excel、PDF、Html、XML、Markdown等
Go语言语法类似于C语言,因此熟悉C语言及其派生语言( C++、 C#、Objective-C 等)的人都会迅速熟悉这门语言。 C语言的有些语法会让代码可读性降低甚至发生歧义。Go语言在C语言的
我正在使用快速将 mkv 转换为 mp4 ffmpeg 命令 ffmpeg -i test.mkv -vcodec copy -acodec copy new.mp4 但不适用于任何 mkv 文件,当
我想计算我的工作簿中的工作表数量,然后从总数中减去特定的工作表。我错过了什么?这给了我一个对象错误: wsCount = ThisWorkbook.Sheets.Count - ThisWorkboo
我有一个 perl 文件,用于查看文件夹中是否存在 ini。如果是,它会从中读取,如果不是,它会根据我为它制作的模板创建一个。 我在 ini 部分使用 Config::Simple。 我的问题是,如果
尝试让一个 ViewController 通过标准 Cocoa 通知与另一个 ViewController 进行通信。 编写了一个简单的测试用例。在我最初的 VC 中,我将以下内容添加到 viewDi
我正在绘制高程剖面图,显示沿路径的高程增益/损失,类似于下面的: Sample Elevation Profile with hand-placed labels http://img38.image
嗨,所以我需要做的是最终让 regStart 和 regPage 根据点击事件交替可见性,我不太担心编写 JavaScript 函数,但我根本无法让我的 regPage 首先隐藏。这是我的代码。请简单
我有一个非常简单的程序来测量一个函数花费了多少时间。 #include #include #include struct Foo { void addSample(uint64_t s)
我需要为 JavaScript 制作简单的 C# BitConverter。我做了一个简单的BitConverter class BitConverter{ constructor(){} GetBy
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我是 Simple.Data 的新手。但我很难找到如何进行“分组依据”。 我想要的是非常基本的。 表格看起来像: +________+ | cards | +________+ | id |
我现在正在开发一个 JS UDF,它看起来遵循编码。 通常情况下,由于循环计数为 2,Alert Msg 会出现两次。我想要的是即使循环计数为 3,Alert Msg 也只会出现一次。任何想法都
我是一名优秀的程序员,十分优秀!