java 數組中求最值,java數組

來源:互聯網
上載者:User

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);
}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.