我有一個關於我的實驗室分配了一些麻煩:麻煩與陣列輸出,並提示用戶輸入輸出
當我的程序試圖提示用戶輸入,程序輸出在同一條線上兩個問題,只需要第二個問題的輸入。
我的程序的輸出:
請輸入第二個僱員的姓名:請輸入第二個僱員的數量: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);
}
}
由於一噸這樣解決了我的問題的第二部分:!d – xGiraffe
QUQ非常感謝爲你的幫助!我真的很感激它! – xGiraffe