当用户输入值时,java print语句发生冲突

时间:2014-10-16 10:36:58

标签: java

我解决了一个关于java的问题,我遇到了一些问题,我是一个初学者,所以请求帮助,我的打印语句(名称和部门)在从用户那里获取值时会相互冲突。

public class payroll2
{  

public static void main(String args[])
{
    payroll2 payroll = new payroll2();
    payroll.SetPayrollDetail();
    payroll.SetBonus();
    payroll.SetCommission();
    payroll.SetNssf();
    payroll.SetNetSalary();
    payroll.GetPayroll();
}

  Scanner myScanner=new Scanner(System.in);
  int empID;
  String empName;
  String empDept;
  String designation;
  int basicSalary;
  double netSal;
  double bonus;
  double commission;
  double nssf;



public void SetPayrollDetail()
{
    System.out.println("Enter ID: ");
    empID = myScanner.nextInt();

    System.out.println("Enter Name: ");
    empName = myScanner.nextLine();

    System.out.println("Enter Department (Marketing or Other): ");
    empDept = myScanner.nextLine();

    System.out.println("Enter Designation (Manager, Executive or Other):");
    designation = myScanner.nextLine();

    System.out.println("Enter Basic Salary: ");
    basicSalary = myScanner.nextInt();

}

public void SetBonus()
{

    if(basicSalary < 1500){
                bonus = 0.0;
            }
            else if(basicSalary>=1500 && basicSalary<3000){
                bonus = basicSalary * (12.0/100.0);
            }
            else if(basicSalary>=3000 && basicSalary<5000){
                bonus = basicSalary * (15.0/100.0);
            }
            else{
                bonus = basicSalary * (25.0/100.0);
    }
}

public void SetCommission()
{
    if( empDept.equalsIgnoreCase("other") ){
                commission = 0.0;
            }
            else if( empDept.equalsIgnoreCase("marketing") &&                      designation.equalsIgnoreCase("manager") ){
                commission = basicSalary * (30.0/100.0);
            }
            else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("executive") ){
                        commission = basicSalary * (15.0/100.0);
            }
            else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("other") ){
                        commission = basicSalary * (10.0/100.0);
            }
            else{
                commission = 0.0;
    }
}


public void SetNssf()
{
    if(basicSalary < 1500){
                nssf = basicSalary * (5.0/100.0);
            }
            else if(basicSalary>=1500 && basicSalary<3000){
                nssf = basicSalary * (8.0/100.0);
            }
            else if(basicSalary>=3000 && basicSalary<5000){
                nssf = basicSalary * (12.0/100.0);
            }
            else if(basicSalary>=5000 && basicSalary<7000){
                nssf = basicSalary * (15.0/100.0);
            }
            else if(basicSalary>=7000 && basicSalary<10000){
                nssf = basicSalary * (20.0/100.0);
            }
            else{
                nssf = basicSalary * (25.0/100.0);
            }

 }


public void SetNetSalary()
{
 netSal=(basicSalary + commission + bonus) - nssf;
}

public void GetPayroll()
{
System.out.println("\n\n\n\t\tPayroll Details \n____________________________________________________\n");

    System.out.println("Employee Id : " + empID + "\t\t Bonus : " + bonus);
  System.out.println("Name : " + empName + "\t\t\t\t Commission : " + commission);
  System.out.println("Department : " + empDept + "\t\t NSSF : " + nssf);
  System.out.println("Designation : " + designation + "\t\t NetSalary : " + netSal);

  System.out.println("Basic Salary : " + basicSalary + "\n");
}

}

1 个答案:

答案 0 :(得分:1)

编辑:

public void SetPayrollDetail()
{
    System.out.println("Enter ID: ");
    empID = myScanner.nextInt();

    System.out.println("Enter Name: ");
    empName = myScanner.next();

    System.out.println("Enter Department (Marketing or Other): ");
    empDept = myScanner.next();

    System.out.println("Enter Designation (Manager, Executive or Other):");
    designation = myScanner.next();

    System.out.println("Enter Basic Salary: ");
    basicSalary = myScanner.nextInt();

}

myScanner.next();