有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从映射转换。条目<String,Integer>到字符串

我在尝试解决这个问题两个小时后发了帖子,但我想在这里得到一些帮助。任务如下:

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;
        }}
    }

所以问题是,我想从每只恐龙中选出最重的恐龙,我已经尝试过多次,但实际上我做不到


共 (3) 个答案

  1. # 1 楼答案

    地图的键必须是恐龙的名字,值必须是它的键。所以,你必须返回恐龙的名字,而不是将amp转换为字符串(这是失败的)

    替换

    String heavy = heaviestBoi.toString();
    

    String heavy = heaviestBoi.getKey();
    

    并返回该字符串,而不是映射对象

  2. # 2 楼答案

    请重构GetHeavest()方法,如下所示。下面给出了完整的源代码,以便您更好地理解

    额外:命名类时请遵循Java命名约定。类名的起始字母应该总是大写

    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 
        }
    }
    

    输出:

    Dino1 added. Weight: Dino1kg
    Dino2 added. Weight: Dino2kg
    Dino3 added. Weight: Dino3kg
    Heaviest Dino: Dino3 //Heaviest one's name returned
    
    
  3. # 3 楼答案

    我想你已经很接近了。查看您的getHeaviest方法,在if语句中,您基本上必须获得“Entry”对象(即dino的名称)的关键元素。不能简单地返回整个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;
    

    因此,第一行实际上对返回的对象没有影响