gpt4 book ai didi

java - 如何创建一个计数器来计算输入最高薪水的人数?

转载 作者:行者123 更新时间:2023-11-29 05:24:41 25 4
gpt4 key购买 nike

import java.util.Scanner;

public class Lab4a{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int numemployees, max, freq;

System.out.print("Enter number of employees: ");
numemployees = keyboard.nextInt();

System.out.println("Enter salary for " + numemployees + " employees:");
int[] salaries = new int [numemployees];

max = salaries[0];
for (int i=0; i < salaries.length; i++){
salaries[i] = keyboard.nextInt();
if (salaries[i] > max)
max = salaries[i];
}

System.out.println("Maximum salary is " + max);
System.out.println(" employees received " + max);
}

所以这个的输出是

输入员 worker 数:3输入 3 名员工的工资:100001500015000最高工资15000 员工领取15000

我想说的是,“2 名员工收到了 15000”。这就是我需要做的。请帮助我如何使用另一个循环建立该计数器......尽可能简单!谢谢!

最佳答案

像这样修改你的循环:

max = salaries[0];
freq = 1;
for (int i=0; i < salaries.length; i++){
salaries[i] = keyboard.nextInt();
if (salaries[i] > max) {
freq = 1;
max = salaries[i];
} else if (salaries[i] == max) {
++freq;
}
}
System.out.println(freq + " employees received " + max);

关于java - 如何创建一个计数器来计算输入最高薪水的人数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23124910/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com