扫描仪不会停止询问输入

时间:2015-05-25 14:37:41

标签: java

我想在我的代码中手动读入一些值。它适用于除最后一个(lambda2)之外的所有值。我可以无限地继续键入值,即使它们不是双打,也没有任何事情发生。如果我用其他任何其他值键入其他内容,我会收到错误消息,以及第一次输入lambda2时。我在另一个代码中以类似的方式(最后只创建了一个不同的对象)的方式完成了它并且工作正常。

    System.out.print("All the asked input must be divided by 'Enters' \n");
    Scanner userinput = new Scanner(System.in);
    double b1;
    double b2;
    System.out.print("Enter the backlog costs for product 1 and 2: \n");
    b1 = userinput.nextDouble();
    b2 = userinput.nextDouble();
    double h0;
    double h1;
    double h2;
    System.out.print("Enter the holding costs for components 0, 1 and 2: \n");
    h0 = userinput.nextDouble();
    h1 = userinput.nextDouble();
    h2 = userinput.nextDouble();
    double lambda1;
    System.out.print("Enter the demand rate of product 1: \n");
    lambda1 = userinput.nextDouble();
    double lambda2;
    System.out.print("Enter the demand rate of product 2: \n");
    lambda2 = userinput.nextDouble();
    userinput.close();  

    double c1 = b1 + h0 + h1;
    double c2 = b2 + h0 + h2;
    SP solvable = new SP( b1, b2, h0, h1, h2, lambda1, lambda2, c1, c2);
    return solvable;

1 个答案:

答案 0 :(得分:1)

您必须有一些方法向您的程序表明您已完成收集输入。在您输入最后一个值之后,在获取更多输入之前没有进一步的指令要执行,因此它将继续尝试收集并且永远不会将控制权传递回您的程序。请查看this答案,了解如何实现这一目标。