英文:
Cannot convert from Map.Entry<String,Integer> to String
问题
我尝试了2个小时来解决它,但我想在这里寻求一些帮助。任务如下:
创建一个名为
getHeaviest
的方法,不需要参数,返回一个字符串。当调用时,该方法应返回数据库中最重恐龙的名称。如何实现由你决定。如果数据库中没有恐龙,则返回一个空字符串。不需要考虑恐龙具有相同的体重。
import java.util.Map;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;
public class randomdamdam {
private Map<String, Integer> dinos;
public randomdamdam () {
dinos = new HashMap<>();
}
public int size(){
return dinos.size();
}
public void addDino(String newDino, int weight){
if (!dinos.containsKey(newDino)) {
dinos.put(newDino, weight);
System.out.println(newDino + "已添加。重量:" + weight + "kg");
} else {
System.out.println(newDino + "无法添加。已在数据库中!");
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + "已更新。新重量:" + newWeight + "kg");
} else {
String line = updatedDino + "无法更新。不在数据库中!";
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + "已移除");
} else {
String line2 = removedDino + "无法移除。不在数据库中!";
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + "在数据库中找不到!";
System.out.println(ofweight);
return 0;
}
}
public Set<String> getDinoNames(){
Set<String> names = dinos.keySet();
return names;
}
public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() == max ) {
String heavy = heaviestBoi.toString();
return heavy;
}
}
return ""; // 如果数据库为空
}
}
问题是,我想从每个恐龙中获取最重的恐龙,我尝试了多次,但实际上做不到。
英文:
I'm posting after 2 hours of trying to solve it but I'd like some help here. The task is the following:
> Create a method getHeaviest which takes no parameters and returns a string. When called, the method should return the name of the heaviest dinosaur in the database. How you achieve this is up to you. If no dinosaurs are in the database, return an empty string instead. Don't worry about dinosaurs having the same weight.
import java.util.Map;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;
import java.util.Map.Entry;
public class randomdamdam {
private Map<String, Integer> dinos;
public randomdamdam () {
dinos = new HashMap<>();
}
public int size(){
return dinos.size();
}
public void addDino(String newDino, int weight){
if (!dinos.containsKey(newDino)) {
dinos.put(newDino, weight);
System.out.println(newDino + " added. Weight: " + newDino + "kg");
} else {
System.out.println(newDino + " cannot be added. It is already in the database!");
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + "updated. Weight: " +newWeight+ "kg");
} else {
String line = updatedDino + "cannot be updated. It is not in the database!";
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + "removed");
} else {
String line2= removedDino +"cannot be removed. It is not in the database!";
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + "cannot be found in the database!";
System.out.println(ofweight);
return 0;
}
}
public Set<String> getDinoNames(){
Set<String> names = dinos.keySet();
return names;
}
public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() == max ) {
String heavy = heaviestBoi.toString();
return heaviestBoi;
}}
}
So the thing is that I'd like to get the heaviest dino out of every dino and I've tried multiple times to do that but I actually can't.
答案1
得分: 1
我认为你已经很接近了。看看你的getHeaviest
方法,在if语句内部,你实际上需要获取“Entry”对象的键元素(也就是恐龙的名称)。你不能简单地返回整个heaviestBoi
对象,因为它的类型是Entry
。
解决方案
public String getHeaviest() {
int max = Collections.max(dinos.values());
for (Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() == max) {
return heaviestBoi.getKey();
}
}
}
附加评论
请注意,你在if语句中写了以下内容:
String heavy = heaviestBoi.toString();
return heaviestBoi;
因此,第一行实际上对返回的对象没有影响。
英文:
I think you were already close. Looking at your getHeaviest
method, within the if statement, you have essentially to get the key element of the "Entry" object (which is the name of the dino). You cannot simply return the whole heaviestBoi
object as it is of type Entry
.
Solution
public String getHeaviest () {
int max = Collections.max(dinos.values());
for (Entry<String, Integer> heaviestBoi : dinos.entrySet())
{
if (heaviestBoi.getValue() == max ) {
return heaviestBoi.getKey();
}
}
}
Additional Comment
Please note that you wrote the following within your if statement:
String heavy = heaviestBoi.toString();
return heaviestBoi;
Hence, the first line has actually no effect in respect of the returned object.
答案2
得分: 1
将地图的键必须是恐龙的名称,值必须是它的键。因此,不要将amp强制转换为String(这会失败),而应该返回作为恐龙名称的键。
将以下内容替换:
String heavy = heaviestBoi.toString();
使用以下内容:
String heavy = heaviestBoi.getKey();
并返回该字符串,而不是地图对象。
英文:
Key of the map must be the name of the dinosaur and value must be its key. So instead of casting amp to String (which is failing) you must return the key which is name of the dinosaur
Replace
String heavy = heaviestBoi.toString();
with
String heavy = heaviestBoi.getKey();
and return that string instead of map object
答案3
得分: 1
请将getHeaviest()
方法重构如下:
public String getHeaviestDino() {
int maxWeight = -1;
String heaviestDinoName = null;
for (Map.Entry<String, Integer> entry : dinos.entrySet()) {
if (entry.getValue() > maxWeight) {
maxWeight = entry.getValue();
heaviestDinoName = entry.getKey();
}
}
return heaviestDinoName;
}
注意,方法名已经按照Java命名规范更改为getHeaviestDino
,首字母大写。
英文:
Please refactor the getHeaviest() method like below.Complete source code is given below for your better understandings.
Extra: Please follow the Java naming conventions when you are naming classes .Ex:Starting letter of a class name should be always Capital.
public class randomdamdam {
private Map<String, Integer> dinos;
public randomdamdam () {
dinos = new HashMap<>();
}
public int size(){
return dinos.size();
}
public void addDino(String newDino, int weight){
if (!dinos.containsKey(newDino)) {
dinos.put(newDino, weight);
System.out.println(newDino + " added. Weight: " + newDino + "kg");
} else {
System.out.println(newDino + " cannot be added. It is already in the database!");
}
}
public void updateDino (String updatedDino, int newWeight){
if (dinos.containsKey(updatedDino)){
System.out.println(updatedDino + "updated. Weight: " +newWeight+ "kg");
} else {
String line = updatedDino + "cannot be updated. It is not in the database!";
System.out.println(line);
}
}
public void removeDino (String removedDino) {
if (dinos.containsKey(removedDino)){
System.out.println(removedDino + "removed");
} else {
String line2= removedDino +"cannot be removed. It is not in the database!";
System.out.println(line2);
}
}
public int getWeight (String existingDinosaur) {
if (dinos.containsKey(existingDinosaur)){
return dinos.get(existingDinosaur);
} else {
String ofweight = existingDinosaur + "cannot be found in the database!";
System.out.println(ofweight);
return 0;
}
}
public Set<String> getDinoNames(){
Set<String> names = dinos.keySet();
return names;
}
public String getHeaviest () {//Here is your method which returns the heaviest dino's name
int max = -1;
String name= null;
for (Map.Entry<String, Integer> heaviestBoi : dinos.entrySet()) {
if (heaviestBoi.getValue() >max ) {
max = heaviestBoi.getValue();
name = heaviestBoi.getKey();
}}
return name;
}
public static void main(String[] args) {
randomdamdam m1 = new randomdamdam();
m1.addDino("Dino1", 450);
m1.addDino("Dino2",455);
m1.addDino("Dino3",700);
System.out.println("Heaviest Dino: "+ m1.getHeaviest() );//Calling the method
}
}
Output:
Dino1 added. Weight: Dino1kg
Dino2 added. Weight: Dino2kg
Dino3 added. Weight: Dino3kg
Heaviest Dino: Dino3 //Heaviest one's name returned
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论