これと一直線になっています。私が書いたプログラムが実際にハッシュマップで正しく動作するかどうか、一般的なタスクを完了しようとする私のアプローチについての一般的なガイダンスが必要です。(コンパイルしようとしましたが、else文に関して90行目でエラーがスローされ、ピザオーダーシステムハッシュマップと一般的な構文
最初の機能の目的は、ユーザーが1行に最大5文字のオーダーを入力することです(これをチェックするためには何も書きません)。最初の文字はMまたはLでなければなりません中型または大型ピザ用。それに続いて、トッピングのための0〜4文字が続きます。
第2の機能の目的は、第1の機能の目的と同じですが、それだけで3つ以上の同じトッピングが可能です。あなたは同じキー最後に追加された値の上書き内容で2つの値を入れしようとしているとき
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
私はこれを完全に見落としました。以前はハッシュマップの代わりに辞書を使用していましたが、文字列を追加する方法を変更するのを忘れました – blockoblock