2013-01-21 33 views
13

मुझे जावा में बूलियन विधि को वापस करने के तरीके की सहायता चाहिए।जावा में एक बुलियन विधि कैसे वापस करें?

public boolean verifyPwd(){ 
     if (!(pword.equals(pwdRetypePwd.getText()))){ 
        txtaError.setEditable(true); 
        txtaError.setText("*Password didn't match!"); 
        txtaError.setForeground(Color.red); 
        txtaError.setEditable(false); 
      } 
     else { 
      addNewUser(); 
     } 
     return //what? 
} 

मैं verifyPwd() चाहते जब भी मुझे लगता है कि विधि कॉल करना चाहते हैं सही या गलत पर एक मूल्य के वापस जाने के लिए: यह नमूना कोड है। मैं इस तरह की विधि को इस तरह कॉल करना चाहता हूं:

if (verifyPwd()==true){ 
    //do task 
} 
else { 
    //do task 
} 

उस विधि के लिए मूल्य कैसे सेट करें?

+8

क्यों एक पूरी तरह से वैध सवाल जो जानने के लिए शुरू कर रहे हैं कि दूसरों के लिए उपयोगी हो सकता है के लिए downvote? –

+0

जो जानता कि आप क्या स्थिति के लिए वापस जाने के लिए करना चाहते हैं? .... –

उत्तर

18

आप एक से अधिक return बयान करने की अनुमति दी है, इसलिए इसे,

if (some_condition) { 
    return true; 
} 
return false; 

लिखने के लिए यह भी true या false को बूलियन मूल्यों की तुलना करने के अनावश्यक है कानूनी है ताकि आप लिख सकते हैं

if (verifyPwd()) { 
    // do_task 
} 

संपादित करें: कभी-कभी आप जल्दी वापस नहीं लौट सकते अधिक काम किया जाना है क्योंकि इस बात। उस स्थिति में आप एक बुलियन वैरिएबल घोषित कर सकते हैं और इसे सशर्त ब्लॉक के अंदर उचित रूप से सेट कर सकते हैं।

boolean success = true; 

if (some_condition) { 
    // Handle the condition. 
    success = false; 
} else if (some_other_condition) { 
    // Handle the other condition. 
    success = false; 
} 
if (another_condition) { 
    // Handle the third condition. 
} 

// Do some more critical things. 

return success; 
1
public boolean verifyPwd(){ 
     if (!(pword.equals(pwdRetypePwd.getText()))){ 
        txtaError.setEditable(true); 
        txtaError.setText("*Password didn't match!"); 
        txtaError.setForeground(Color.red); 
        txtaError.setEditable(false); 
        return false; 
      } 
     else { 
      addNewUser(); 
      return true; 
     } 
} 
+0

क्यों downvote? बस इतना मैं सीख सकता हूं कि गलती कहाँ थी। –

4

इस प्रयास करें:

public boolean verifyPwd(){ 
     if (!(pword.equals(pwdRetypePwd.getText()))){ 
        txtaError.setEditable(true); 
        txtaError.setText("*Password didn't match!"); 
        txtaError.setForeground(Color.red); 
        txtaError.setEditable(false); 
        return false; 
      } 
     else { 
      return true; 
     } 

} 

if (verifyPwd()==true){ 
    addNewUser(); 
} 
else { 
    // passwords do not match 
} 
+0

मैं जानता हूँ कि यह अपने मूल कोड में किया गया था, लेकिन वहाँ जब 'if' ब्लॉक' return' के साथ समाप्त हुआ एक 'else' के लिए कोई कारण नहीं है, और सच के साथ एक बूलियन मान तुलना करने के लिए कोई आवश्यकता नहीं है। –

2

तुम भी पठनीयता की खातिर ऐसा कर सकते हैं,

boolean passwordVerified=(pword.equals(pwdRetypePwd.getText()); 

if(!passwordVerified){ 
    txtaError.setEditable(true); 
    txtaError.setText("*Password didn't match!"); 
    txtaError.setForeground(Color.red); 
    txtaError.setEditable(false); 
}else{ 
    addNewUser(); 
} 
return passwordVerified; 
+0

यह रिकर्सिव नहीं है? –

+1

@JayMarz रिकर्सिव क्या है? –

0

सबसे अच्छा तरीका कोड ब्लॉक और return भीतर Boolean चर घोषित करने के लिए किया जाएगा कोड के अंत में, इस तरह:

public boolean Test(){ 
    boolean booleanFlag= true; 
    if (A>B) 
    {booleanFlag= true;} 
    else 
    {booleanFlag = false;} 
    return booleanFlag; 

} 

मैं इस का सबसे अच्छा तरीका लगता है।