java - 尝试在其他方法中显示 HashMap 键集的列表

标签 java class hashmap

所以,我想从主类调用 HashMap 键集列表并将它们列在控制台中。我试图在每次打印之前显示按键集:

public static void main(String[] args) {
     Scanner input = new Scanner(System.in);
     // The keyset can be set here to show the alternatives to convert to the user
     System.out.println("What length you want to confert from");
     String to = input.nextLine();

     System.out.println("What length you want to confert to");
     String from = input.nextLine();

     System.out.println("Input length");
     double value = input.nextDouble();

     int result = (int)Length.convert(value, from, to);
     System.out.println((int )value +  from + " = " +  result + to);
}
** 这是Length中用于转换长度的第二个方法: **

public static double convert(double value, String from, String to){
     HashMap<String, Double> table= new HashMap<>();
     table.put("mm", 0.001);
     table.put("cm", 0.01);
     table.put("dm", 0.1);
     table.put("m", 1.0);
     table.put("hm", 100.0);
     table.put("km", 1000.0);
     table.put("ft", 0.3034);
     table.put("yd", 0.9144);
     table.put("mi", 1609.34);

     double from_value = table.get(from);
     double to_value = table.get(to);
     double result = from_value / to_value * value;

     return result;  
}

最佳答案

修复Length类:

class Length {

    //Declare the map as class variable
    static Map<String, Double> table = new HashMap<>();

    //Initialize the map
    static {
        table.put("mm", 0.001);
        table.put("cm", 0.01);
        table.put("dm", 0.1);
        table.put("m", 1.0);
        table.put("hm", 100.0);
        table.put("km", 1000.0);
        table.put("ft", 0.3034);
        table.put("yd", 0.9144);
        table.put("mi", 1609.34);
    }

    public static double convert(double value, String from, String to) {

        double from_value = table.get(from);
        double to_value = table.get(to);
        double result = from_value / to_value * value;

        return result;
    }

    //Print the KeySet
    public static void printMap() {
        System.out.println(table.keySet());
    }
}

更新main方法:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    //Show Keyset
    Length.printMap();


    System.out.println("What length you want to confert from");
    String to = input.nextLine();

    System.out.println("What length you want to confert to");
    String from = input.nextLine();

    System.out.println("Input length");
    double value = input.nextDouble();

    int result = (int) Length.convert(value, from, to);
    System.out.println((int) value + from + " = " + result + to);
}

关于java - 尝试在其他方法中显示 HashMap 键集的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35081778/

相关文章:

java - 哈希时不可避免的碰撞?

java - 带图片和文字的自定义按钮?

java - 使 adjustmentQuantity 方法在 0 处停止

C++与类中的成员相同的类

ruby - 如何在 Ruby 模块上调用 #new 来实例化其类之一?

java - 如果我有ArrayList,如何比较两个HashMap?

java - StringBuilder在多线程环境下失败的实际原因是什么

java - 如何重置 switch 语句中的值?

java - 在java中我可以在一个文件中有多个类/对象吗?

javascript - 需要帮助查找整数数组中的逻辑缺陷以查找 K 个不同对的对