gpt4 book ai didi

java - 您好,我无法让 public String getGenderName() 输出用户输入的内容

转载 作者:行者123 更新时间:2023-12-01 12:41:31 24 4
gpt4 key购买 nike

我无法让 public String getGenderName() 输出用户输入的内容。它只是不断输出错误输入。请帮助我,我对java有点陌生,所以请不要半解释。你我的代码有什么问题吗?请告诉我,如果有人知道如何实现公共(public)枚举性别,我也有兴趣看看您对此的看法

import java.text.NumberFormat;
import java.util.Scanner;

public class Employee {
private String firstName;
private String lastName;
private static String gender;
private int dependents;
private double annualSalary;
private NumberFormat nf = NumberFormat.getCurrencyInstance();
private static Scanner scan;
private static char sex;

public Employee() {
firstName = "not given";
lastName = "not given";
gender = "";
dependents = 0;
annualSalary = 20.000;
}

public Employee(String first, String last, char sex, int dep, double salary) {
setFirstName(first);
setLastName(last);
Character.toString(sex);;
setDependents(dep);
setAnnualSalary(salary);
}

public double calculatePay() {
return annualSalary / 52;
}

public void displayEmployee() {
System.out.println("First Name: " + getFirstName());
System.out.println("Last Name: " + getLastName());
System.out.println("Gender: " + getGenderName());
System.out.println("Dependents: " + getDependents());
System.out.println("Annual Salary: " + nf.format(getAnnualSalary()));
System.out.println("Weekly Pay: " + nf.format(calculatePay()));
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}

public String getFirstName() {
return firstName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getLastName() {
return lastName;
}

***public String getGenderName()
{
if ("m".equalsIgnoreCase(gender))
return "Male";
if ("f".equalsIgnoreCase(gender))
return "Female";
else
return "Wrong Input";
}***

public void setDependents(int dependents) {
this.dependents = dependents;
}

public int getDependents() {
return dependents;
}

public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}

public double getAnnualSalary() {
return annualSalary;
}

public static void displayDivider(String outputTitle) {
System.out.println("**************** " + outputTitle + " ****************");
}

public static String getInput(String inputType) {
System.out.println("Enter the " + inputType + ": ");
scan = new Scanner(System.in);
String input = scan.next();
return input;
}

public static void main(String[] args) {
// Create an Employee object using the default constructor
displayDivider("Create an Employee object using the default constructor");
Employee employee1 = new Employee();
Employee.setGender(Employee.gender);
String input;
int dependents;
double annualSalary;
input = getInput("First Name");
employee1.setFirstName(input);
input = getInput("Last Name");
employee1.setLastName(input);
input = getInput("Gender");
setSex(input.charAt(0));
Employee.setGender("gender");
input = getInput("Dependents");
dependents = Integer.parseInt(input);
employee1.setDependents(dependents);
input = getInput("Annual Salary");
annualSalary = Double.parseDouble(input);
employee1.setAnnualSalary(annualSalary);
displayDivider("Employee Information");
employee1.displayEmployee();
displayDivider("Create a second Employee object using the multi-argument constructor");
Employee employee2 = new Employee("John", "Doe", 'M', 3, 46000.0);
displayDivider("Employee Information");
employee2.displayEmployee();
}

private static void setGender(String string) {
// TODO Auto-generated method stub

}

/**
* @return the sex
*/
public static char getSex() {
return sex;
}

/**
* @param sex the sex to set
*/
public static void setSex(char sex) {
Employee.sex = sex;
}
}

输出:

Enter the Annual Salary: 

Enter the First Name:
g
Enter the Last Name:
hhj
Enter the Gender:
m
Enter the Dependents:
7
Enter the Annual Salary:
9
**************** Employee Information ****************
First Name: g
Last Name: hhj
Gender: **Wrong Input**
Dependents: 7
Annual Salary: $9.00
Weekly Pay: $0.17
**************** Sample Employee Information ****************
First Name: John
Last Name: Doe
Gender: **Wrong Input**
Dependents: 3
Annual Salary: $46,000.00
Weekly Pay: $884.62

最佳答案

目前,您没有在构造函数中为gender 分配任何内容。你应该写:

gender = Character.toString(sex);

并且您的 setGender 方法为空

private static void setGender(String string) {
// TODO Auto-generated method stub

}

它应该是:

public static void setGender(String string) {
gender = string;

}

注意:您不应将性别保持为静态,这没有任何意义。

关于java - 您好,我无法让 public String getGenderName() 输出用户输入的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25055588/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com