Java Can';无法执行while循环。它通过并在最后再次请求patientid,然后退出
无法执行我的while循环。它通过并在最后再次请求patientid,然后退出Java Can';无法执行while循环。它通过并在最后再次请求patientid,然后退出,java,Java,无法执行我的while循环。它通过并在最后再次请求patientid,然后退出 //boolean flag for being in control group isInControlGroup = false; Scanner keyboard = new Scanner (System.in); //prompt for patients I.D., import keyboard. System.out.print("Enter Patient id: ");
//boolean flag for being in control group
isInControlGroup = false;
Scanner keyboard = new Scanner (System.in);
//prompt for patients I.D., import keyboard.
System.out.print("Enter Patient id: ");
//read Patients I.D.
patientId = keyboard.nextLine();
患者ID必须为三个数字,后跟a或b。像123a
这就是问题的症结所在:
//finding if patient is a part of the control group
while(patientId != null && !patientId.equals("")){ //Test for input in the patientID string. && !patientId.equals("")
//Decide if patient is in control group or not
if (patientId.charAt(patientId.length()-1)== 'a')
isInControlGroup = true;
else if(patientId.charAt(patientId.length()-1)== 'b')
isInControlGroup = false;
else
isInControlGroup = false;
I think it might not be reading the a and b properly.not sure though.
//prompt for the sex of the patient (M or F).
System.out.println("Enter sex (m/f) for patient:");
//read the sex of the patient
input = keyboard.nextLine();
sex = input.charAt(0);
//Prompt for total cholesterol, HDL, and triglycerides
System.out.println("Enter total cholesterol, HDL, and triglycerides for patient " + patientId + ":");
//read total cholesterol, HDL, and triglycerides.
totalCholesterol = keyboard.nextInt();
HDL = keyboard.nextInt();
triglycerides = keyboard.nextInt();
//Compute LDL value using LDL=totalCholesterol-HDL-triglycerides/5.
LDL = totalCholesterol - HDL - triglycerides/ 5;
//Compute totalChloesterolRatio(decimal division). totalChloesterolRatio=totalChloesterol/HDL
totalCholesterolRatio = totalCholesterol/(double)HDL;
totalCholesterolRatio= Math.round(totalCholesterolRatio*10);//Use Math.round.
totalCholesterolRatio= totalCholesterolRatio/10;//Use 10 to get decimal at tenth spot.
//Update quantitities to compute averages for aggregate report, using a boolean flag.
if(isInControlGroup==true)
{
controlMembers++;//Increment Counter for control members.
controlLdlTotal += LDL;
controlRatios += totalCholesterolRatio;
}
if(isInControlGroup== false) //If isInControlGroup is false, it adds counts to the treatment group.
{
treatmentMembers++;//Increment counter for treatment members.
treatmentLdlTotal += LDL;
treatmentRatios += totalCholesterolRatio;
}
//Determine the totalCholesterol classification
if (totalCholesterol >= 240)
totalChloesterolClassification = "HIGH";
else if (totalCholesterol >= 200)
totalChloesterolClassification = "BORDERLINE HIGH";
else if (totalCholesterol < 200)
totalChloesterolClassification = "DESIRABLE";
//Determine HDL classification
switch(sex)
{
case 'm'://When the sex is male.
case 'M':
if(HDL<40)
hdlClassification="LOW";
else if(HDL<=59)
hdlClassification="AVERAGE";
else if(HDL>=60)
hdlClassification ="HIGH";
break;
case 'w'://When sex is female.
case 'W':
if(HDL<50)
hdlClassification = "LOW";
else if(HDL<=59)
hdlClassification="AVERAGE";
else if(HDL>=60)
hdlClassification="HIGH";
break;
}
//Determine LDL classification
if(LDL >= 160)
LDLClassification = "HIGH";
if(LDL >= 130 && LDL <= 159 )
LDLClassification = "BORDERINE HIGH";
if(LDL >= 100 && LDL <= 129)
LDLClassification = "NEAR OPTIMAL";
if(LDL >= 0 && LDL < 100)
LDLClassification = "OPTIMAL";
//Determine triglycerides classification
if (triglycerides >= 200)
triglyceridesClassification = "HIGH";
else if (triglycerides >= 199)
triglyceridesClassification = "BORDERLINE HIGH";
else if (triglycerides < 150)
triglyceridesClassification = "DESIRABLE";
//Determine totalChloestrol/HDL Ratio classification
if (totalCholesterolRatio >= 5.1)
totalCholesterolRatioClassification = "INCREASED RISK";
else if (totalCholesterolRatio >= 3.3)
totalCholesterolRatioClassification = "AVERAGE RISK";
else if (totalCholesterolRatio < 3.3)
totalCholesterolRatioClassification = "DECREASED RISK";
System.out.println("Lipid Profile for Patient "+ patientId);
System.out.println("Total cholesterol: " + totalCholesterol + " " + totalChloesterolClassification );
System.out.println("HDL: "+ HDL+ " " + hdlClassification);
System.out.println("LDL:" + LDL + " "+ LDLClassification);
System.out.println("Triglycerides: " + triglycerides + " " + triglyceridesClassification );
System.out.println("Ratio: " + totalCholesterolRatio + " " + totalCholesterolRatioClassification );
仍然会再次提示输入id,然后退出。在循环的底部,您有一个额外的
键盘。nextLine()
您的代码被塞进了块中,并且不连贯,我们无法遵循控制流、变量初始化/重置等。由于格式不好,我不太理解您的代码,但是有一个patientId=keyboard.nextLine()代码>在每次迭代结束时,这可能会使条件patientId!=空&!patientId.equals(“”)
false,因此代码退出。
patientId=null;
System.out.print("Enter Patient id: ");
patientId = keyboard.nextLine();
keyboard.nextLine();
} //end of loop