在if语句中分配变量值[duplicate]

时间:2022-11-11 19:37:58

This question already has an answer here:

这个问题在这里已有答案:

I was wondering whether it is possible to assign a variable a value inside a conditional operator like so:

我想知道是否可以在条件运算符中为变量赋值,如下所示:

if((int v = someMethod()) != 0) return v;

if((int v = someMethod())!= 0)return v;

Is there some way to do this in Java? Because I know it's possible in while conditions, but I'm not sure if I'm doing it wrong for the if-statement or if it's just not possible.

有没有办法在Java中做到这一点?因为我知道在条件允许的情况下这是可能的,但是我不确定我是否在if语句中做错了,或者它是不可能的。

9 个解决方案

#1


106  

Variables can be assigned but not declared inside the conditional statement:

可以在条件语句中分配变量但不声明变量:

int v;
if((v = someMethod()) != 0) return true;

#2


24  

You can assign, but not declare, inside an if:

您可以在if中分配,但不能声明:

Try this:

尝试这个:

int v; // separate declaration
if((v = someMethod()) != 0) return true;

#3


14  

an assignment returns the left-hand side of the assignment. so: yes. it is possible. however, you need to declare the variable outside:

赋值返回赋值的左侧。所以:是的。有可能的。但是,您需要在外部声明变量:

int v = 1;
if((v = someMethod()) != 0) {
    System.err.println(v);
}

#4


5  

Yes, you can assign the value of variable inside if.

是的,你可以在里面指定变量的值。

I wouldn't recommend it. The problem is, it looks like a common error where you try to compare values, but use a single = instead of == or ===.

我不推荐它。问题是,在您尝试比较值时,它看起来像是一个常见错误,但使用单个=而不是==或===。

It will be better if you do something like this:

如果你做这样的事情会更好:

int v;
if((v = someMethod()) != 0) 
   return true;

#5


2  

I believe that your problem is due to the fact that you are defining the variable v inside the test. As explained by @rmalchow, it will work you change it into

我相信你的问题是由于你在测试中定义变量v的事实。正如@rmalchow所解释的那样,你可以将其改为

int v;
if((v = someMethod()) != 0) return true;

There is also another issue of variable scope. Even if what you tried were to work, what would be the point? Assuming you could define the variable scope inside the test, your variable v would not exist outside that scope. Hence, creating the variable and assigning the value would be pointless, for you would not be able to use it.

还有另一个可变范围的问题。即使你尝试过的是什么工作,重点是什么?假设您可以在测试中定义变量范围,那么您的变量v将不存在于该范围之外。因此,创建变量并分配值将毫无意义,因为您将无法使用它。

Variables exist only in the scope they were created. Since you are assigning the value to use it afterwards, consider the scope where you are creating the varible so that it may be used where needed.

变量仅存在于它们创建的范围内。由于您要分配值以便在之后使用它,因此请考虑创建变量的范围,以便可以在需要的地方使用它。

#6


2  

Yes, it's possible to do. Consider the code below:

是的,这是可能的。考虑以下代码:

public class Test  
{        
    public static void main (String[] args)       
    {       
        int v = 0;          
        if ((v=dostuff())!=0)            
        {          
            System.out.printf("HOWDY\n");          
        }             
    }                
    public static int dostuff()       
    {             
        //dosomething              
        return 1; 
    }       
}          

I hope this will satisfy your question.

我希望这能满足你的问题。

#7


1  

Yes, it is possible to assign inside if conditional check. But, your variable should have already been declared to assign something.

是的,如果有条件检查,可以在里面分配。但是,您的变量应该已经被声明为分配一些东西。

#8


0  

You can assign a variable inside of if statement, but you must declare it first

您可以在if语句中指定变量,但必须先声明它

#9


0  

Because I know it's possible in while conditions, but I'm not sure if I'm doing it wrong for the if-statement or if it's just not possible.

因为我知道在条件允许的情况下这是可能的,但是我不确定我是否在if语句中做错了,或者它是不可能的。

HINT: what type while and if condition should be ??

提示:什么类型的时候和条件应该是??

If it can be done with while, it can be done with if statement as weel, as both of them expect a boolean condition.

如果可以用while完成,可以使用if语句作为weel来完成,因为它们都期望一个布尔条件。

#1


106  

Variables can be assigned but not declared inside the conditional statement:

可以在条件语句中分配变量但不声明变量:

int v;
if((v = someMethod()) != 0) return true;

#2


24  

You can assign, but not declare, inside an if:

您可以在if中分配,但不能声明:

Try this:

尝试这个:

int v; // separate declaration
if((v = someMethod()) != 0) return true;

#3


14  

an assignment returns the left-hand side of the assignment. so: yes. it is possible. however, you need to declare the variable outside:

赋值返回赋值的左侧。所以:是的。有可能的。但是,您需要在外部声明变量:

int v = 1;
if((v = someMethod()) != 0) {
    System.err.println(v);
}

#4


5  

Yes, you can assign the value of variable inside if.

是的,你可以在里面指定变量的值。

I wouldn't recommend it. The problem is, it looks like a common error where you try to compare values, but use a single = instead of == or ===.

我不推荐它。问题是,在您尝试比较值时,它看起来像是一个常见错误,但使用单个=而不是==或===。

It will be better if you do something like this:

如果你做这样的事情会更好:

int v;
if((v = someMethod()) != 0) 
   return true;

#5


2  

I believe that your problem is due to the fact that you are defining the variable v inside the test. As explained by @rmalchow, it will work you change it into

我相信你的问题是由于你在测试中定义变量v的事实。正如@rmalchow所解释的那样,你可以将其改为

int v;
if((v = someMethod()) != 0) return true;

There is also another issue of variable scope. Even if what you tried were to work, what would be the point? Assuming you could define the variable scope inside the test, your variable v would not exist outside that scope. Hence, creating the variable and assigning the value would be pointless, for you would not be able to use it.

还有另一个可变范围的问题。即使你尝试过的是什么工作,重点是什么?假设您可以在测试中定义变量范围,那么您的变量v将不存在于该范围之外。因此,创建变量并分配值将毫无意义,因为您将无法使用它。

Variables exist only in the scope they were created. Since you are assigning the value to use it afterwards, consider the scope where you are creating the varible so that it may be used where needed.

变量仅存在于它们创建的范围内。由于您要分配值以便在之后使用它,因此请考虑创建变量的范围,以便可以在需要的地方使用它。

#6


2  

Yes, it's possible to do. Consider the code below:

是的,这是可能的。考虑以下代码:

public class Test  
{        
    public static void main (String[] args)       
    {       
        int v = 0;          
        if ((v=dostuff())!=0)            
        {          
            System.out.printf("HOWDY\n");          
        }             
    }                
    public static int dostuff()       
    {             
        //dosomething              
        return 1; 
    }       
}          

I hope this will satisfy your question.

我希望这能满足你的问题。

#7


1  

Yes, it is possible to assign inside if conditional check. But, your variable should have already been declared to assign something.

是的,如果有条件检查,可以在里面分配。但是,您的变量应该已经被声明为分配一些东西。

#8


0  

You can assign a variable inside of if statement, but you must declare it first

您可以在if语句中指定变量,但必须先声明它

#9


0  

Because I know it's possible in while conditions, but I'm not sure if I'm doing it wrong for the if-statement or if it's just not possible.

因为我知道在条件允许的情况下这是可能的,但是我不确定我是否在if语句中做错了,或者它是不可能的。

HINT: what type while and if condition should be ??

提示:什么类型的时候和条件应该是??

If it can be done with while, it can be done with if statement as weel, as both of them expect a boolean condition.

如果可以用while完成,可以使用if语句作为weel来完成,因为它们都期望一个布尔条件。