进程停止,无法继续

时间:2014-10-22 13:42:43

标签: java loops if-statement while-loop

所以我发现我的编码存在问题,但我无法发现它

case 2:     
            char newAnswer1 = 'Y';
            if (pat1.getUreaLevel() > 0)
            {    

                System.out.print("The existing urea level is" + pat1.getUreaLevel());
                System.out.print("Do you want to overwrite the existing urea level?: ");
                newAnswer1 = sc.next().charAt(0);

            }

            if (newAnswer1 == 'y' || newAnswer1 == 'Y')
            {
                System.out.print("Enter the a value of urea level (mmol/l): ");


                double ureaLevel = sc.nextDouble();

                do
                {
                    double ureaLevel = sc.nextDouble();

                    if (ureaLevel < 0 || ureaLevel > 10)
                    {
                        System.out.println("The value entered is invalid.");
                        System.out.print("Please enter a new urea level (mmol/l) : ");

                    }

                }while(ureaLevel < 0 || ureaLevel > 10);
            }

我的问题是当我在输入值(无效或有效)后尝试输入尿素水平值时,它只是停止并结束该过程。哪部分我做错了?

1 个答案:

答案 0 :(得分:1)

我怀疑你的问题在这里

double ureaLevel = sc.nextDouble(); //1

do
{
  double ureaLevel = sc.nextDouble(); //2
  /* ... */
}while(ureaLevel < 0 || ureaLevel > 10);

你正在等待第一次迭代Double两次(第一次循环),它可能只是等待第二次进入,没有像你说的那样做。

解决方案?只需删除第一个: -

double ureaLevel;

do
{
  ureaLevel = sc.nextDouble(); //only one value fetched
  /* ... */
}while(ureaLevel < 0 || ureaLevel > 10);