java 在keyset后打印出特定值

omvjsjqw  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(143)
public static void main(String[] args) {
    HashMap<String, Integer>Car = new HashMap();
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter the number of employees worked with you this week");
    int Number = sc.nextInt();
    while (Number < 2 || Number > 5) {
        System.out.println("Enter a number between 2 and 5");
        Number = sc.nextInt();
    }
    System.out.println("Enter their names");
    String [] Name = new String [Number];
    sc.nextLine();
    for (int i=0; i<Number; i++) {
        int a = i+1;
        System.out.println("Enter name of employee " + a);
        Name[i] = sc.nextLine();
    }
    int[] Cars = new int[Number];
    for (int i=0; i<Number; i++) {
        String b = Name[i];
        System.out.println("Enter the number of cars sold by the employee " + b);
        Cars[i] = sc.nextInt();
        Car.put(Name[i], Cars[i]);
    }
    for (String i: Car.keySet()) {
        
    }
    ArrayList<Integer>CarOnly = new ArrayList<>(Car.values());
    Collections.sort(CarOnly, Collections.reverseOrder());
    for (int i: CarOnly) {
        for (String j: Car.keySet()) {
            if (i == Car.get(j))
                System.out.println(j);
        }
    }
    

}

}
我编写这段代码是为了显示最大值及其String,但是,它不是打印出特定值,而是打印出HashMap中的整个值

vlurs2pr

vlurs2pr1#

我猜这就是你想要达到的目的吧

public class DumbCode {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        final HashMap<String, Integer> hashMap = new HashMap<>();

        System.out.println("Enter the number of employees working with you this week:");
        int numberOfEmployees = scanner.nextInt();
        while(numberOfEmployees<2 || numberOfEmployees>5) {
            numberOfEmployees = scanner.nextInt();
        }

        int i=0;
        while(i++<numberOfEmployees) {
            System.out.println("Enter the name:");
            final String name = scanner.next();
            System.out.println("Enter the number of cars sold");
            final int carsSold = scanner.nextInt();
            hashMap.put(name, carsSold);
        }

        hashMap.entrySet().stream()
                .sorted((o1, o2) -> o1.getValue() > o2.getValue() ? 1 : 0)
                .limit(1)
                .forEach(o -> {
                    System.out.println(o.getKey() + " sold "+ o.getValue()+" cars.");
                });
    }
}

不过我也会给以后留下一些建议,当问问题的时候,要清楚你的目标是什么。还要试着写更干净的代码,比如,当试着读你的代码的时候有很多问题。
1.变量名不应该以大写字母开头,除非你声明一个常量。

  1. int Number = sc.nextInt();不能表达变量的意图。int numberOfEmployees是更好的名称。
  2. for (String i: Car.keySet()) { }如果这个for循环什么都不做,那么它有什么用呢?
    1.如果你想在HashMap中存储东西,那么你可以同时获取Key和Value的输入,然后直接把它们放进去,而不是使用3个循环来填充HashMap。
    1.尝试使用函数式编程而不是命令式编程,因为它可读性更强、更简洁。
gxwragnw

gxwragnw2#

我从你的问题中理解到你想在程序结束时显示卖出最多汽车的雇员的名字。如果是这样的话,请看下面的代码,我已经替换了你的一些代码来完成这项工作,请看代码中的注解来找到代码的哪一部分被替换了:

public static void main(String[] args) {
HashMap<String, Integer>Car = new HashMap();
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of employees worked with you this week");
int Number = sc.nextInt();
while (Number < 2 || Number > 5) {
    System.out.println("Enter a number between 2 and 5");
    Number = sc.nextInt();
}
System.out.println("Enter their names");
String [] Name = new String [Number];
sc.nextLine();
for (int i=0; i<Number; i++) {
    int a = i+1;
    System.out.println("Enter name of employee " + a);
    Name[i] = sc.nextLine();
}
int[] Cars = new int[Number];
for (int i=0; i<Number; i++) {
    String b = Name[i];
    System.out.println("Enter the number of cars sold by the employee " + b);
    Cars[i] = sc.nextInt();
    Car.put(Name[i], Cars[i]);
}
for (String i: Car.keySet()) {
    
}
ArrayList<Integer>CarOnly = new ArrayList<>(Car.values());
Collections.sort(CarOnly, Collections.reverseOrder());

// i have replaced the code that is using nested loops with the code below
int maxCars = 0;
String maxEmployee = "";
for (String name : Car.keySet()) {
    int numCars = Car.get(name);
    if (numCars > maxCars) {
        maxCars = numCars;
        maxEmployee = name;
    }
}
System.out.println("The employee who sold the most cars is " + maxEmployee + " with " + maxCars + " cars sold.");

}
希望它有帮助,请考虑阅读这篇文章

相关问题