2016-11-29 18 views
-5

我是編碼方面的新手。我試圖在jGrasp中編寫一個工資單寄存器,但我不斷收到這些錯誤:「無法找到符號」和「變量已在方法中定義」。它看起來像我已經定義了其中一個變量,但我無法弄清楚要改變它以使其正常工作。我用下面的錯誤標記了斑點。預先感謝任何幫助!獲取錯誤「找不到符號」和「變量已在方法中定義」

//Lab4 Payroll Register Refinements 

//this makes available all extra utilities from Java library including scanner 
import java.util.*; 
import java.io.*; //needed for files 



public class Lab4 // class name MUST be name of .java file 
{//start of class 
    public static void main(String [] args) throws FileNotFoundException 

    {// start of main method 
     Scanner keyIn = new Scanner(System.in); 
     // assigns "keyIn" to keyboard 

     Scanner inPayrollFile = new Scanner(new FileReader("Lab4Input1.txt")); 
     // assigns "inPayrollFile" to input file 

     PrintWriter outPayrollFile = new PrintWriter("Lab4Output1.txt"); 
     // assigns "outPayrollFile" to output file 

     //System.out displays on the monitor 


    //Variables and defined constants go here 

     final String HEADING1 = "ID#  HOURS  RATE GROSS  DEDUCT  NET"; //example of a defined constant 

     String employeeName;  //Employee's name is entered 
     int empId;    //Employee's Id number is entered 
     int empDeptCode;   //Employee's department code is entered 
     double hoursWorked;  //Amount of hours worked is entered 
     double hourlyPayRate;  //Employee's hourly pay rate is entered 
     double empOvertimePay; //Employee's overtime rate 
     double empRegPay;   //Employee's regular rate of pay 
     double empBonus;   //Employee's bonus pay 
     double grossPay;   //Employee's gross pay 
     double deduction;   //Amount deducted from gross pay 
     double netPay;   //Employee net pay 
     double totalGrossPay = 0; //Employee's total gross pay 
     double totalNetPay = 0; //Employee's total net pay 
     double totalDeduction = 0;//Total amount deducted 
     int totalEmployees = 0; //Total number of employees 
///////////////////////////////////////////////////////////////////////////  
    //instructions start here 

    System.out.println ("Please enter Employee name: "); 
    employeeName = keyIn.nextLine(); 

     //before loop instructions go here 

      outPayrollFile.println (HEADING1); 
     //example of Printing report heading to the output file 


/////////////////////////////////////////////////////////////////////////// 
    //loop, which has input, process, output 
      while(inPayrollFile.hasNext()) 
     {//start while loop - hasNext is TRUE when there is an input record 
     //     hasNext is FALSE when no more input records - the loop ends 

      //input - within loop 

      empId = inPayrollFile.nextInt(); //example of reading from input file 
     hoursWorked = inPayrollFile.nextDouble (); 
     hourlyPayRate = inPayrollFile.nextDouble (); 
     empDeptCode = inPayrollFile.nextInt(); 
     System.out.println ("Processing empId" + empId); 


      //process - within loop 

     empRegPay = getRegPay(hoursWorked, hourlyPayRate); 
     empOvertimePay = getEmpOvertimePay(hoursWorked, hourlyPayRate); 
     grossPay = empRegPay + empOvertimePay; 
     empBonus = getBonus(grossPay, hoursWorked, empDeptCode); 
     grossPay = grossPay + empBonus; 
     deduction = getDeduction(grossPay); 
     netPay = grossPay - deduction; 



     if (grossPay > 1000) 
     { 
      deduction = grossPay * 0.30; 
     } 
     else 
     { deduction = grossPay * 0.25; 
     } //endif 

     netPay = (grossPay - deduction); 
     totalGrossPay = (totalGrossPay + grossPay); 
     totalDeduction = (totalDeduction + deduction); 
     totalNetPay = (totalNetPay + netPay); 
     totalEmployees = (totalEmployees + 1); 


     if (grossPay > 1000) 
     { 
      deduction = grossPay * 0.30; 
     } 
     else 
     { deduction = grossPay * 0.25; 
     } //endif 
      //output - within loop 

     outPayrollFile.printf ("%4d", empId); 
     outPayrollFile.printf ("%10.2f", hoursWorked); 
     outPayrollFile.printf ("%10.2f", hourlyPayRate); 
     outPayrollFile.printf ("%10.2f", grossPay); 
     outPayrollFile.printf ("%10.2f", deduction); 
     outPayrollFile.printf ("%10.2f%n", netPay); 


     }//end while loop 

       /////////////////////////////////////////////////////////////////////////// 
    //after loop instructions go here 

     System.out.println (); 
     outPayrollFile.printf ("Total%29.2f", totalGrossPay); 
     outPayrollFile.printf ("%10.2f", totalDeduction); 
     outPayrollFile.printf ("%10.2f%n", totalNetPay); 
     outPayrollFile.printf ("Total Employees%4d", totalEmployees); 
     inPayrollFile.close(); 
     outPayrollFile.close(); 
     System.out.println ("Program Completed"); 
     System.out.println ("Program written by " + employeeName); 


    }//end of main 


/////////////////////////////////////////////////////////////////////////// 
    //regular pay method goes here 
/////////////////////////////////////////////////////////////////////////// 

public static double getRegPay (double mEmpHours, double mEmpRate) 

{//begin method 
    double result;  //local variable for gross 

     if (mEmpHours > 40) 
     { 
      result = mEmpRate * 40; 
     } 
     else 
     { result = mEmpHours * mEmpRate; 
     } //endif 


    return result;  //value in mGross is returned  
}//end method 

/////////////////////////////////////////////////////////////////////////// 
    //overtime pay method goes here 
/////////////////////////////////////////////////////////////////////////// 

public static double getEmpOvertimePay (double mEmpHours, double mEmpRate) 

{//begin method 
    double result;  //local variable for gross 

     if (mEmpHours > 40) 
     { 
      result = mEmpRate * 1.5 * (mEmpHours - 40); 
     } 
     else 
     { result = 0; 
     } //endif  


    return result;  //value in mGross is returned  
}//end method 

/////////////////////////////////////////////////////////////////////////// 
    //gross pay method goes here 
/////////////////////////////////////////////////////////////////////////// 

public static double getGrossPay (double empRegPay, double empOvertimePay) 

{//begin method 
    double result;  //local variable for gross pay 

      result = empRegPay + empOvertimePay; 

    return result;  //value in gross pay is returned 
}//end method   

/////////////////////////////////////////////////////////////////////////// 
    //bonus pay method goes here 
/////////////////////////////////////////////////////////////////////////// 

public static double getBonus (double mGrossPay, double mEmpHours, int mEmpDept) 

{//begin method 
    double mBonus; //local variable for bonus 

     if (mEmpDept ==25) 
     if (mEmpHours >= 40) 

      mBonus = 0.10 * mGrossPay; 
     else 
      mBonus = 0.05 * mGrossPay; 

     else mBonus = 0; 

    return mBonus; //value in mBonus is returned   
}//end method 

//switch method/translation of department code to department name 
public static String getEmpDeptCode (int empDeptCode) 

{//begin method 
String empDeptNumb; 

switch (empDeptNumb) 
{//begin switch 
    case 20:   //if code = 20 
     empDeptNumb = "Administration"; 
     break; 

    case 23:   //if code = 23 
     empDeptNumb = "Production"; 
     break; 

    case 25:   //if code = 25 
     empDeptNumb = "Sales"; 
     break; 

    default:   //else if none of the cases are true 
     empDeptNumb = "Invalid"; 

}//end switch 
return empDeptNumb; 
}//end method 

/////////////////////////////////////////////////////////////////////////// 
    //second gross pay method goes here 
/////////////////////////////////////////////////////////////////////////// 

public static double getEmpGrossPay (double empGrossPay, double empBonus) 

{//begin method 
    double result;  //local variable for gross pay 

      result = empGrossPay + empBonus; 

    return result;  //value in gross pay is returned 
}//end method   


////////////////////////////////////////////////////////////////////////// 
    //deduction pay method goes here 
////////////////////////////////////////////////////////////////////////// 
public static double getDeduction (double mGross) 
// getDeduction is the value-returning method name 
// it will be sent a double and that value goes into mGross 
{//begin method 
    double mDeduction; //local variable for deduction 

    if (mGross > 1000) 
     mDeduction = (mGross - 1000) * 0.30 + 225; 
    else 

    if (mGross > 500) 
     mDeduction = (mGross - 500) * 0.25 + 100; 
    else 
     mDeduction = mGross * 0.20; 

    return mDeduction; //value in mDeduction is returned 
}//end method 

////////////////////////////////////////////////////////////////////////// 
    //net pay method goes here 
////////////////////////////////////////////////////////////////////////// 

public static double getNetPay (double empGrossPay, double empDeduction) 

{//begin method 
    double netPay;  //local variable for net pay 

      result = empGrossPay - empDeduction; <--- this is the first error 

    return result;  //value in net pay is returned <--- this is the second error 
}//end method   

/////////////////////////////////////////////////////////////////////////// 
    //department code method goes here 
/////////////////////////////////////////////////////////////////////////// 

public static double empDeptNumb (double empDeptNumb) 

{//begin method 
    double empDeptCode; //local variable for empDeptCode <---this is the third error 

      result = empDeptNumb; <---this is the fourth error 

    return result;  //value in empDeptNumb is returned <--- the final error 
}//end method 

}//end of class  
+2

您切換的初始化字符串,然後你期望它要*是*(不要繼續ain)號碼?這應該如何工作? – Tom

回答

1

你需要改變你的switchswitch(empDeptNumb)switch(empDeptCode)因爲你是用整數20,30等..比較在case語句(如empDeptCode是整數,如果你改變switch(empDeptCode),它會工作。

+0

感謝您的回答!它確實解決了這個問題,但我現在有了另一個-_- –

0

我敢打賭,這是所有因拼寫錯誤。如果你有「開關(empDeptNumb)」這也許應該是「開關(empDeptCode)」

相關問題