简体   繁体   English

keyset 后打印出特定值

[英]Printing out a specific value after keyset

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

}

} }

I made this code to display the largest value with its String, however, instead of printing out a specific value, it prints out the entire value that is in the HashMap我使这段代码用它的字符串显示最大值,但是,它没有打印出特定值,而是打印出 HashMap 中的整个值

I suppose this is what you're trying to achieve?我想这就是你想要达到的目标?

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.");
                });
    }
}

However I will also leave some advice for future, when asking questions, be clear in what your goal is.但是,我也会为将来留下一些建议,在提问时,请明确您的目标是什么。 Also try to write cleaner code, For example, there are so many issues when trying to read your code.还要尝试编写更清晰的代码,例如,在尝试阅读代码时会出现很多问题。

  1. Variables names should not start with a capital letter unless you're declaring a constant.变量名称不应以大写字母开头,除非您要声明常量。
  2. int Number = sc.nextInt(); does not convey the intention of the variable.没有传达变量的意图。 int numberOfEmployees is much better name. int numberOfEmployees这个名字要好得多。
  3. for (String i: Car.keySet()) { } What is the use of putting this for loop if it's doing nothing? for (String i: Car.keySet()) { }如果什么都不做,把这个 for 循环有什么用?
  4. If you want to store things in a HashMap, then you can take inputs of Key and Value at the same time and put them in directly instead of using 3 loops just to fill up a HashMap.如果你想把东西存到一个HashMap中,那么你可以同时输入Key和Value直接放入,而不是用3个循环来填满一个HashMap。
  5. Try using Functional programming over imperative programming since it's much more readable and concise.尝试使用函数式编程而不是命令式编程,因为它更具可读性和简洁性。

What I understood from your question is that you want to display the Name of the Employee who sold the most cars at the end of the program.我从你的问题中了解到,你想显示在程序结束时售出最多汽车的员工的姓名。 If that's the case see the code below, I have replaced some of your code to get the job done, see comments in the code to find what part of the code is replaced:如果是这种情况,请参阅下面的代码,我已经替换了您的一些代码以完成工作,请参阅代码中的注释以查找替换了哪部分代码:

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.");

} }

Hope it Helps and please do consider reading this How do I ask a good question?希望它有所帮助,请考虑阅读这篇文章我如何提出一个好问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM