简体   繁体   English

如何使用 Do-While 循环构造程序

[英]How to use Do-While Loop to construct the program

public static double avgArry(double[] a) {
        double sum = 0;
        double average = 0;
        int i =0;

        while(i<a.length) {
        sum += a[i] ;
        average = sum / a.length;
        ++i;
    }
        return average;
}   

    public static void main(String[] args) {
        // TODO Auto-generated method stub
double j;
        double[] array = {1.5, 20.3, 4.5, 5.5, 10.3, 450, 20.4, -22.3};

        j = avgArry(array);

        System.out.format("The average is: %.3f" , j);


}


}

The program above calculates the average of elements in an array.上面的程序计算数组中元素的平均值。 But my issue is how can I put it in Do-While loop?但我的问题是如何将它放入 Do-While 循环中? While loop and for-loop have been done by me. While 循环和 for-loop 已经由我完成。 Please help me out:,,,(. Please don't judge me I'm new to Java.请帮帮我:,,,(。请不要评判我,我是 Java 的新手。

The average is: 61.275平均值为:61.275

This should do it.这应该这样做。 Keep in mind for this to work you need to have at least one element in your array请记住,要使其正常工作,您的数组中至少需要一个元素

  do{
       sum += a[i] ;
       average = sum / a.length;
       ++i;
    }
    while(i<a.length);

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

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