获取错误“无法找到符号”和“变量已在方法中定义”

时间:2016-11-29 23:49:32

标签: java jgrasp

我是编码的新手。我正在尝试在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 个答案:

答案 0 :(得分:1)

您需要更改switch 当你在switch(empDeptNumb)语句中与整数20,30等进行比较时switch(empDeptCode)case empDeptCodeswitch(empDeptCode)是整数,如果你改为{{1} },它会起作用。

答案 1 :(得分:0)

我认为这完全是由于一个错字。你有“switch(empDeptNumb)”的地方应该是“switch(empDeptCode)”