Java 是否可以在数组中使用 If Else 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19319993/
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-12 16:04:14  来源:igfitidea点击:

Is it possible to use If Else statement in an Array?

javaarraysfunctionloopsmethods

提问by Joey

import java.util.Arrays;

public class Arrays2 {

  public static void main(String[] args){
    int [] array = {1,2,10,20};

    addUp(array);
    showUp(array);
  }

  public static void addUp(int x[]){
    System.out.println("AddUp Function");
    for (int i = 0; i < x.length; i++){
      System.out.print(x[i]+"\t");
    }
    System.out.println ("\n");

  }

  public static void showUp(int y[]){
    System.out.println("ShowUp Function which Multiplies by 2!");
    **If(array[] >= 10)**
    for (int k = 0; k < y.length; k++){
      System.out.print(y[k]*2+"\t");
    }
  } 
}

How can I apply an if-else statement in an array? In my showUp method it should multiply the elements in the array if the value is greater than 10.

如何在数组中应用 if-else 语句?在我的 showUp 方法中,如果值大于 10,它应该乘以数组中的元素。

采纳答案by tobias_k

You probably want something like this:

你可能想要这样的东西:

for (int k = 0; k < y.length; k++) {
    if (y[k] >= 10) {
        System.out.print(y[k] * 2 + "\t");
    }
}

What you wrote looks similar to a filterin some other programming languages, but Java does not have those. You have to iterate the array and use an ifcheck insidethe loop.

您编写的内容与filter其他一些编程语言中的a 类似,但 Java 没有这些。您必须迭代数组并在循环使用if检查。

回答by maksimov

This snippet wouldn't even compile:

这个片段甚至不会编译:

public static void showUp(int y[]){
    System.out.println("ShowUp Function which Multiplies by 2!");
    If(array[] >= 10)
        for (int k = 0; k < y.length; k++){
            System.out.print(y[k]*2+"\t");
        }
} 

First, ifin Java is lowercase, and Java is case-sensitive. Second, there's no arrayvariable in the showUpmethod. Third, array[]is invalid syntax.

首先,if在Java中是小写的,而Java是区分大小写的。其次,方法中没有array变量showUp。第三,array[]是无效的语法。

Are you trying to check the length of your array y- the method parameter? Then you need to do this:

您是否要检查数组的长度y- 方法参数?然后你需要这样做:

public static void showUp(int y[]){
    System.out.println("ShowUp Function which Multiplies by 2!");
    if (y.length >= 10)
        for (int k = 0; k < y.length; k++){
            System.out.print(y[k]*2+"\t");
        }
} 

On second thoughts, the length can't be greater than 10, so please check the answer by @tobias_k - he seems to be right.

再想一想,长度不能大于 10,所以请检查@tobias_k 的答案 - 他似乎是对的。

回答by OOkhan

Are you trying to do this?

你想这样做吗?

if(arr[i] > 10 )
{
    arr[i]=arr[i]*2;
}
you can do this 

回答by Sachin M

you need to use .length

你需要使用 .length

public static void showUp(int y[]){
    System.out.println("ShowUp Function which Multiplies by 2!");
    if (y.length >= 10)
        for (int k = 0; k < y.length; k++){
            System.out.print(y[k]*2+"\t");
        }
}