基本 Java 问题
Basic Java Issue
我编译没有错误,但是我可以毫无问题地完成第一个循环。然而第二次绕过它会提示部门名称,但提示员工人数。所以我最终看到输入部门名称:输入员工人数:
不确定为什么会这样,但我看的时候没有发现任何不妥之处。
如果你看到我哪里出错了,你能指出行号或代码字符串吗?
谢谢。
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 循环之外创建一个计数器变量。假设计数器 == 0,在循环结束时递增它,这样您就不会在使用我的解决方案的初始循环中产生问题。
int counter = 0;
While(){
System.out.print("Enter Division Name: ");
if(counter != 0){
input.nextLine();
}
name = input.nextLine();
counter++;
}
while(true) {
input = new Scanner(System.in);
}
在 while 循环中实例化您的 Scanner
实例 input
。
只需在每个 input.nextInt(); 之后添加 input.nextLine(); 或 输入。 nextDouble(); AS:
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
}
要了解为什么需要这样做,请阅读 Java 文档
我编译没有错误,但是我可以毫无问题地完成第一个循环。然而第二次绕过它会提示部门名称,但提示员工人数。所以我最终看到输入部门名称:输入员工人数:
不确定为什么会这样,但我看的时候没有发现任何不妥之处。
如果你看到我哪里出错了,你能指出行号或代码字符串吗?
谢谢。
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 循环之外创建一个计数器变量。假设计数器 == 0,在循环结束时递增它,这样您就不会在使用我的解决方案的初始循环中产生问题。
int counter = 0;
While(){
System.out.print("Enter Division Name: ");
if(counter != 0){
input.nextLine();
}
name = input.nextLine();
counter++;
}
while(true) {
input = new Scanner(System.in);
}
在 while 循环中实例化您的 Scanner
实例 input
。
只需在每个 input.nextInt(); 之后添加 input.nextLine(); 或 输入。 nextDouble(); AS:
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
}
要了解为什么需要这样做,请阅读 Java 文档