在选择并执行案例后切换案例重复

时间:2014-12-25 12:21:15

标签: java

我是编程新手,我尝试使用Switch case来实现HashMaps,用户选择一个选项并添加或删除键。如果用户选择插入键值对的选项1,程序在此之后结束,我不能选择允许用户显示键集等的其他选项。我想编程以在成功案例之后继续执行执行。抱歉我的英语不好。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashMap;


public class HashMapImplementation {
    public static void main(String args[]) throws Exception {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));


        System.out.println("Please select what you want to do:");
        System.out.println("1. Add Key-Value to HashMap ");
        System.out.println("2. Delete Key-Value from HashMap ");
        System.out.println("3. Display all Keys");
        System.out.println("4. Display all Values");
        System.out.println("5. Display Key-Value Set");
        System.out.println("6. Find a Value in HashMap");
        int c = Integer.parseInt(reader.readLine());
        switch (c) {
            case 1:
                System.out.println("Please enter the Key:");
                String key = reader.readLine();
                System.out.println("Please enter the value:");
                String value = reader.readLine();

                if (hashMap.containsKey(key)) {
                    System.out.println("The key you entered already exists in the HashMap. Would you like to overwrite it?(Y/N)");
                    String overwrite = reader.readLine();
                    if (overwrite == "y") {
                        hashMap.put(key, value);
                        System.out.println("Key: " + key + " and Value: " + value + " Inserted successfully!");
                        break;
                    } else {
                        System.out.println("No value entered");
                        break;
                    }

                }

                hashMap.put(key, value);
                System.out.println("Key: " + key + " and Value: " + value + " Inserted successfully!");
                break;


            case 2:
                System.out.println("Please enter the Key you want to remove from HashMap:");
                key = reader.readLine();
                if (hashMap.containsKey(key)) {
                    hashMap.remove(key);
                    System.out.println("Key: " + key + " was successfully removed from HashMap");
                    break;
                } else {
                    System.out.println("The key you entered doesnt exist in HashMap");
                    break;
                }

            case 3:
                System.out.println();
                hashMap.keySet();
                break;

            case 4:
                System.out.println();
                hashMap.values();
                break;

            case 5:
                System.out.println();
                hashMap.toString();
                break;

            case 6:
                System.out.println("Please enter the key for the value you want to look up in the HashMap");
                key = reader.readLine();
                if (hashMap.containsKey(key)) {
                    hashMap.remove(key);
                    System.out.println("Key: " + key + " was successfully removed from HashMap");
                    break;
                } else {
                    System.out.println("The key you entered doesnt exist in HashMap");
                    break;
                }

            default:
                System.out.println("Please enter numbers between 1 and 6 from the list of options!");

        }
    }
}

1 个答案:

答案 0 :(得分:5)

您应该使用while循环包围switch语句。

while (someCondition) {
    System.out.println("Please select what you want to do:");
    System.out.println("1. Add Key-Value to HashMap ");
    System.out.println("2. Delete Key-Value from HashMap ");
    System.out.println("3. Display all Keys");
    System.out.println("4. Display all Values");
    System.out.println("5. Display Key-Value Set");
    System.out.println("6. Find a Value in HashMap");
    int c = Integer.parseInt(reader.readLine());
    switch (c) {
       ....
    }
}
相关问题