Java的语言,代码设计实现一个允许重复的字典,在我编程开发的过程里,总是有解决不了的错误,怎么办呢
你可以把你的错误啥的,还有相关信息拿出来
不知道你这个问题是否已经解决, 如果还没有解决的话:不废话直接出代码:
import java.io.*;
public class Mj {
public static void main(String[] args) throws IOException {
float n,m;
BufferedReader buf;
String str;
buf =new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入字符串1:");
str=buf.readLine();
n=Float.parseFloat(str);
System.out.print("请输入字符串2:");
str=buf.readLine();
m=Float.parseFloat(str);
System.out.println("n/m="+(n/m));
}
}
运行截图如下
由此可以做加减乘运算,都可以的,就是改一下最后的运行方式:
System.out.println("n/m="+(n/m));
就这个就行了。
根据问题描述,你想设计一个允许保存重复的键值对的字典。在Java语言中,可以通过使用HashMap
或LinkedHashMap
来实现这个需求。以下是具体的解决方案:
RepeatableDictionary
。import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RepeatableDictionary {
// 你可以在这里实现你的字典类
}
RepeatableDictionary
类中添加一个成员变量来存储键值对。这里使用的是HashMap
来存储数据,其中键可以重复。import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RepeatableDictionary {
private Map<String, List<String>> dictionary;
public RepeatableDictionary() {
dictionary = new HashMap<>();
}
// 其他方法的实现
}
addEntry
,该方法可以接收一个键和一个值。首先判断字典中是否已存在该键,如果存在,则将值添加到对应键的值列表中;如果不存在,则创建一个新的键值对。import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RepeatableDictionary {
private Map<String, List<String>> dictionary;
public RepeatableDictionary() {
dictionary = new HashMap<>();
}
public void addEntry(String key, String value) {
List<String> values = dictionary.getOrDefault(key, new ArrayList<>());
values.add(value);
dictionary.put(key, values);
}
// 其他方法的实现
}
getValues
,该方法接收一个键作为参数,并返回该键对应的值列表。import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RepeatableDictionary {
private Map<String, List<String>> dictionary;
public RepeatableDictionary() {
dictionary = new HashMap<>();
}
public void addEntry(String key, String value) {
List<String> values = dictionary.getOrDefault(key, new ArrayList<>());
values.add(value);
dictionary.put(key, values);
}
public List<String> getValues(String key) {
return dictionary.getOrDefault(key, new ArrayList<>());
}
// 其他方法的实现
}
removeEntry
,获取所有键的方法getKeys
等。import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class RepeatableDictionary {
private Map<String, List<String>> dictionary;
public RepeatableDictionary() {
dictionary = new HashMap<>();
}
public void addEntry(String key, String value) {
List<String> values = dictionary.getOrDefault(key, new ArrayList<>());
values.add(value);
dictionary.put(key, values);
}
public List<String> getValues(String key) {
return dictionary.getOrDefault(key, new ArrayList<>());
}
public void removeEntry(String key, String value) {
List<String> values = dictionary.get(key);
if (values != null) {
values.remove(value);
if (values.isEmpty()) {
dictionary.remove(key);
}
}
}
public List<String> getKeys() {
return new ArrayList<>(dictionary.keySet());
}
}
这样,你就可以使用RepeatableDictionary
类来实现一个允许保存重复的键值对的字典了。可以调用addEntry
方法来添加键值对,调用getValues
方法来获取键对应的值列表,调用removeEntry
方法来删除键值对,调用getKeys
方法来获取所有的键。
如果还有其他问题,或者对代码有疑问,请随时提问。