数组输出问题和用户输入输出提示

时间:2012-05-21 10:04:36

标签: java arrays java.util.scanner

我的实验室任务有些问题:

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

我的程序输出:

  

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

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

数组输出也是如下:

0.00.00.00.00.00.00.00.00.00.0 

而不是像这样:

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

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

这是我的代码:

Employee.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);
}
}

2 个答案:

答案 0 :(得分:2)

对于您的第一个问题,只需将Scanners移至read_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();
}

答案 1 :(得分:0)

您不需要3种不同的扫描仪,一种是扫描仪。

对于打印部件,您需要println()或使用'\n'作为新行。

E.g:

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()),该函数会自动将新行添加到outputes文本中。在第二种情况下,您正在创建字符串的新行部分(您已经在第一行的开头使用了它)

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

铸造:

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

格式:

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

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