gpt4 book ai didi

java - 开关比较字符

转载 作者:太空宇宙 更新时间:2023-11-04 11:34:21 25 4
gpt4 key购买 nike

我在将字符串输入转换为字符并将字符与开关进行比较时遇到问题。我希望用户输入披萨大小的订单,然后输入最多 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”)。

这可以通过两种方式解决:

  1. 使用2个map(不一定是hashmap,也可以使用treemap)。一种用于尺寸,一种用于配料。
  2. 改变你看待角色的方式。让“m”仅表示一件事,并且对每个字母都这样做。任何字母都不能有多个含义。

我个人认为第一种方式更好。尺寸和配料不应该在同一个变量上,它们没有以任何方式连接。

<强>2。您读取输入的方式首先,您可以使用 read() 代替 readLine()
其次,您需要将输入视为字符串而不是字符。然后使用 str.charAt(index) 从字符串中读取字符。

<小时/>

我解决问题的一般流程

  1. 读取用户输入的字符串
  2. 使用简单的开关确定披萨的大小(这是一项非常有限的检查,因此让我们保持简单。
  3. 迭代字符串中的所有剩余字母,并为每个字母使用配料哈希来添加价格

完成所有这些之后,您将在评估整个字符串(大小和配料)后得到价格。

希望这有帮助;)

关于java - 开关比较字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43453477/

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