0

无法执行我的 while 循环。它通过并在最后再次要求耐心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 之后,它会退出所有这些节目。

patientId=null;
System.out.print("Enter Patient id: ");
patientId = keyboard.nextLine();
keyboard.nextLine();

}   //end of loop

仍然再次提示输入 id,然后退出。

4

1 回答 1

0

在循环的底部,你有一个额外的keyboard.nextLine()

于 2013-10-15T14:41:13.397 回答