Java 选择并执行案例后,切换案例重复
我是编程新手,我尝试使用Switch case实现HashMaps,其中用户选择一个选项并添加或删除键。如果用户选择选项1(插入键值对),程序在此之后结束,我无法选择其他允许用户显示键值集的选项等。我希望程序在成功执行案例后继续执行。对不起,我的英语不好Java 选择并执行案例后,切换案例重复,java,Java,我是编程新手,我尝试使用Switch case实现HashMaps,其中用户选择一个选项并添加或删除键。如果用户选择选项1(插入键值对),程序在此之后结束,我无法选择其他允许用户显示键值集的选项等。我希望程序在成功执行案例后继续执行。对不起,我的英语不好 import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashMap; public class HashMapImplement
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!");
}
}
}
您应该在switch语句周围加一个while循环
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) {
....
}
}
您应该在switch语句周围加一个while循环
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) {
....
}
}
完美的谢谢:太好了!谢谢您: