简体   繁体   English

您如何在if语句中放入if,else语句

[英]How do you put a if, else statement within a for statement

How do you put an if...else statement within a for statement? 您如何将if...else语句放入for语句中?

This is the code: 这是代码:

// This is within a actionListener. When ever I press a button, this happens.
for (int i = 0; i < booleanVariable.length; i++) {
    //length is 8
    System.out.println("booleanVariable is" + booelanVariable[i]);
    if (booleanVariable[i] = true) {
        booleanVariable[i] = false;
        System.out.println("booleanVariable was true")
        break;
    } else {
        System.out.println(" booleanVariable is false");
    }
}

This is the output over 3 presses: 这是3次印刷的输出:

booleanVariable is true
booleanVariable was true
//correct    


booleanVariable is false
booleanVariable was true
//incorrect.    

booleanVariable is false
booleanVariable was true
//incorrect

This means that, for some reason, even if booleanVariable is false, the output says is true, which is false. 这意味着,由于某种原因,即使booleanVariable为false,输出也将显示为true,即为false。

What I've found: 我发现了什么:

  • Without the break; 没有break; statement, the program goes around the for all 8 times and outputs exactly the same as without a break , only incorrect values another 5 times. 声明中,程序进入周围的所有8次和输出完全一样,没有break ,只有不正确的值另外5次。

What I've tried: 我试过的

  • Changing the if from true to false (out of desperation) 将if从true更改为false(出于绝望)
  • If within a While within a For 如果在一段时间内
  • else, else if 其他,否则
  • case switch 机箱开关

Normally, I'd just use a switch, but I'm using booleans, and booleans dont work is switches. 通常,我只使用开关,但是我使用的是布尔值,布尔值不起作用是开关。

If someone knows the answer, I'd be forever grateful. 如果有人知道答案,我将永远感激不已。 Thanks. 谢谢。

Your problem may be in this line: 您的问题可能在以下行中:

if (booleanVariable[i] = true) {

Boolean comparisons should use == , unless you are intending to set the value to true. 布尔比较应使用== ,除非您打算将值设置为true。 In any event, this line will always evaluate to true. 无论如何,该行将始终为true。

Note also that you never really need to compare to true , you can simply say something like: 还要注意,您根本不需要真正地与true进行比较,您可以简单地说:

if (booleanVariable[i]) {

since if the value is true, the code block will be entered, and if it is false, it will proceed to the else case. 因为如果该值为true,则将输入代码块;如果为false,则将进行else情况。

Change your 改变你的

if (booleanVariable[i] = true) {

to

if (booleanVariable[i] == true) {

You need to use == operator for comparison. 您需要使用==运算符进行比较。 One = sets the value to true which returns always true. =将值设置为true ,则始终返回true。 You can also change it to 您也可以将其更改为

if (booleanVariable[i]) {

You have to use == to compare the values. 您必须使用==比较值。 Yu use the = that asigns the value true to the variable so it will always be true Yu使用=为变量赋值为true,因此它将始终为true

if (booleanVariable[i] == true)

There's an error in the if statement -- if语句中有一个错误-

You are assigning true to the variable, that's why it's outputting true each time. 您正在为变量分配 true,这就是为什么它每次都输出true。 it needs to be == (equals) not = (assign). 它必须是==(等于)而不是=(分配)。

if (booleanVariable[i])代替if (booleanVariable[i] = true) .... =是赋值运算符...您也可以使用==代替

You should change if (booleanVariable[i] = true) as if (booleanVariable[i] == true). 您应该将if(booleanVariable [i] == true)更改为if(booleanVariable [i] == true)。 need '==' instead of '='. 需要使用“ ==”而不是“ =”。

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

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