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

}

}

我使這段代碼用它的字符串顯示最大值,但是,它沒有打印出特定值,而是打印出 HashMap 中的整個值

我想這就是你想要達到的目標?

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. 變量名稱不應以大寫字母開頭,除非您要聲明常量。
  2. int Number = sc.nextInt(); 沒有傳達變量的意圖。 int numberOfEmployees這個名字要好得多。
  3. for (String i: Car.keySet()) { }如果什么都不做,把這個 for 循環有什么用?
  4. 如果你想把東西存到一個HashMap中,那么你可以同時輸入Key和Value直接放入,而不是用3個循環來填滿一個HashMap。
  5. 嘗試使用函數式編程而不是命令式編程,因為它更具可讀性和簡潔性。

我從你的問題中了解到,你想顯示在程序結束時售出最多汽車的員工的姓名。 如果是這種情況,請參閱下面的代碼,我已經替換了您的一些代碼以完成工作,請參閱代碼中的注釋以查找替換了哪部分代碼:

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

}

希望它有所幫助,請考慮閱讀這篇文章我如何提出一個好問題?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM