Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 何时用于、while或do while循环/如何启动_Java - Fatal编程技术网

Java 何时用于、while或do while循环/如何启动

Java 何时用于、while或do while循环/如何启动,java,Java,今天开始学习循环和不同的类型。我的问题是在这种情况下,我会尝试使用哪种类型?与其他人相比有什么优势?在看过我的课堂讲稿后,似乎应该始终使用do while,但我确信情况并非如此 还有,我将如何开始第一个关于返回给定数组的和的问题。给定的数组是什么?这就是我应该插入的运行参数行吗 public class SumMinMaxArgs { // TODO - write your code below this comment. // You will need to write

今天开始学习循环和不同的类型。我的问题是在这种情况下,我会尝试使用哪种类型?与其他人相比有什么优势?在看过我的课堂讲稿后,似乎应该始终使用do while,但我确信情况并非如此

还有,我将如何开始第一个关于返回给定数组的和的问题。给定的数组是什么?这就是我应该插入的运行参数行吗

 public class SumMinMaxArgs {
    // TODO - write your code below this comment.
    // You will need to write three methods:
    //
    // 1.) A method named sumArray, which will return the sum
    //     of the given array.  If the given array is empty,
    //     it should return a sum of 0.
    //
    // 2.) A method named minArray, which will return the
    //     smallest element in the given array.  You may
    //     assume that the array contains at least one element.
    //     You may use your min method defined in lab 6, or
    //     Java's Math.min method.
    //
    // 3.) A method named maxArray, which will return the
    //     largest element in the given array.  You may
    //     assume that the array contains at least one element.
    //     You may use your max method defined in lab 6, or
    //     Java's Math.max method.
    // 





    // DO NOT MODIFY parseStrings!
    public static int[] parseStrings(String[] strings) {
        int[] retval = new int[strings.length];
        for (int x = 0; x < strings.length; x++) {
            retval[x] = Integer.parseInt(strings[x]);
        }
        return retval;
    }

    // DO NOT MODIFY main!
    public static void main(String[] args) {
        int[] ints = parseStrings(args);
        System.out.println("Sum: " + sumArray(ints));
        System.out.println("Min: " + minArray(ints));
        System.out.println("Max: " + maxArray(ints));
    }
}

Java支持的四种循环:

循环的C样式:对于int i=0;i foreach循环,当您希望迭代集合,但不关心索引:for Customer c:customers{…}

while循环:whilesome_条件{…}当某些代码必须执行时,只要该条件为真。如果条件一开始为false,则块内的代码(即括号内的代码)将永远不会执行

do while循环:do{statement1;}while条件;将执行语句1,即使条件开始为false,但只执行一次


这三种形式的表现力完全相同。在特定情况下使用什么取决于风格、惯例和便利性。这很像你可以用不同的英语句子表达相同的意思

也就是说,do-while主要用于循环至少运行一次的情况,即只有在第一次迭代之后才检查条件


for主要用于迭代某个集合或索引范围。

和。读一下描述,你会发现其中一个人的逻辑完全不同。另一种方法主要是进行迭代,主要是因为您可以在末尾使用它做您想做的事情当您确定要运行循环多少次时,最好使用for循环,当您不知道要运行循环多少次但知道终止循环的条件时,最好使用while循环。do while在您希望至少运行一次循环时使用。我们不是来给您做作业的。在提出具体问题之前,请先尝试一下。读@BhawandeepSingla如果我想迭代X次,我可以做int I=0;当i++