陣列的列印,求和以及求最大值
阿新 • • 發佈:2022-03-10
``java
public class Demo03 {
public static void main(String[] args){
System.out.println("Hello World !");
int[] arrays = {1,2,10,4,5}; //列印陣列中的元素 for (int i = 0; i < arrays.length; i++) { System.out.println(arrays[i]); } System.out.println("===================分割線=============="); //計算所有元素的和 int sum = 0; for (int i = 0; i < arrays.length; i++) { sum = sum + arrays[i]; } System.out.println(sum); System.out.println("===================分割線=============="); //查詢最大元素 int standardMax = 0; for (int array : arrays) { if (array >= standardMax) { standardMax = array; } } System.out.println(standardMax); //查詢最大元素方法二 int standardMax1 = arrays[0]; for (int i = 1,j = arrays.length-1; i < arrays.length; i++,j--) { if (arrays[i] >= standardMax1){ standardMax1 = arrays[i]; } } System.out.println("zuida" + standardMax1); }