Java是否有某种原因在Employee2中跳过fName?

huangapple go评论128阅读模式
英文:

Is there a reason Java is jumping over fName for Employee2?

问题

// 省略导入语句

public class EmployeeTest { // 员工测试类开始

    public static void main(String[] args) { // 主方法开始

        // 创建扫描器实例
        // 分配输入为变量
        Scanner input = new Scanner(System.in);

        // 创建变量
        String fName;
        String lName;
        double salary;

        // 生成两个 Employee 类的新实例
        // 将变量 myEmployee1 和 myEmployee2 分配给新实例
        Employee myEmployee1 = new Employee();
        Employee myEmployee2 = new Employee();

        // ... 省略部分代码 ...

        ///////////////////////////////////////////
        ////// 开始第一个员工部分 ///////
        /////////////////////////////////////////

        // 获取第一个员工的信息
        // 将第一个员工的信息分配给变量

        System.out.print("输入第一个员工的名字:");
        fName = input.nextLine(); // 获取第一个员工的名字

        System.out.print("输入第一个员工的姓氏:");
        lName = input.nextLine(); // 获取第一个员工的姓氏

        System.out.print("输入第一个员工的工资:");
        salary = input.nextDouble();

        if (salary < 0) {
            salary = 0.00;
        }

        // 设置第一个员工的信息
        myEmployee1.setFName(fName);
        myEmployee1.setLName(lName);
        myEmployee1.setSalary(salary);

        // ... 省略部分代码 ...

        ///////////////////////////////////////////
        ////// 开始第二个员工部分 ///////
        /////////////////////////////////////////

        // 获取第二个员工的信息
        // 将第二个员工的信息分配给变量

        System.out.print("输入第二个员工的名字:");
        fName = input.nextLine(); // 获取第二个员工的名字

        System.out.print("输入第二个员工的姓氏:");
        lName = input.nextLine(); // 获取第二个员工的姓氏

        System.out.print("输入第二个员工的工资:");
        salary = input.nextDouble();
        if (salary < 0) {
            salary = 0.00;
        }

        // 设置第二个员工的信息
        myEmployee2.setFName(fName);
        myEmployee2.setLName(lName);
        myEmployee2.setSalary(salary);

        // ... 省略部分代码 ...

        // 结束主要代码块

    } // 主方法结束

} // 员工测试类结束

public class Employee { // 员工类开始

    private String fName, lName; // 访问受限制的字符串变量
    private double salary; // 访问受限制的 double 类型的工资数据

    // 设置 fName 的方法
    public void setFName(String fName) {
        this.fName = fName;
    }

    // 获取 fName 的方法
    public String getFName() {
        return this.fName;
    }

    // 设置 lName 的方法
    public void setLName(String lName) {
        this.lName = lName;
    }

    // 获取 lName 的方法
    public String getLName() {
        return this.lName;
    }

    // 设置工资的方法
    public void setSalary(double salary) {
        this.salary = salary;
    }

    // 获取工资的方法
    public double getSalary() {
        return this.salary;
    }

} // 员工类结束
输入第一个员工的工资:20000
输入第二个员工的名字:james
输入第二个员工的姓氏:green
输入第二个员工的工资:10000
1 - 员工名字,姓氏,工资:lee, bron, 20000.00
2 - 员工名字,姓氏,工资:james, green, 10000.00

你想要对任何员工进行更改吗? true
你想要更改哪个员工? 
1
谢谢,祝您拥有愉快的一天!
1 - 员工名字,姓氏,工资:lee, bron, 20000.00
2 - 员工名字,姓氏,工资:james, green, 10000.00

提前感谢您的任何帮助。

英文:

I am working on an assignment and most everything is working except I have 2 java files, Employee and EmployeeTest. I have 2 instances of Employee name myEmployee1 and myEmployee2. I am able to input the first name, last name and salary for my Employee1, but it prints the request for the first name for employee 2, but does not let me put anything in because it then jumps directly to the last name for employee 2. I have checked and double checked my code, and all appears correct. Is there something I am missing?

EmployeeTest.java

//import scanner
import java.util.Scanner;
public class EmployeeTest { //begin employee test class
public static void main(String[] args) { //begin main method
//create scanner instance
//assign input as variable
Scanner input = new Scanner ( System.in);
//create variables
String fName;
String lName;
double salary;
//generate 2 new instance of Employee class
//assign variable myEmployee1 and myEmployee2 to new instance
Employee myEmployee1 = new Employee();
Employee myEmployee2 = new Employee();
///////////////////////////////////////////
////// Begin Employee 1 Section //////////
/////////////////////////////////////////
//Get Employee1 info
//assign employee1 info to variables
System.out.print(&quot;Enter first name for employee 1: &quot;);
fName = input.nextLine(); // get employee1 first name
System.out.print(&quot;Enter last name for employee 1: &quot;);
lName = input.nextLine(); // get employee1 last name
System.out.print(&quot;Enter salary for employee 1: &quot;);
salary = input.nextDouble();
if (salary &lt; 0) {
salary = 0.00;
}
//set employee1 info
myEmployee1.setFName(fName);
myEmployee1.setLName(lName);
myEmployee1.setSalary(salary);
/////////////////////////////////////////
////// End Employee 1 Section //////////
///////////////////////////////////////
///////////////////////////////////////////
////// Begin Employee 2 Section //////////
/////////////////////////////////////////
//Get Employee2 info
//assign employee2 info to variables
System.out.print(&quot;Enter first name for employee 2: &quot;);
fName = input.nextLine(); // get employee2 first name
System.out.print(&quot;Enter last name for employee 2: &quot;);
lName = input.nextLine(); // get employee2 last name
System.out.print(&quot;Enter salary for employee 2: &quot;);
salary = input.nextDouble();
if (salary &lt; 0) {
salary = 0.00;
}
//set employee2 info
myEmployee2.setFName(fName);
myEmployee2.setLName(lName);
myEmployee2.setSalary(salary);
///////////////////////////////////////////////////////////////////////////	
////// End Employee 2 Section //////////
//////////////////////////////////////////////////////////////////////////
//display employee1 info to terminal
System.out.printf(&quot;1 - Employee First Name, Last Name, Salary: %s, %s, %.2f&quot;, myEmployee1.getFName(), myEmployee1.getLName(), myEmployee1.getSalary());
//display employee2 info to terminal
System.out.printf(&quot;2 - Employee First Name, Last Name, Salary: %s, %s, %.2f&quot;, myEmployee2.getFName(), myEmployee2.getLName(), myEmployee2.getSalary());
///new code
//find if user wants to make changes to user
System.out.print(&quot;Would you like to make changes to any employees? &quot;);
changeInfo = input.next();
System.out.println(&quot;Which employee would you like to change? &quot;);
changeEmployee = input.nextInt();
//either run employee1, employee2 or exit based on users decision to change employee
if(changeInfo == &quot;true&quot; &amp;&amp; changeEmployee == 1) {
///////////////////////////////////////////
////// Rerun Employee 1 Section //////////
/////////////////////////////////////////
//Get Employee1 info
//assign employee1 info to variables
System.out.print(&quot;Enter first name for employee 1: &quot;);
fName = input.nextLine(); // get employee1 first name
System.out.print(&quot;Enter last name for employee 1: &quot;);
lName = input.nextLine(); // get employee1 last name
System.out.print(&quot;Enter salary for employee 1: &quot;);
salary = input.nextDouble();
input.nextLine(); // used to clear buffer of carriage return after nextDouble
//if salary is less than zero, set to 0.00
if (salary &lt; 0) {
salary = 0.00;
}
//set employee1 info
myEmployee1.setFName(fName);
myEmployee1.setLName(lName);
myEmployee1.setSalary(salary);
/////////////////////////////////////////
////// End Rerun Employee 1 Section /////
////////////////////////////////////////
}
else if(changeInfo == &quot;true&quot; &amp;&amp; changeEmployee == 2) {
///////////////////////////////////////////
////// Rerun Employee 2 Section //////////
/////////////////////////////////////////
//Get Employee2 info
//assign employee2 info to variables
System.out.print(&quot;Enter first name for employee 2: &quot;);
fName = input.nextLine(); // get employee2 first name
System.out.print(&quot;Enter last name for employee 2: &quot;);
lName = input.nextLine(); // get employee2 last name
System.out.print(&quot;Enter salary for employee 2: &quot;);
salary = input.nextDouble();
input.nextLine();
//if salary is less than zero, set to 0.00
if (salary &lt; 0) {
salary = 0.00;
}
//set employee2 info
myEmployee2.setFName(fName);
myEmployee2.setLName(lName);
myEmployee2.setSalary(salary);
///////////////////////////////////////////////
////// End Rerun Employee 2 Section //////////
//////////////////////////////////////////////
}
else {
System.out.println(&quot;Thank you and have a wonderful day!&quot;);
}
//output updated employee info
//display employee1 info to terminal
System.out.printf(&quot;1 - Employee First Name, Last Name, Salary: %s, %s, %.2f\n&quot;, myEmployee1.getFName(), myEmployee1.getLName(), myEmployee1.getSalary());
//display employee2 info to terminal
System.out.printf(&quot;2 - Employee First Name, Last Name, Salary: %s, %s, %.2f&quot;, myEmployee2.getFName(), myEmployee2.getLName(), myEmployee2.getSalary());
}//end main method
} // end employee test class

Employee.java


public class Employee { //begin Employee class
private String fName, lName; //access restricted String variables
private double salary; // access restricted data type double for salary
//setter for fName
public void setFName(String fName) {
this.fName = fName;
}
//getter for fName
public String getFName() {
return this.fName;
}
//setter for lName
public void setLName(String lName) {
this.lName = lName;
}
//getter for lName
public String getLName() {
return this.lName;
}
//setter for salary
public void setSalary(double salary) {
this.salary = salary;
}
//getter for salary
public double getSalary() {
return this.salary;
}
}//end Employee class

Output

Enter salary for employee 1: 20000
Enter first name for employee 2: james
Enter last name for employee 2: green
Enter salary for employee 2: 10000
1 - Employee First Name, Last Name, Salary: lee, bron, 20000.00
2 - Employee First Name, Last Name, Salary: james, green, 10000.00
Would you like to make changes to any employees? true
Which employee would you like to change? 
1
Thank you and have a wonderful day!
1 - Employee First Name, Last Name, Salary: lee, bron, 20000.00
2 - Employee First Name, Last Name, Salary: james, green, 10000.00

Thanks in advance for any help.

答案1

得分: 1

        System.out.print("输入雇员1的薪水:");
        salary = input.nextDouble();
        input.nextLine(); // <---

        if (salary < 0) {
            salary = 0.00;
        }

nextDouble 只会获取一个数字,会在缓冲区留下额外的换行符。你可以用 nextLine 清除它。

英文:
        System.out.print(&quot;Enter salary for employee 1: &quot;);
        salary = input.nextDouble();
        input.nextLine(); // &lt;---

        if (salary &lt; 0) {
            salary = 0.00;
        }

nextDouble is only going to grab a number, leaving an extra carriage return in buffer. You can clear it with a nextLine.

huangapple
  • 本文由 发表于 2020年9月11日 06:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/63838475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定