繁体   English   中英

在java中将变量从if传递给else

[英]Passing variable from if to else in java

if循环中定义a变量,需要传递在else if循环中更新的值。 例子 :

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
}

首先,你的问题没有意义。 如果执行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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM