2016-12-24 82 views
-6

我想要显示薪水的最大值和最小值,但是我得到的最大值和最小值都是相同的值。这里是我的代码:我想在java中找到最大值和最小值

import java.util.*; 

class Wage { 

String employee_name, skill; 
int hours; 
double sum; 
String[] sno = {"1", "2", "3", "4"}; 
double max = Double.MIN_VALUE; 
double min = Double.MAX_VALUE; 
Scanner s = new Scanner(System.in); 

public void getEmployeeDetails() { 
    System.out.println("Welcome to Use General Wage Record System"); 
    for (String count : sno) { 

     if (count.equalsIgnoreCase("1")) { 
      System.out.print("Enter Name of Employee 1: "); 
      employee_name = s.nextLine(); 
      System.out.print("Enter the Skill Level (1,2,3) of Employee:"); 
      Integer level_count = Integer.valueOf(s.nextLine()); 
      if (level_count <= 3) { 
       System.out.print("Enter the Worked Hours for " + employee_name + ":"); 
       hours = Integer.parseInt(s.nextLine()); 
       if (level_count == 1) { 
        sum = hours * 15; 
       } 
       if (level_count == 2) { 
        sum = hours * 17; 
       } 
       if (level_count == 3) { 
        sum = hours * 21; 
       } 

       System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " " + "$" + sum); 
      } 

     } 

     if (count.equalsIgnoreCase("2")) { 
      System.out.print("Enter Name of Employee 2:"); 
      employee_name = s.nextLine(); 

      System.out.print("Enter the Skill Level (1,2,3) of Employee:"); 

       Integer level_count = Integer.valueOf(s.nextLine()); 
      if (level_count <= 3) { 
       System.out.print("Enter the Worked Hours for " + employee_name + ":"); 
       hours = Integer.parseInt(s.nextLine()); 
       if (level_count == 1) { 
        sum = hours * 15; 
       } 
       if (level_count == 2) { 
        sum = hours * 17; 
       } 
       if (level_count == 3) { 
        sum = hours * 21; 
       } 
       System.out.println("The wage of employee " + employee_name + "(Level " + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " " + "$" + sum); 
      } 

     } 

     if (count.equalsIgnoreCase("3")) { 
      System.out.print("Enter Name of Employee 3:"); 
      employee_name = s.nextLine(); 
      System.out.print("Enter the Skill Level (1,2,3) of Employee:"); 
      Integer level_count = Integer.valueOf(s.nextLine()); 
      if (level_count <= 3) { 
       System.out.print("Enter the Worked Hours for " + employee_name + ":"); 
       hours = Integer.parseInt(s.nextLine()); 
       if (level_count == 1) { 
        sum = hours * 15; 
       } 
       if (level_count == 2) { 
        sum = hours * 17; 
       } 
       if (level_count == 3) { 
        sum = hours * 21; 
       } 

       System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + " $" + sum); 
      } 

     } 

     if (count.equalsIgnoreCase("4")) { 
      System.out.print("Enter Name of Employee 4:"); 
      employee_name = s.nextLine(); 
      System.out.print("Enter the Skill Level (1,2,3) of Employee:"); 

       Integer level_count = Integer.valueOf(s.nextLine()); 
      if (level_count <= 3) { 
       System.out.print("Enter the Worked Hours for " + employee_name + ":"); 
       hours = Integer.parseInt(s.nextLine()); 
       if (level_count == 1) { 
        sum = hours * 15; 
       } 
       if (level_count == 2) { 
        sum = hours * 17; 
       } 
       if (level_count == 3) { 
        sum = hours * 21; 
       } 
       System.out.println("The wage of employee" + employee_name + "(Level" + String.valueOf(level_count) + ")" + "for" + hours + " " + "hours is" + "$ " + sum); 
      } 

     } 
    } 

} 

void average() { 
    System.out.println("\n\n"); 
    System.out.println("stastical information for bar chart"); 
    System.out.println("=================================="); 

    if (sum > max) { 

     max = sum; 

     System.out.println("Maximum of wage" + max + ", " + employee_name); 
    } 
    if (sum < min) { 
     min= sum ; 
     System.out.println("Minimum of Wage" + min + ", " + employee_name); 
    } 
} 

} 

public class Pay { 

/** 
* @param args the command line arguments 
*/ 
    public static void main(String[] args) { 

     Wage wm = new Wage(); 
    wm.getEmployeeDetails(); 
    wm.average(); 
} 
+0

你只能叫'平均()'一次,所以只有一个V-考虑'sum'的值,即最后分配的值,所以'min = max = sum'。 – Andreas

+0

如何打电话请用实例详细说明 –

回答

0

getEmployeeDetails()方法的循环,你没有保存和更新分钟工资,最大工资,有工资,这些员工姓名。
此外,你应该在命名中更具体。例如,maxWageminWagemaxmin更有意义。
因此,在Wage类的字段声明中应该有maxWage,maxWageEmployeeName,minWageminWageEmployeeName字段。

在系统中插入的每个员工,你应该通过调用专用方法更新这些值:

public void getEmployeeDetails() { 

    if (count.equalsIgnoreCase("1")) { 
    .... 
    updateRanking(sum, employee_name); 
    } 
} 

public void updateRanking(double actualWage, String employee_name){ 
    if (actualWage > maxWage) { 
     maxWage = actualWage; 
     maxWageEmployeeName = employee_name; 
    } 
    else if (actualWage < minWage) { 
     minWage = actualWage; 
     minWageEmployeeName = employee_name; 
    } 
} 

此外,您average()方法叫你的程序到底应该只显示结果,而不是进行比较,因为你应该有更新的信息有关的最大值,最小值工资,谁拥有这些作为updateRanking()在员工工资的每次插入称为:

void average() { 
    System.out.println("\n\n"); 
    System.out.println("stastical information for bar chart"); 
    System.out.println("==================================");   
    System.out.println("Maximum of wage" + maxWage + ", " + maxWageEmployeeName);  
    System.out.println("Minimum of Wage" + minWage + ", " + minWageEmployeeName);  
} 
+0

whaat平均值 –

+0

在初始化为'0'的类中添加'double totalWage'字段,并在'updateRanking()'方法中增加此实际工资。然后在“平均()”方法中,通过将“总工资”除以所插入的员工数量即可得到工资平均值 – davidxxx

相关问题