简体   繁体   English

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

[英]Passing variable from if to else in java

Defined a variable in if loop and need to pass the value updated in else if loop.if循环中定义a变量,需要传递在else if循环中更新的值。 Example :例子 :

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
}

First of all, your question makes no sense.首先,你的问题没有意义。 If the if statement is executed, the else if will be ignored hence passing on any data from the if body to the else if body is irrelevant.如果执行if语句,则else if将被忽略,因此将任何数据从if主体传递到else if主体是无关紧要的。

However, what you can do is changing the else if to a separate if statement and define a outside of the if bodies.但是,您可以做的是将else if更改为单独的if语句并定义if主体a外部。 In principle this could look like this - needs tweaking as per what you really want (unclear from your question).原则上这可能看起来像这样 - 需要根据您真正想要的进行调整(从您的问题中不清楚)。

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