java 數組中求最值,java數組
java中數組求最值,這在實際的開發中幾乎用不到,但是在面試中會偶爾被問到,這是考你基本的思維能力,現在說下這個題的基本思路
思路:
1:先定義一個變數,一般是用數組的第一個值
2:在迴圈中判斷(從第二個角標到數組的length-1角標對應的值)是否大於這個之前定義的值,如果大於那麼就把這個值賦值給max,直到比完就可以求出最大值
代碼如下:
public static void main(String[] args) {int[] arr = {1,3,5,6,9,7};int max = arr[0];for(int i=1;i<arr.length;i++){if(arr[i]>max){max = arr[i];}}}
JAVA編程數組最大值與最小值
兄弟以後注意點,編程是件心細的活,你在最後的
System.out.print("max="+max);
System.out.print("min="+max);
相同所以輸入相同,以後要注意點,其實JAVA更主要的是JavaEE編程,這些演算法其實不必過於在乎,希望對你有用!
public class a {
public static void main(String args[]) {
int a[]={84,40,16,3,10,49,28,76,94,70};
int n;
int min=a[0];
int i;
int max=a[0];
for(n=0;n<=9;n++) {
if(max<a[n])
max=a[n];
}
for(i=0;i<=9;i++) {
if(min>a[i])
min=a[i];
}
System.out.print("max="+max);
System.out.print("min="+min);
}
}
java數組最大值
import java.util.Scanner;
public class max {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
double[] g = new double[10];
double tem = 0.0;
for (int k = 0; k <= 9; k++) {
g[k] = scan.nextDouble();
}
int i, j = 0;
for (i = 0; i < 9; i++) {
if (g[j] < g[i + 1]) {
tem = g[i + 1];
j = i + 1;
}
}
System.out.println("max=" + tem);
}
}