【发布时间】:2015-03-07 20:27:37
【问题描述】:
我已经编译没有错误,但是我可以完成第一个循环没有问题。但是第二次绕过它会提示输入部门名称,但会提示输入员工人数。所以我最终看到输入部门名称:输入员工人数:
不知道为什么会发生这种情况,但我在查看时没有发现任何不合适的地方。
如果你看到我哪里出错了,你能指出行号或代码字符串吗?
谢谢。
import java.util.Scanner;
public class PayRoll {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name;
int employees;
double salary;
while(true) {
System.out.print("Enter Division Name: ");
name = input.nextLine();
if(name.equalsIgnoreCase("stop")) {
break;
}else {
System.out.print("Enter Number of Employees: ");
employees = input.nextInt();
while(employees <= 0) {
System.out.print("Number of Employees Must be Greater than 0, Please Re-enter: ");
employees = input.nextInt();
}
System.out.print("Enter Average Salary: ");
salary = input.nextDouble();
while(salary <= 0) {
System.out.print("Average Salary Must be Greater than 0, Please Re-enter: ");
salary = input.nextDouble();
}
Division d = new Division(name,employees,salary);
System.out.println();
System.out.println("Division " + d.getName());
System.out.println("Has " + d.getEmployees() + " Employees.");
System.out.printf("Averaging $%.2f\n",d.getSalary(),"per Employee");
System.out.printf("Making the Division total: $%.2f\n", d.getTotal());
System.out.println();
}
}
}
}
class Division {
private String name;
private int employees;
private double salary;
public Division(String name, int employees, double salary) {
this.name = name;
this.employees = employees;
this.salary = salary;
}
public double getTotal() {
return employees*salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getEmployees() {
return employees;
}
public void setEmployees(int employees) {
this.employees = employees;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
【问题讨论】:
-
StackTrace 在哪里..?
-
@AmiteshRai - OP 说没有错误
-
@Ascalonian 确实如此。我只是看错了问题
-
使用调试器逐步完成。
-
如果你使用了调试器,你会看到在第一个循环之后名字变成了
""
标签: java loops while-loop java.util.scanner public