如何仅在键入所有输入后才打印输出,而不是逐行打印?
How do I print the output only after keying in all the inputs, instead of having it printed line by line?
我有一名员工class:
public class Employee{
private String firstName;
private String lastName;
private double salary;
private double rate;
public Employee(String firstName, String lastName, double salary){
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public double getSalary(){
return salary/1000;
}
public double setSalaryIncrease(double rate){
if ((rate-1)*100>=0){
this.rate = ((rate-1)*100);
return this.rate;
}
else{
this.rate = -((rate-1)*100);
return this.rate;
}
}
public String toString(){
return firstName+" "+lastName+": salary is "+String.format("%.0f", salary/1000)+"K, annual raise is "+String.format("%.0f", rate)+"%";
}
}
在我的主要 class、
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
Employee employee = new Employee(sc.next(), sc.next(), sc.nextDouble());
employee.setSalaryIncrease(sc.nextDouble());
System.out.println(employee);
}
}
}
例如,如果我有一个输入列表,例如
Mohammad Ali 24000.0 1.11
Steve Gates 36000.0 1.05
Michael Jordan 71000.0 1.07
我希望仅在扫描完所有 3 个输入后才打印它。但是,我最终在扫描每一行后打印每一行,即
穆罕默德·阿里 24000.0 1.11
Mohammad Ali:工资24K,年涨幅11%
史蒂夫·盖茨 36000.0 1.05
Steve Gates:工资36K,年涨幅5%
如何修改我的 toString() 方法以在读取所有输入后将所有内容作为一个整体打印出来,而不更改我的主要 class?
System.out.println(); prints every string in new line
尝试System.out.print("string")
在 while 循环之后你可以使用
System.out.println("") 跳转到新行
我有一名员工class:
public class Employee{
private String firstName;
private String lastName;
private double salary;
private double rate;
public Employee(String firstName, String lastName, double salary){
this.firstName = firstName;
this.lastName = lastName;
this.salary = salary;
}
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public double getSalary(){
return salary/1000;
}
public double setSalaryIncrease(double rate){
if ((rate-1)*100>=0){
this.rate = ((rate-1)*100);
return this.rate;
}
else{
this.rate = -((rate-1)*100);
return this.rate;
}
}
public String toString(){
return firstName+" "+lastName+": salary is "+String.format("%.0f", salary/1000)+"K, annual raise is "+String.format("%.0f", rate)+"%";
}
}
在我的主要 class、
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
Employee employee = new Employee(sc.next(), sc.next(), sc.nextDouble());
employee.setSalaryIncrease(sc.nextDouble());
System.out.println(employee);
}
}
}
例如,如果我有一个输入列表,例如
Mohammad Ali 24000.0 1.11
Steve Gates 36000.0 1.05
Michael Jordan 71000.0 1.07
我希望仅在扫描完所有 3 个输入后才打印它。但是,我最终在扫描每一行后打印每一行,即
穆罕默德·阿里 24000.0 1.11
Mohammad Ali:工资24K,年涨幅11%
史蒂夫·盖茨 36000.0 1.05
Steve Gates:工资36K,年涨幅5%
如何修改我的 toString() 方法以在读取所有输入后将所有内容作为一个整体打印出来,而不更改我的主要 class?
System.out.println(); prints every string in new line
尝试System.out.print("string") 在 while 循环之后你可以使用 System.out.println("") 跳转到新行