2016-03-27 16 views
1

私はユーザー入力を取得しており、whileループを使用して入力の検証を続けています。 しかし、入力する入力の種類が真であるとは限りませんが、偽を返してループを繰り返し続けます。 BSTクラスのwhileループが繰り返されない場合

String deletelName; 

System.out.println("Type patient's last name to delete"); 
deletelName = cin.next(); 

Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null); 

while (!bst.contains(removePatient)) { 
    System.out.println("Patient's last name does not exist. Type another last name : "); 
    deletelName = cin.next(); 
} 

パート:

public boolean contains(AnyType x) 
{ 
    return contains(x, root); 
} 


private boolean contains(AnyType x, BinaryNode<AnyType> t) 
{ 
    if (t == null) 
     return false; 

    int compareResult = x.compareTo(t.element); 

    if(compareResult < 0) 
     return contains(x, t.left); 

    else if (compareResult > 0) 
     return contains (x, t.right); 
    else 
     return true; 
} 
+3

'bst'は何ですか?A:このようなもので

Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null); while (!bst.contains(removePatient)) { System.out.println("Patient's last name does not exist. Type another last name : "); deletelName = cin.next(); } 

:ソリューションは、これを交換することですかこれ以上のコードを提供する必要があります。 – Gendarme

+0

あなたはそれを仲間で解決しましたか?あなたがした場合は、答えを受け入れてスレッドを閉じます。 – Vucko

答えて

1

これは非常に明白な理由のために永遠に行きます。

Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null); 

while (!bst.contains(removePatient)) { 
    System.out.println("Patient's last name does not exist. Type another last name : "); 
    deletelName = cin.next(); 
    removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null); 

} 
1

removePatientが変更されていない、唯一deletelName

これは、ループを使用するコードのセクションです。したがって、問題を解決するには、ループの最後にremovePatient = new Patient (deletelName.toLowerCase(),null,null,null,null);を追加してください。このライン

Patient removePatient = new Patient (deletelName.toLowerCase(),null,null,null,null); 

は、whileループではありませんので、あなたが新しい患者を毎回行っていないので、それは常に同じPatientでチェック:

+0

私はずっと長い間同じことを書いていました。あなたはその時代に答えました:D私は正しいと思います。 – Vucko

関連する問題