英文:
Comparing Key items with an integer using Hasmap in Java
问题
public class Translate {
    
    Map<Integer, String> map = new HashMap<>();
    private int number;
    {
        map.put(0, "zero");
        map.put(1, "one");
        map.put(2, "two");
        map.put(3, "three");
        map.put(4, "four");
        map.put(5, "five");
        map.put(6, "six");
        map.put(7, "seven");
        map.put(8, "eight");
        map.put(9, "nine");
    }
    public Translate(int number) {
        this.number = number;
    }
    // This is where I'm stuck. I need to compare the Key item to see if it's equal to the integer that is in the Translate constructor.
    public String convertToString() {
        if (map.containsKey(number)) {
            return map.get(number);
        }
        return "";
    }
}
英文:
I'm trying to compare the Key item to see if is equal to the integer that is in the Translate constructor. But someone I can't really figure it out. Sorry for the mess and I' still fairly new to Java. Thanks
public class Translate {
    
        Map<Integer, String> map = new HashMap<>();
        private int number;
        map.put(0, "zero");
        map.put(1, "one");
        map.put(2, "two");
        map.put(3, "three");
        map.put(4, "four");
        map.put(5, "five");
        map.put(6, "six");
        map.put(7, "seven");
        map.put(8, "eight");
        map.put(9, "nine");
    
        public Translate(int number) {
            this.number = number;
        }
    
        //this is where I'm stuck. I need to compare the Key item to see if is it equal to the integer that is in the Translate constructor. 
        public String convertToString() {
            if (map.get(number) == this.number) {
            };
        }
    }
答案1
得分: 2
你可以尝试类似这样的写法:
    import java.util.*;
    
    public class Translate {
        
        private static Map<Integer, String> map;
        
        static {
            map = new HashMap<>();
            map.put(0, "zero");
            map.put(1, "one");
            map.put(2, "two");
            map.put(3, "three");
            map.put(4, "four");
            map.put(5, "five");
            map.put(6, "six");
            map.put(7, "seven");
            map.put(8, "eight");
            map.put(9, "nine");
        }
            
        private int number;
        
        public Translate(int number) {
            this.number = number;
        }
        
        public String convertToString() {
            if (map.containsKey(this.number)) {
                return map.get(this.number);
            }
            return null;
        }
        
        public static void main(String[] args) {
            Translate t = new Translate(7);
            System.out.println(t.convertToString());
        }
    }
英文:
You have to try something like this:
import java.util.*;
public class Translate {
    
    private static Map<Integer, String> map;
    
    static {
        map = new HashMap<>();
        map.put(0, "zero");
        map.put(1, "one");
        map.put(2, "two");
        map.put(3, "three");
        map.put(4, "four");
        map.put(5, "five");
        map.put(6, "six");
        map.put(7, "seven");
        map.put(8, "eight");
        map.put(9, "nine");
    }
        
    private int number;
    
    public Translate(int number) {
        this.number = number;
    }
    
    public String convertToString() {
        if (map.containsKey(this.number)) {
            return map.get(this.number);
        }
        return null;
    }
    
    public static void main(String[] args) {
        Translate t = new Translate(7);
        System.out.println(t.convertToString());
    }
}
答案2
得分: 1
我不是 Java 的专家,但以下是我发现有问题的部分:
- 
如果 (map.get(number).equals(this.number)) <--- 这里你正在获取值,而不是键。
而应该使用 map.contains() 进行检查,如果返回为 true,则返回你传入的键的值。
 - 
你永远不应该使用 "==" 来比较对象,而应该使用该集合的 .equals() 方法,如果有的话。
 
PS:如果我写错了什么内容,欢迎提供反馈。
英文:
I'm not the expert on java but here is what i found troublesome
if (map.get(number).equals(this.number)) <--- 1.you are fetching the value not the key in this.
Instead check with map.contains() , if returned true by it, return the value of the key you passed.
- You should never compare objects with "==" but should instead use .equals() method of that collection , if it is there.
 
PS: Open for feedback if I wrote something wrong
答案3
得分: 0
public String convertToString() {
  // 只需要判断映射中是否包含这个键,可以使用 containsKey 方法
  if (map.containsKey(this.number)) {
    // 进行其他你想要的操作
  }
}
或者,如果你要在映射中存在值的情况下使用该值,可以省略 containsKey 直接使用 get
public String convertToString() {
  // 返回映射中键为 this.number 的值,如果不存在则返回一个提示信息
  return map.getOrDefault(this.number, "找不到 " + this.number + "!");
}
英文:
public String convertToString() {
  // You just need to see if the map contains this key, so you can use containsKey
  if (map.containsKey(this.number)) {
    // Do whatever else you want to do
  }
}
Alternatively, if you will use the value in the map if it exists, you can skip the containsKey and just go straight to get
public String convertToString() {
  // Return either the value in the map with key of this.number, or a message that 
  // indicates that no value is present
  return map.getOrDefault(this.number, "Couldn't find " + this.number + "!");
}
答案4
得分: 0
你应该首先调用Translate方法并传入任何值。或者,你可以给数字赋予默认值,比如 int number = 5;
然后你可以使用以下代码块:
public String convertToString() {
    if (map.containsKey(this.number)) {
        return map.get(this.number);
    }
    return null;
}
接着,打印该值:System.out.println(t.convertToString());
英文:
You should call Translate method with any value at first. Or, you can give default value to number, such as int number =5;
Then you can use
public String convertToString() {
    if (map.containsKey(this.number)) {
        return map.get(this.number);
    }
    return null;
} method.
Then, print the value  System.out.println(t.convertToString());
答案5
得分: 0
请尝试以下代码:
public class Translate {
    private int number;
    Map<Integer, String> map = Map.of(0, "zero", 1, "one", 2, "two", 3, "three", 4, "four", 5, "five", 6, "six", 7, "seven", 8, "eight", 9, "nine");
    public Translate(int number) {
        this.number = number;
    }
    public String convertToString() {
        return map.get(number);  // 如果没有对应的条目,将返回null
    }
}
以上代码如果没有对应的条目,将返回null。或者,您也可以直接返回数字本身:
public String convertToString() {
    return map.getOrDefault(number, Integer.toString(number));
}
如果您想要严格一些,可以在构造函数中针对无效的数字抛出异常:
public Translate(int number) {
    if (!map.containsKey(number)) {
        throw new IllegalArgumentException("number must be 0-9");
    }
    this.number = number;
}
英文:
Try this:
public class Translate {
    private int number;
    Map<Integer, String> map = Map.of(0, "zero", 1, "one", 2, "two", 3, "three", 4, "four", 5, "five", 6, "six", 7, "seven", 8, "eight", 9, "nine");
    public Translate(int number) {
        this.number = number;
    }
    public String convertToString() {
        return map.get(number);  // returns null if there's no entry
    }
}
Theabove code rerurns null if there's no entry for the number. Alternatively, you could just return the number itself.
public String convertToString() {
    return map.getOrDefault(number, Integer.toString(number));
}
If you wanted to be strict, you could throw an exception if an invalid number is passed to the constructor:
public Translate(int number) {
    if (!map.containsKey(number)) {
        throw new IllegalArgumentException("number must be 0-9");
    }
    this.number = number;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论