Java Name字段在第二次通过while循环时没有返回任何内容
这个程序应该运行两次while循环。第一次运行时,它会显示姓名、职业、年龄、薪资和带奖金的薪资。但是,当循环第二次运行时,name字段为空,并且工资w/奖金添加到第一个员工的工资中。谢谢Java Name字段在第二次通过while循环时没有返回任何内容,java,Java,这个程序应该运行两次while循环。第一次运行时,它会显示姓名、职业、年龄、薪资和带奖金的薪资。但是,当循环第二次运行时,name字段为空,并且工资w/奖金添加到第一个员工的工资中。谢谢 import java.util.Scanner; public class Worker { public static void main(String[] args) { String name; int age; String occupat
import java.util.Scanner;
public class Worker {
public static void main(String[] args) {
String name;
int age;
String occupation;
double salary;
double total = 10000;
int count = 0;
Employee employeeInfo = new Employee();
Scanner keyboard = new Scanner(System.in);
while (count < 2) {
//User Name
System.out.println("Enter your name: ");
name = keyboard.nextLine();
employeeInfo.setEmployeeName(name);
keyboard.nextLine();
//User occupation
System.out.println("Enter your occupation: ");
occupation = keyboard.nextLine();
employeeInfo.setOccupation(occupation);
//User age
System.out.println("Enter your age: ");
age = keyboard.nextInt();
employeeInfo.setAge(age);
//User salary
System.out.println("Enter your salary: ");
salary = keyboard.nextDouble();
employeeInfo.setSalary(salary);
total = total + salary;
//Output information
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Occupation: " + occupation);
System.out.println("Original Salary: " + salary);
System.out.println("Salary with bonus: " + total);
count++;
}
// employeeInfo.greetingMessage("");
}
}
public class Employee {
private String employeeName;// Employee Name
private int age;// Employee Age
private String occupation;// Employee job title
private double salary;// Employee salary
// Setters
public void setEmployeeName(String name) {
employeeName = name;// Initializes employee name
}
public void setAge(int num) {
age = num;// Initializes employee age
}
public void setOccupation(String occu) {
occupation = occu;// Initializes occupation
}
public void setSalary(double num2) {
salary = num2;// Initializes salary
}
// Getters
public String getEmployeeName() {
return employeeName;
}
public int getAge() {
return age;
}
public String getOccupation() {
return occupation;
}
public double getSalary() {
return salary;
}
public void greetingMessage(String greeting){
System.out.println("Greetings " + getEmployeeName());
}
}
import java.util.Scanner;
公社工人{
公共静态void main(字符串[]args){
字符串名;
智力年龄;
字符串占用;
双薪;
双倍总数=10000;
整数计数=0;
员工employeeInfo=新员工();
扫描仪键盘=新扫描仪(System.in);
而(计数<2){
//用户名
System.out.println(“输入您的姓名:”);
name=keyboard.nextLine();
employeeInfo.setEmployeeName(名称);
keyboard.nextLine();
//用户职业
System.out.println(“输入您的职业:”;
职业=键盘.nextLine();
employeeInfo.setOccupation(职业);
//用户年龄
System.out.println(“输入您的年龄:”;
年龄=键盘.nextInt();
员工信息设置(年龄);
//用户工资
System.out.println(“输入您的薪资:”);
薪水=键盘.nextDouble();
员工信息固定工资(工资);
合计=合计+工资;
//输出信息
System.out.println(“名称:”+Name);
System.out.println(“年龄:+Age”);
System.out.println(“职业:”+职业);
System.out.println(“原始工资:+工资”);
System.out.println(“带奖金的工资:+总额”);
计数++;
}
//employeeInfo.greetingMessage(“”);
}
}
公营雇员{
私有字符串employeeName;//员工姓名
private int age;//员工年龄
私有字符串职业;//员工职务
私人双薪;//员工工资
//二传手
public void setEmployeeName(字符串名称){
employeeName=name;//初始化员工姓名
}
公共无效设置(整数){
age=num;//初始化员工年龄
}
公共无效设置占用(字符串占用){
占领=占领;//初始化占领
}
公共空间设置表(双倍num2){
salary=num2;//初始化salary
}
//吸气剂
公共字符串getEmployeeName(){
返回员工姓名;
}
公共整数getAge(){
回归年龄;
}
公共字符串getOccupation(){
返回职业;
}
公共双薪制{
返回工资;
}
公共无效问候语消息(字符串问候语){
System.out.println(“问候语”+getEmployeeName());
}
}
您应该在循环中移动此行,以便在每次迭代中创建一个新的employee对象
Employee employeeInfo = new Employee();
另外还有一个额外的
键盘.nextLine()代码>从键盘读取名称后 扫描仪有问题。你不应该在nextInt
之后再调用nextLine
——我读到了。您需要制作第二台扫描仪并使用它捕获数字,如下所示:
Scanner keyboard = new Scanner(System.in);
Scanner key2 = new Scanner(System.in);
如果需要字符串,请使用第一个字符串,对于数字,请使用第二个字符串:
name = keyboard.nextLine();
age = key2.nextInt();
另外,在while循环中实例化Employee对象,否则每次迭代都会覆盖它
while (count < 2) {
employeeInfo = new Employee();
while(计数<2){
employeeInfo=新员工();
最后,在获得姓名后但在获得职业之前删除第二条下一行。您必须在while循环开始时创建员工项目:
while(count < 2)
{
Employee employeeInfo = new Employee();
//...rest of your code...
}
while(计数<2)
{
员工employeeInfo=新员工();
//…剩下的代码。。。
}
无论如何,他从不使用employeeInfo
实例做任何事情-这不是问题所在。