gpt4 book ai didi

java - 数组输出出现问题并提示用户输入输出

转载 作者:行者123 更新时间:2023-11-30 04:47:17 25 4
gpt4 key购买 nike

我的实验室作业遇到一些问题:

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

我的程序的输出:

Please enter the name of the second employee:Please enter the number of the second employee: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);
}
}

最佳答案

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

关于java - 数组输出出现问题并提示用户输入输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10683208/

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