二維數組計算最低值與最高值
public classConutMark { public static void main(String[] args) { //定義二維數組 int grades[][] = { {77, 68, 86, 73 }, { 96, 87, 89, 81 }, { 70, 90, 86, 81 } }; int lowGrade =grades[0][0]; //定義保存最低分數的變量 for (int i = 0; i<grades.length; i++) { //循環遍歷數組 for (int column = 0; column <grades[i].length; column++) { if (grades[i][column]<lowGrade) //取數組中最小值賦予lowGrade變量 lowGrade =grades[i][column]; } } inthighGrade =grades[0][0]; //定義保存最高分數的變量 for (int j = 0; j<grades.length; j++) { for (int column =0; column < grades[j].length; column++) { if(grades[j][column] >highGrade) //取數組中最大值賦予highGrade變量 highGrade =grades[j][column]; } } System.out.println("最低的分數爲:" +lowGrade); //打印數組中最小值 System.out.println("最高的分數爲:" +highGrade); //打印數組中最大值 } } |
判斷某數據是否在指定數組中
public class TestDemo { public static voidmain(String args[]) { int data [] = newint [] {209,201,2,2,3,6,7} ; int searchData = 3; // 要查找的內容 if(isExists(data,searchData)) { // 爲true System.out.println("已經查找到了內容。") ; } else { System.out.println("沒有找到內容。") ; } } public staticbooleanisExists(int temp [],int search) { //存在 for (int x = 0 ; x<temp.length ; x ++) { if(temp[x] == search) { returntrue ; // 查找到了,後面的循環不做了 } } return false ; // 沒有查找到 } } |