1

我在实验室任务方面遇到了一些麻烦:

当我的程序尝试提示用户输入时,程序会在同一行输出两个问题,并且只接受第二个问题的输入。

我的程序的输出:

请输入第二名员工的姓名:请输入第二名员工的号码:1

(它们出现在同一行而不是单独的行)

数组的输出也是这样的:

0.00.00.00.00.00.00.00.00.00.0 

而不是这样:

0, 0, 0, 0, 0, 0, 0, 0, 0, 0

我不太确定如何解决这两个问题,任何帮助将不胜感激!

这是我的代码:

雇员.java

//import java.util.*;

public class Employee
{
    private String empName;
    private int empNumber;
    private String empAddress;
    private double empSalary;

private double[] empBonus=new double[10];

public Employee(){}

public Employee(String empName_, int empNumber_, String empAddress_, double empSalary_, double[] empBonus_)
{
    this.empName=empName_;
    this.empNumber=empNumber_;
    this.empAddress=empAddress_;
    this.empSalary=empSalary_;

    this.empBonus=empBonus_;
}

public String getName()
{
    return this.empName;
}

public int getEmployeeNumber()
{
    return this.empNumber;
}

public String getAddress()
{
    return this.empAddress;
}

public double getSalary()
{
    return this.empSalary;
}

public String changeAddress(String chAddress)
{
    return empAddress=chAddress;
}

public double changeSalary(double chSalary)
{
    return empSalary=chSalary;
}

public String addBonus(double[] empBonus)
{
    String arrayBonus=new String("");

    for(int i=0; i<empBonus.length;i++)
    {
        arrayBonus+=empBonus[i];
    }

    return arrayBonus;
}

public String toString()
{
    return ("\nEmployee's name: "+empName+"\nEmployee's Number: "+empNumber+"\nEmployee's address: "+empAddress+
            "\nEmployee's original salary: "+empSalary+ "\nEmployee's bonuses: "+addBonus(empBonus)+"\n");
}
}

EmployeeTester.java

import java.util.*;

public class EmployeeTester
{
public static void main(String[] args)
{
    Scanner in1=new Scanner(System.in);
    Scanner in2=new Scanner(System.in);
    Scanner in3=new Scanner(System.in);

    Employee emp1;
    Employee emp2;

    emp1=read_input("first", in1, in2, in3);
    emp2=read_input("second", in1, in2, in3);

    System.out.println(emp1.toString());
    System.out.println(emp2.toString());

}

public static Employee read_input(String msg, Scanner scan1, Scanner scan2, Scanner scan3)
{
    String name, address;
    int num;
    double salary;
    double[] bonus=new double[10];

    System.out.print("\nPlease enter the name of the "+msg+" employee:");
    name=scan1.nextLine();

    System.out.print("Please enter the number of the "+msg+" employee:");
    num=scan2.nextInt();

    System.out.print("Please enter the address of the "+msg+" employee:");
    address=scan1.nextLine();

    System.out.print("Please enter the salary of the "+msg+" employee:");
    salary=scan3.nextDouble();

    System.out.print("Please add a bonus for the "+msg+" employee:");
    bonus[0]=scan3.nextDouble();

    System.out.print("Add more bonuses to the "+msg+"employee? (y/n) \nNote: Enter 0.0 if you would like to terminate adding more bonuses: ");

    if(scan1.next().startsWith("y"))
    {
        for(int i=1; i<bonus.length;i++)
        {
            System.out.print("Continue entering a bonus to "+msg+" employee:");
            bonus[i]=scan3.nextDouble();

            if(bonus[i]==0.0 || i==bonus.length)
            {
                break;
            }
        }
    }   

    return new Employee(name, num, address, salary, bonus);
}
}
4

2 回答 2

2

对于您的第一个问题,只需改变您Scannersread_input方法,以便它们每次都重新开始。

public static void main(String[] args) {
    Employee emp1;
    Employee emp2;

    emp1 = read_input("first");

    emp2 = read_input("second");

    System.out.println(emp1.toString());
    System.out.println(emp2.toString());

}

public static Employee read_input(String msg) {
    Scanner scan1 = new Scanner(System.in);
    Scanner scan2 = new Scanner(System.in);
    Scanner scan3 = new Scanner(System.in);
    ...

对于您的第二个问题,在addBonus您构建输出字符串的方法中,您没有添加任何空格或逗号。StringBuilder此外,如果您使用这种类型的循环连接而不是重复创建新的字符串对象,它的效率也会更高。

public String addBonus(double[] empBonus)
{
    StringBuilder arrayBonus = new StringBuilder();

    for(int i=0; i<empBonus.length;i++)
    {
        arrayBonus.append(empBonus[i] + ", ");
    }

    return arrayBonus.toString();
}
于 2012-05-21T10:18:04.597 回答
0

您不需要 3 台不同的扫描仪,只需一台即可。

对于印刷部分,您需要println()或使用'\n'换行。

例如:

System.out.println("Please enter the name of the "+msg+" employee:"); 
System.out.print("Please enter the name of the "+msg+" employee:\n"); 

在第一种情况下,您正在调用一个函数(println()而不是print()),该函数会自动将换行符附加到输出文本。在第二种情况下,您正在制作字符串的换行部分(您已经在打印的第一行的开头使用了它)

使用数组输出0.0是因为这些值是浮点值(double在您的情况下),默认情况下会打印小数部分。要仅打印整数部分,您需要将值转换为整数或使用双精度格式。

铸件:

double a = 0.0;
System.out.print("'a' as integer: " + ((int)a));

格式化:

System.out.format("Here is a number: %.0f", a);

.和之间的数字f指定要输出的小数位数。

于 2012-05-21T10:10:20.947 回答