Java 简单循环程序的问题。第二个循环不允许按预期输入
我已经做了一段时间了,似乎不明白为什么它会这样。在程序第一次运行之后,它不会等待分区名称提示的输入。我知道这是我的某种形式的语法错误,我就是一辈子也弄不明白Java 简单循环程序的问题。第二个循环不允许按预期输入,java,loops,Java,Loops,我已经做了一段时间了,似乎不明白为什么它会这样。在程序第一次运行之后,它不会等待分区名称提示的输入。我知道这是我的某种形式的语法错误,我就是一辈子也弄不明白 //payroll import java.util.Scanner; public class Payroll { public static void main(String[] args) { //create scanner object for inputs Scanner input = new Scanner
//payroll
import java.util.Scanner;
public class Payroll
{
public static void main(String[] args)
{
//create scanner object for inputs
Scanner input = new Scanner(System.in);
String name = "Null";
//create instance of class Division
Division newDivision1 = new Division();
//prompt for new division name.
do {
System.out.println( "Please Enter the Division Name. Enter \"stop\" to exit: " );
name = input.nextLine();
System.out.println();
if (!name.equals("stop"))
{
newDivision1.setDivName(name); //set name of object instance to input name.
//prompt for employees
System.out.println("Please input the number of employees for " + newDivision1.getDivName() + ".");
int employees = input.nextInt(); //capture int value of employees
System.out.println();
if (employees < 0)
{
do {
System.out.printf("Please input a positive value for the number of employees:\n");
employees = input.nextInt();
} while (employees < 0);
}
newDivision1.setDivEmployees(employees); //set employees to object instance
//prompt for average salary
System.out.println("Please input the average salary for " + newDivision1.getDivName() + ". \nPlease enter as digits and decimal only, \nexclude \"$\" and comma's.\n");
double salary = input.nextFloat(); //capture average salary as float
if (salary < 0)
{
do {
System.out.printf("Please input a positive value for the average salary: \n");
salary = input.nextFloat();
} while (salary < 0);
}
newDivision1.setAvgSalary(salary);//set average salary to object instance
//output totals
System.out.printf("The %s division contains %d employees with an average salary of $%.2f.\n", newDivision1.getDivName(), newDivision1.getDivEmployees(), newDivision1.getAvgSalary());
System.out.printf("The total payroll for the division is $%.2f.\n", newDivision1.getTotalSalary());
}
} while (!name.equals("stop"));
System.out.println( "The Program will now Exit.\n");
}//end main
}//end payroll
格式设置很正确,但这是基本程序
任何帮助都将不胜感激
补充:根据评论,我创建了3个不同的扫描仪,分别输入浮点、字符串和整数,完美解决了问题
吸取教训,谢谢 尝试用next替换nextLine。我看到的最简单的解决方案是这样的-
while (employees < 0) {
System.out.printf("Please input a positive value for the number of employees:\n");
if (input.hasNextInt()) {
employees = input.nextInt();
} else if (input.hasNext()) {
input.next();
} else {
System.err.println("no input");
System.exit(1);
}
}
或者,您可以阅读下一行并尝试解析它-
while (employees < 0) {
System.out.printf("Please input a positive value for the number of employees:\n");
if (input.hasNextLine()) {
employees = Integer.parseInt(input.nextLine().trim());
} else {
System.err.println("no input");
System.exit(1);
}
}
问题是,在while循环中的最后一次调用中,您使用的是nextFloat,并且由于用户输入(例如)5.0,然后按下Enter键,因此扫描仪仅捕获浮点部分,但将\n保留在缓冲区中 返回循环的开头时,nextLine会检测缓冲区中的\n并继续,因为它只读取了一行
您可以通过在while循环结束时添加另一个对nextLine的调用或使用next而不是nextLine来解决此问题。严格来说,它的可能重复并不是您的语法错误。我建议不要混合使用nextLine和nextLine/Float/Double/etc.调用,因为这会导致类似的情况,即输入流中存在空白,nextLine在下次调用时会捕捉到空白