【发布时间】:2020-02-15 14:04:23
【问题描述】:
我正在创建一个显示员工姓名和薪水的程序。名称和薪水由用户输入,我想创建两个员工,即两个实例。如何从用户那里获得两个单独的输入?
import java.util.Scanner;
公共类员工{
String first_name;
String last_name;
double pay = 0;
public Employee(double p, String f, String l) {
first_name = f;
last_name = l;
pay = p;
}
public void name_printer(){
//To collect Area
System.out.println("Your name is "+ first_name+" "+last_name+".");
}
public void pay_printer(){
//To collect Area
pay=pay*12;
System.out.println("Your yearly pay is "+ pay+".");
}
public void pay_update(){
//To collect Area
pay=pay*12;
pay=pay+(pay*0.1);
System.out.println("Your raised yearly pay is "+ pay+".");
}
public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
System.out.print("Enter first name ");
String in1_f = input.nextLine();
System.out.print("Enter last name ");
String in1_l = input.nextLine();
System.out.print("Enter your monthly pay ");
double user1_p = input.nextDouble();
double in1_p=0;
if (user1_p>0){
in1_p=user1_p;
}
Employee employee1 = new Employee(in1_p, in1_f, in1_l);
employee1.name_printer();
employee1.pay_printer();
employee1.pay_update();
}
【问题讨论】:
-
您的代码正在使用
input.nextLine()从用户那里获取输入。究竟是什么阻止您再使用 3 次来获取下一位员工的信息?... -
我以为您想知道如何在设计方面做到正确。在这种情况下,你最好重用代码,我已经发布了一个答案,展示了如何做到这一点