Java调用方法和陷阱返回

时间:2015-07-16 15:32:35

标签: java methods return-value

我在调用方法然后捕获其返回时遇到问题。

我需要它来更新结果,所以下一次循环它会看到它并返回不同的消息。

public class Patient {

private char patientStatus;

public boolean admit() {

    if (patientStatus != 'S')
        return false;

    else
        patientStatus = 'A';
    return true;
}

此部分位于main()方法

do {
        Patient temp = null;
        System.out.print("Enter selection: ");
        menuSelect = sc.nextLine();

        // validation

        if (menuSelect.length() != 1) {
            System.out.println("You must enter a single character");
        } else {
            menuAnswer = menuSelect.charAt(0);
            switch (menuAnswer) {
            case 'A':
            case 'a':

                // patient number

                System.out.print("Enter patient number: ");
                patNumber = sc.nextLine();

                // search for patient number

                for (int i = 0; i < pat.length && temp == null; i++) {
                    if (pat[i].getPatientNo().equals(patNumber)) {
                        temp = pat[i];
                    }
                }

                if (temp == null) {
                    System.out.println("Patient not found");
                } else {
                    System.out.println("Patient " + patNumber + " Found");

                    boolean patStatus = temp.admit();

                    if (patStatus == false) {
                        System.out.println("Admitted");

                    } else if (patStatus == true) {
                        System.out.println("Already admitted");
                    }

                }

            }
        }
    } while (menuAnswer != 'x' && menuAnswer != 'X');
    System.out.println("Exiting menu");

我不知道如何更新patStatus,以便下次在菜单中选择&#39; A&#39;并返回相同的患者编号&#34;已经录取&#34;。

如果有足够的代码来了解发生了什么,请告诉我。

2 个答案:

答案 0 :(得分:1)

您的Patient具有patientStatus的属性,但其值永远不会保存。您的admit()方法需要为其设置值。

目前,您的代码仅返回值,但不保存。 试试这个:

public class Patient {

private char patientStatus;

/** "Getter" method for patientStatus
*/
public char getPatientStatus(){
    return patientStatus;
}

/** "Admits" the new patient, changing its patientStatus
* @return "true" if patient is admitted; "false" if patient was already admitted.
*/
public boolean admit() {

    if (patientStatus != 'A')
        patientStatus = 'A'; //set the value to Admitted
        return true;
    else
        return false;
}

}

然后,在循环中,测试admit()调用的值:

if (temp == null) {
    System.out.println("Patient not found");
} else {
    System.out.println("Patient " + patNumber + " Found");

    boolean admitted = temp.admit(); // try to admit the patient

    if (admitted) {
        System.out.println("Admitted");
    } else { //You don't need another if here
        System.out.println("Already admitted");
    }
}

由于admitted属于boolean类型,因此您不需要使用==运算符,因为if语句使用boolean值作为if参数。

else之后您不需要第二个boolean语句,因为true只能有两个值,如果它不是false,那么它可以只有var controller = new UserController() var result = controller.productGrid(""); Assert.IsNotNull(result); var viewResult = result as PartialViewResult; var hasErrors = controller.ModelState.Values.Any(d => d.Errors.Any()); Assert.IsFalse(hasErrors); Assert.IsInstanceOf<PartialViewResult>(result); Assert.IsNotNull(viewResult); Assert.AreEqual(true, result.ViewData.Count > 0);

答案 1 :(得分:0)

   /* You have to re-factor the code on these lines. 
   Maintain Patients class which holds admitted patients.*/

public class Patients{

    private ConcurrentHashMap<Integer, Patient> allPatients = new ConcurrentHashMap();
    private HashSet<Integer) admittedPatients = new HashSet();

    public Patients(){

    }
    public void add(Patient p){
        allPatients.put(p.getPatientId(),p);
    }
    public Patient removePatient(int patientId){
        dischargePatients.remove(patientId);
        return allPatients.remove(patientId);
    }
    public Patient getPatient(int patientId){
        return allPatients.get(patientId);
    }
    public void admitPatient(int patientId){
        admittedPatients.add(patientId);
    }
    public boolean dischargePatient(int patientId){
        return admittedPatients.remove(patientId);
    }
    public boolean isAdmittedPatient(int patientId){
         return admittedPatients.contains(patentId);
    }
}

From `Patient.java` class, you can admit & discharge patient. 

如果getPatient()null则表示患者不在列表中。 一旦他出现,isAdmittedPatient返回是否 他是否被录取。