2016-12-24 12 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

'平均()'一度、これだけ1 V 'sum 'の値、すなわち最後に割り当てられた値が考慮されるので、' min = max = sum'となる。 – Andreas

+0

どうやって呼び出すことができますか? –

答えて

0

getEmployeeDetails()方法のループでは、あなたが保存して分の賃金、最大賃金及びこれらの賃金を持っている従業員の名前を更新しません。
さらに、名前の具体的な内容を記述する必要があります。例えば、maxWageおよびminWageは、maxおよびminより意味があります。
Wageクラスのフィールド宣言には、maxWage,maxWageEmployeeName,minWageおよびminWageEmployeeNameのフィールドがあるはずです。システムに挿入し、各従業員で

、専用のメソッドを呼び出すことによって、これらの値を更新する必要があります。他に

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()方法は、結果だけを表示してはなりませんあなたは賃金MIN、MAXについての情報が更新されている必要があり、誰が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

平均値は –

+0

であり、 '0 'と' updateRanking() 'メソッドで初期化されたクラスの' double totalWage'フィールドを追加して、このactualWageにインクリメントしてください。 'average()'メソッドでは、 'totalWage'を挿入された従業員の数で除算して賃金平均を求めます – davidxxx

関連する問題