- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我在将字符串输入转换为字符并将字符与开关进行比较时遇到问题。我希望用户输入披萨大小的订单,然后输入最多 4 个看起来像“lop”(大橄榄菠萝)的配料,例如,我想将字符串拆分并比较为字符,这样我就可以将订单输出为“中等橄榄披萨,菠萝”(披萨上的菠萝,我知道异端邪说),并在最后给出总数。
对于此类任务,我是否走在正确的轨道上,或者使用数组、分割数组输入并比较字符串的每个字母而不是使用字符和 HashMap 会更容易吗?
到目前为止我的代码
import java.util.Scanner;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
public class Exercise_1{
public static void pizzaServiceA(String args[]){
HashMap <Character, String> Toppings = new Hashmap <Character, String>();
//pizza
Toppings.put('m', "meduim");
Toppings.put('l', "large");
//topping
Toppings.put('h', "ham");
Toppings.put('m', "mozzerella");
Toppings.put('o', "olives");
Toppings.put('p', "pineapple");
Toppings.put('s', "spinach");
Toppings.put('H', "ham");
Toppings.put('M', "mozzerella");
Toppings.put('O', "olives");
Toppings.put('P', "pineapple");
Toppings.put('S', "spinach");
HashMap <Character, Double> Prices = new Hashmap <Character, Double>();
//pizza price
Prices.put('m', 4.00);
Prices.put('l', 5.00);
//topping price medium
Prices.put('h', 1.40);
Prices.put('m', 1.00);
Prices.put('o', 0.80);
Prices.put('p', 1.00);
Prices.put('s', 1.20);
//topping price large
Prices.put('H', 2.10);
Prices.put('M', 1.50);
Prices.put('O', 1.20);
Prices.put('P', 1.50);
Prices.put('S', 1.20);
Scanner in = new Scanner(System.in);
System.out.println("Enter order: ");
String input = in.nextLine();
Character order = Character.valueOf(input);
Double totalPrice = 0.0;
switch (order){
case 'm':
totalPrice += 4.00;
System.out.println("Medium pizza ");
break;
case 'l':
totalPrice += 5.00;
System.out.println("Large pizza ");
break;
}
}
}
编辑:好的,这就是我现在想到的(仍然使用 HashMap ,但丢弃开关
System.out.println("Enter a pizza order: ");
Scanner reader = new Scanner(System.in);
String orders = reader.nextLine();
Char[] orderLetters = orders.toCharArray();
String fullOrder = "";
Double fullPrice = 0.0;
//check if sequence enters it more than 5 characters
if (orderLetters.equals("quit")) {
System.out.println("Quitting.");
System.exit(0);
}
else if (!(orderLetters[0].equals('l')))
{
System.out.println("Please enter the size of your pizza, m or l");
}
else if (!(orderLetters[0].equals('m')))
{
System.out.println("Please enter the size of your pizza, m or l");
}
for(Character orderLetters : c.toCharArray())
{
Double price = Prices.get(orderLetters);
fullPrice += price;
Character type = Toppings.get(orderLetters);
if(type == 'm' || type == 'l')
{
fullOrder += type + " pizza with ";
}
else
{
fullOrder += type + ",";
}
}
fullOrder += fullPrice;
System.out.printf("%.2f", "£", fullOrder);
}
最佳答案
您的代码存在一些问题:
<强>1。哈希
您正在使用从字符到字符串的 HashMap ,这是可以的,但每个字符只能有一个输出(您不能让“m”同时指向“medium”和“mozzerella”)。
这可以通过两种方式解决:
我个人认为第一种方式更好。尺寸和配料不应该在同一个变量上,它们没有以任何方式连接。
<强>2。您读取输入的方式首先,您可以使用 read()
代替 readLine()
。
其次,您需要将输入视为字符串而不是字符。然后使用 str.charAt(index)
从字符串中读取字符。
完成所有这些之后,您将在评估整个字符串(大小和配料)后得到价格。
希望这有帮助;)
关于java - 开关比较字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43453477/
我想要 angularJs 中的一个按钮,当我按下它时会调用一个函数,当我再次按下它时它会执行另一个功能,比如 ON-OFF 开关。 我有这个: 我会在单击一次时调用另一个函数。 最佳答案 您可
我有一个相当大的 switch 语句(或同样大的 if 语句),我想在 Doxygen 中记录一些方法,可能是调用者图,或者可能是某种流程图格式。 我怎样才能做到这一点? 谢谢 ! 最佳答案 通常 d
public int[] Level1Items(int floor) { switch (floor) { case 0: case 1:
我有一些R代码看起来基本上是这样的: compute.quantiles <- function(mu, type) { ## 'mu' and 'type' are vectors of the
嗨,我正在尝试处理 ajax json 响应 这是我的代码 success: function (j) { switch(true) { case (j.cho
我在尝试在 Javascript 中进行切换时遇到问题,当切换激活时,根据情况,程序将显示许多新按钮,您可以单击这些按钮并播放声音。 这是我的 HTML 和 Javascript,带有第一组按钮: f
static double rSetzen(){ double r; System.out.println("Sind sie männlich oder weiblich?");
作为我用 Haskell 编写的迷你解释器的一部分,我正在编写一个执行以下操作的函数:如果是 eval (App e1 e2) ,我想递归评估 e1 ( eval e1 ),将结果设置为 v1 .然后
对于C语言,我知道我很接近,但是对于输入的任何字符,打印输出都是“辅音”。我的 switch case 语句有什么问题。我需要 if 语句吗? #include #include int main
我有以下方法,它以类的类型作为参数: public void test(Type proType){ } 我目前有一个很大的 if else 看起来像: if(proType == typeof(Cl
我正在编写带有开关功能的代码。我希望我的默认代码打印“错误”并使程序停止运行。我应该在默认值末尾添加 return 1; 吗? 如果是这样,这样对吗? switch (course3) { c
VB.NET,但 C# 也可以。 我有一个 MustInherit 基类和 170 个基于它的继承类。为什么这么多?因为每个继承的类在 Sub New() 中做了不同的事情。继承的类型不添加任何新属性
在我的开关中,我希望案例从 0 变为 (number_of_cases-1),而无需自己编写数字。因此,如果我在中间删除一个 case block ,则以下 case 将重新编号(减 1),以便再次从
我想在我的应用中使用精确的 Material 开关。正是谷歌设计指南中的那些。 https://www.google.com/design/spec/components/selection-cont
有没有比我使用的更好的方法来实现切换/开关。它有效,但对我来说似乎很笨拙。 var foo = true; $(document).on("click","$element",function(){
我尝试将全局变量与 switch 语句一起使用,但变量的值没有改变。也许是范围问题? 我需要一个 switch 语句来逐一循环遍历每种情况,但每次我在控制台中查看时,“i”变量都会重置为 1。 为什么
我最近在一个网站上工作,在本地主机上一切正常,但是当我将它移到服务器上时,更改 div 函数开始加载怪异,我不知道为什么。当您循环浏览投资组合按钮和联系人按钮时,该错误会更加明显。 该网站仅针对 Fi
我一直在开发一个使用 switch() 函数的项目,如下所示: switch (selectedMenu) { case 1: switch (selectedIndex) {
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Variable declaration in c# switch statement 我一直想知道: 当我
我有一个奇怪的问题,我似乎无法解决。我有相当复杂的代码,但我已经简化了它,问题仍然存在。 请参阅以下内容: 'correct'); switch (true) { case empty($m
我是一名优秀的程序员,十分优秀!