0
無法讓我的while循環執行。它經過並要求在再次結束patientid,然後退出無法讓我的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: ");
//read Patients I.D.
patientId = keyboard.nextLine();
患者ID必須被隨後a或b 3號。像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後,它退出這一切說明。
patientId=null;
System.out.print("Enter Patient id: ");
patientId = keyboard.nextLine();
keyboard.nextLine();
} //end of loop
再次提示輸入id,然後退出。
你的代碼塞進塊等脫節,我們不能按照控制流,無功inialization /復位等 –
我真的不理解你的代碼由於差的格式化,但在每次迭代結束時有一個'patientId = keyboard.nextLine();'這可能使條件'patientId!= null &&!patientId.equals(「」)'false,因此代碼退出。 – Daniel