Java 您可以在 if-else 语句的条件中使用 for 循环吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22368120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:18:53  来源:igfitidea点击:

Can you use a for loop inside the condition of an if-else statement?

javaif-statementfor-loop

提问by user3413468

Can you use a for loop inside the condition of an if-elsestatement? For example, something like this...

您可以在if-else语句的条件中使用 for 循环吗?例如,这样的事情......

if(
        for(q = 0; q < 10; q++){
            values[q]>=values[q+1];
        }          
            )

            {

         done = 0;
    }

This is loading an error I can't seem to place. I want the if statement to check to see if the int[]I called valuesis in order from greatest to least, and if it is, set intvariable doneequal to 0.

这是加载一个我似乎无法放置的错误。我希望 if 语句检查int[]我调用的values是否按从大到小的顺序排列,如果是,则将int变量设置为done等于 0。

I only just started taking a programming class and I bet this is a very dumb mistake, but I've been trying to figure this out for a while and some help would be absolutely fantastic.

我才刚刚开始上编程课,我敢打赌这是一个非常愚蠢的错误,但我一直在努力解决这个问题,一些帮助绝对是太棒了。

采纳答案by wattostudios

You should work out your condition first (ie is your array in order), and then feed that in to your ifstatement. Like so...

您应该首先计算出您的条件(即按顺序排列数组),然后将其输入到您的if语句中。像这样...

boolean isOrdered = true;

for(q = 0; q < 10; q++){
  if (values[q]>=values[q+1]){
    // in order
    }
  else {
    // not in order
    isOrdered = false;
    break; // we have found a false, so we can quit out of the for loop
    }
  }

if (isOrdered){
  // do something if the array is in order;
  }

回答by Mauren

No, you can't. The ifcondition must evaluate to some boolean value, which doesn't happen with this forloop.

不,你不能。该if条件必须计算为一些布尔值,不与这种情况发生for循环。

It can only be in the ifstatement body, like

它只能在if语句体中,比如

if(someCondition)
    for(int i = 0;i < 10;i++)...

To achieve your goal, a code like this one might help:

为了实现您的目标,像这样的代码可能会有所帮助:

boolean ordered = true;

for(int i = 0;i < array.length - 1;i++) {
    if(array[i] > array[i+1]) {
        ordered = false;
        break;
    }
}

if(ordered) // your if body here

回答by Rainbolt

A for loop canexist inside of an if block

for 循环可以存在于 if 块中

if (true) {
    for (int i = 0; i < 5; i++) {
        System.out.println("Hello, World!");
    }
}

But a for loop can notbe the condition of the if block

但是 for 循环不能作为 if 块的条件

if( for (int i = 0; i < 5; i++) { } ) { }

A for loop is not a boolean. Every if condition requires a boolean.

for 循环不是布尔值。每个 if 条件都需要一个布尔值。

回答by Rami

The expression inside the "if" condition should evaluate to a boolean or Boolean otherwise the code will not compile.

“if”条件中的表达式应计算为布尔值或布尔值,否则代码将无法编译。

Check that Java specs page here

在此处检查 Java 规范页面

回答by merlin2011

I think you probably want the alternate nesting, and start with the assumption that done = 0(that is, that the list is correctly ordered), unless you prove in the loop that it is not true.

我认为您可能想要替代嵌套,并从假设done = 0(即列表已正确排序)开始,除非您在循环中证明它不正确。

public class Play{
    public static void main(String[] args) {
        done = 0;
        for(q = 1; q < 10; q++){
            if (values[q-1]<values[q]) {
                  done = 1;
                  break;
             }   

        }          


    }
}

回答by davecroman

No. Ifrequires a booleantest. For loopdoesn't return a boolean value. You can instead separate the check into another function.

没有If需要一个布尔测试。For loop不返回布尔值。您可以改为将检查分离到另一个函数中。

if( isArrayOrdered(values) ){
    done = 0;
}

// assuming values is an array of `int` values
private boolean isArrayOrdered( int[] values){
   for(int q = 1; q < values.length; q++){ 

      if( values[q] > values[q-1] ){
         return false;
      }
   }

   return true;
}

回答by Bohemian

You can do it if you refactor the logic into a method that returns a boolean:

如果将逻辑重构为返回布尔值的方法,则可以执行此操作:

if (isInOrder(values)) {
    //
}

private static boolean isInOrder(int[] array) {
    for (int i = 0; i < array.length - 1; i++)
        if (array[i] > array[i+1])
            return false;
    return true;
}

回答by Scary Wombat

You need to break you logic down into steps.

您需要将逻辑分解为多个步骤。

first of all, a method

首先,一个方法

boolean isInOrder (int [] values) {

    for(int q = 0; q < values.length - 1; q++){

        if (values[q] > values[q+1]) {
          return false; 
        }
   }
   return true;
}

// now lets call this method

// 现在让我们调用这个方法

  if (isInOrder (values)) {
     System.out.println ("In Order");
  }
  else {
     System.out.println ("Not In Order");
  }

回答by Phyo Gyi

It is impossible to put the "for loop" statement between if statement "(" and ")".But it is possible if you write "for loop" between if statement "{" and "}".Thanks

将“for 循环”语句放在 if 语句“(”和“)”之间是不可能的。但是如果你在 if 语句“{”和“}”之间写“for 循环”是可能的。谢谢