简单的循环程序问题。第二个循环不允许输入按预期方式

时间:2014-06-09 03:26:49

标签: 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(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

格式化inst非常正确,但这是基本程序。

非常感谢任何帮助!

ADDED:根据评论,我创建了3个不同的扫描仪,分别用于输入浮点数,字符串和整数,它完美地解决了问题!

经验教训,谢谢!!

3 个答案:

答案 0 :(得分:0)

尝试将nextLine()替换为next() ..

答案 1 :(得分:0)

我看到的最简单的解决方案就是这样 -

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);
  }
}

答案 2 :(得分:0)

问题在于,在while循环中的最后一次调用中,您使用nextFloat()并且因为用户输入了5.0,然后按下&#34;输入&# 34; key - 扫描程序仅捕获Float部分,但将\n留在缓冲区中。

当你回到循环的开头时,nextLine()会在缓冲区中检测到\n并继续(因为它只读了&#34;行&#34;)。

您可以通过在while循环结束时添加对nextLine()的另一个调用或使用next()而不是nextLine()来解决此问题。