基本Java问题
我编译时没有错误,但是我可以完成第一个循环,没有问题。但是,第二次遍历将提示输入部门名称,而提示输入员工人数则不会。最后我看到输入部门名称:输入员工人数: 不知道为什么会发生这种情况,但我看不出有什么不对劲的地方 如果你看到我哪里出错了,你能指出行号或代码串吗 谢谢基本Java问题,java,loops,while-loop,java.util.scanner,public,Java,Loops,While Loop,Java.util.scanner,Public,我编译时没有错误,但是我可以完成第一个循环,没有问题。但是,第二次遍历将提示输入部门名称,而提示输入员工人数则不会。最后我看到输入部门名称:输入员工人数: 不知道为什么会发生这种情况,但我看不出有什么不对劲的地方 如果你看到我哪里出错了,你能指出行号或代码串吗 谢谢 import java.util.Scanner; public class PayRoll { public static void main(String[] args) { Scanner inpu
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;
}
}
在while循环外创建一个计数器变量。假设counter==0,在循环结束时递增它,这样就不会使用我的解决方案在初始循环中产生问题
int counter = 0;
While(){
System.out.print("Enter Division Name: ");
if(counter != 0){
input.nextLine();
}
name = input.nextLine();
counter++;
}
在while循环中实例化扫描仪实例输入。只需添加input.nextLine;在每次输入之后。nextInt;或输入。下一步加倍;作为:
要理解为什么需要这样做,请阅读Java文档StackTrace在哪里?@AmiteshRai-OP说没有error@Ascalonian的确我只是误读了这个问题,然后用调试器进行了一步。如果你使用了调试器,你会看到在第一个循环之后这个名称就变成了。这会使第一个循环暂停。用户必须按两次enter键输入部门名称。但这对其他所有人都有效loops@Ascalonian只是通过创建一个计数器来修复它。谢谢你指出这不是一个有效的解决方案
while(true) {
input = new Scanner(System.in);
}
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();
input.nextLine(); // Add it Here
while(employees <= 0) {
System.out.print("Number of Employees Must be Greater than 0, Please Re-enter: ");
employees = input.nextInt();
input.nextLine();
}
System.out.print("Enter Average Salary: ");
salary = input.nextDouble();
input.nextLine();
while(salary <= 0) {
System.out.print("Average Salary Must be Greater than 0, Please Re-enter: ");
salary = input.nextDouble();
input.nextLine();
}
// Rest of the Code
}