2017-02-12 7 views
-2

aの変数はifの変数で、ループはelse ifというループで更新された値を渡す必要があります。例:Javaのifからelseへの変数の受け渡し

if(document.getVersionIdentifier().getValue().equals("00")) 
{ 
    String a=attrs.put(CREATED_BY, shortenFullName(document 
          .getCreatorFullName())); 
    // Value a = USer1 
} 
else if(document.getVersionIdentifier().getValue().equals("01")) 
{ 
    String b = attrs.put(document,a); 
    // Need value of b to be User1 
} 

答えて

2

まず、あなたの質問は意味をなさない。 ifステートメントが実行されると、else ifは無視され、ifボディからelse ifボディへのデータはすべて無視されます。

ただし、else ifを別のifステートメントに変更し、ifボディの外側にaを定義することができます。原則として、これはこのように見えるかもしれません - あなたが本当に望むもの(あなたの質問からは不明です)に合わせて調整が必要です。

String a = null; 
if(document.getVersionIdentifier().getValue().equals("00")) 
{ 
    a = attrs.put(CREATED_BY, shortenFullName(document.getCreatorFullName())); 
    // Value a = User1 
} 

// The value of a can be either null or set during the if statement above. 
// If a has a value the next if statement will always be false so the value of a 
// will be always null if the next if statement is true. 
if(document.getVersionIdentifier().getValue().equals("01")) 
{ 
    String b = attrs.put(document,a); 
    // Need value of b to be User1 
} 
+3

コードが書かれているように意味をなさないことに注意してください。最初の 'if'が実行された場合、' getVersionIdentifier()。getValue() 'が2回呼び出されたときに異なる値を返すことができない限り、2番目の呼び出しは行われません。したがって、 'a'は決して' null '以外の何かになることはありません。だからこれは間違いなく微調整が必​​要です。 – ajb

+0

合意。これを明確にするために、より多くのコードドキュメントを追加しました。 –

関連する問題