【问题标题】:Trouble with array outputs and prompting for user-input outputs数组输出和提示用户输入输出的问题
【发布时间】:2012-05-21 10:04:35
【问题描述】:

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

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

我的程序的输出:

请输入第二名员工的姓名:请输入第二名员工的编号: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);
}
}

【问题讨论】:

    标签: java arrays java.util.scanner


    【解决方案1】:

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

    【讨论】:

    • 曲Q!非常感谢你的帮助!我真的很感激!
    【解决方案2】:

    您不需要 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 之间的数字指定要输出的小数位数。

    【讨论】:

    • 我之前试过一台扫描仪,但它给了我在同一行而不是分开的两行输出。我在 google 上搜索,它告诉我制作一个新的扫描仪,因为与 nextInt() 有关。它只是暂时修复了程序,然后在我添加更多编程内容后再次出现相同的输出>__
    • 感谢您的帮助! ^__^
    猜你喜欢
    • 1970-01-01
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多