這裏的方法:這種方法有什麼問題?
public static String CPUcolor()
{
System.out.println ("What color am I?") ;
String s = getIns() ;
System.out.println ("are you sure I'm "+s+"? (Y/N)") ;
String a = getIns() ;
while (!((a.equals ("y")) || (a.equals ("Y")) || (a.equals ("n")) || (a.equals ("N"))))
{
System.out.println ("try again") ;
a = getIns() ;
}
if (a.equals ("n") || a.equals("N"))
{CPUcolor() ;}
System.out.println ("I am "+s) ;
return s ;
}
這裏是這種方法的一個可能的輸出(Y的和n的是用戶輸入):
What color am I? red are you sure I'm red? (Y/N) N What color am I? blue are you sure I'm blue? (Y/N) N What color am I? Yellow are you sure I'm Yellow? (Y/N) y I am Yellow I am blue I am red
爲什麼該行的「我藍色「和」我是紅色「印刷?爲什麼他們以相反的順序打印紅色,第一次輸入,最後打印?
見http://stackoverflow.com/questions/2611573#2611589爲什麼遞歸是完全錯誤的這個問題。 – vladr 2010-04-09 23:40:08