7

ifステートメントを使用して戻り値の型がtruefalseかをチェックするブール値メソッドがあるとします。

public boolean isValid() {
   boolean check;
   int number = 5;

   if (number > 4){
      check = true;
   } else {
      check = false;
   }

 return check;

そして今、このメソッドを別のメソッドのifステートメントのパラメーターとして使用したいと考えています。

if(isValid == true)  // <-- this is where I'm not sure
   //stop and go back to the beginning of the program
else
   //continue on with the program

基本的に私が求めているのは、if ステートメントのパラメーター内のブール値メソッドの戻り値の型を確認するにはどうすればよいですか? ご回答ありがとうございます。

4

7 に答える 7

13

これはメソッドなので、後で括弧を使用して呼び出す必要があるため、コードは次のようになります。

if(isValid()) {
    // something
} else {
    //something else
}
于 2012-11-27T06:28:42.943 に答える
3
public boolean isValid() {
   int number = 5;
   return number > 4;
}

if (isValid()) {
    ...
} else {
    ...
}
于 2012-11-27T06:30:16.013 に答える
2

- Ifステートメントは値のみを受け入れ booleanます。

public boolean isValid() {

   boolean check = false;   // always intialize the local variable
   int number = 5;

   if (number > 4){
      check = true;
   } else {
      check = false;
   }

 return check;

}


if(isValid()){

    // Do something if its true
}else{

    // Do something if its false
}
于 2012-11-27T07:06:00.947 に答える
2

IF 条件内で関数を呼び出すことができるはずです。

if (isValid()) {

}else {

}

isValid()を返すのでboolean、条件はすぐに評価されます。条件をテストする直前にローカル変数を作成する方が良いと聞いています。

 boolean tempBoo = isValid();

 if (tempBoo) {

 }else {

 }
于 2012-11-27T06:31:48.743 に答える
0
if (isValid()) {
   // do something when the method returned true
} else {
   // do something else when the method returned false
}
于 2012-11-27T06:30:41.417 に答える
0

使用できます:

if(isValid()){
     //do something....
}
else
{
    //do something....
}
于 2012-11-27T06:34:02.270 に答える