直接與這一點。我只需要一些關於我編寫的程序是否實際上能夠正確使用Hashmaps以及試圖完成一般任務的方法的一般指導(第一次嘗試編譯它,它會在第90行中拋出有關else語句的錯誤,認爲我的括號是搞砸了)比薩訂單系統哈希曲線和一般語法
第一個功能的目的是讓用戶在一行中輸入最多5個字符的順序(沒有寫任何東西來檢查這一點),第一個字符必須是M或L適合中型或大型披薩。然後接着0到4個字符的澆頭。
第二個功能用途與第一個功能相同,只有它可以允許3個或更多相同的澆頭。
public class Exercise_1{
public static void pizzaServiceA(String args[]){
HashMap <Character, String> Toppings = new Hashmap <Character, String>();
//pizza
dictionary.put("m", "meduim");
dictionary.put("l", "large");
//topping
dictionary.put("h", "ham");
dictionary.put("m", "mozzerella");
dictionary.put("o", "olives");
dictionary.put("p", "pineapple");
dictionary.put("s", "spinach");
dictionary.put("H", "ham");
dictionary.put("M", "mozzerella");
dictionary.put("O", "olives");
dictionary.put("P", "pineapple");
dictionary.put("S", "spinach");
HashMap <Character, Double> Prices = new Hashmap <Character, Double>();
//pizza price
dictionary.put("m", 4.00);
dictionary.put("l", 5.00);
//topping price medium
dictionary.put("h", 1.40);
dictionary.put("m", 1.00);
dictionary.put("o", 0.80);
dictionary.put("p", 1.00);
dictionary.put("s", 1.20);
//topping price large
dictionary.put("H", 2.10);
dictionary.put("M", 1.50);
dictionary.put("O", 1.20);
dictionary.put("P", 1.50);
dictionary.put("S", 1.20);
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 (input.equals("quit")) {
System.out.println("Quitting.");
System.exit(0);
}
else if (!(order[0].equals('l')))
{
System.out.println("Please enter the size of your pizza, m or l");
}
else if (!(order[0].equals('m')))
{
System.out.println("Please enter the size of your pizza, m or l");
}
for(Char orderLetters : c.toCharArray())
{
Double price = Prices.get(orderLetters);
fullPrice += price;
String type = Toppings.get(orderLetters);
if(type == 'm' || type == 'l')
{
fullOrder += type + " pizza with ";
}
else
{
fullOrder += type + ",";
}
}
fullOrder += fullPrice;
System.out.printf("%.2f", "£", fullOrder);
}
public static void pizzaServiceB(){
Map<Character, Integer> map = new Hashmap<Character, Integer>();
for(int i = 0; i <s.length(); i++){
char orderLetters = c.charAt(i); //s.charAt?
if (map.containsKey(orderLetters)){
int c = map.get(orderLetters); //counts letters in orderletters
map.put(orderLetters, ++c);
{
else
{
map.put(orderLetters, 1);
}
}
}
}
if (c.equals() = 3){
System.out.println("You cannot order "); //if topping occurs 3 times print
}
//same functionality of A but orders with more than 3 toppings shoudlnt be allowed
}
public static void main(){
Exercise_1 ex1 = null;
ex1.testpizzaServiceA();
//ex1.testpizzaServiceB();
}
}
什麼是'字典'? – px06
另外,它似乎正在嘗試在'期望'字符時將'String'傳遞給'put()'。我會仔細檢查你的'HashMap'對象的創建。 – Logan
你我完全忽略了這一點。我之前使用字典而不是hashmaps,並忘記改變我如何添加字符串 – blockoblock