[英]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.