for比大小

**

案例:找出数组中的最大值

**

//在数组中填入数据,然后找出数组中的最大值
  public static void main(String[] args) {
	int [] sz=new int[5];
	Scanner sc=new Scanner(System.in);
	System.out.println("请输入"+sz.length+"个数据");
	//在数组中填入数据
	for (int b=0;b<sz.length;b++){
		sz[b]=sc.nextInt();
	}
	System.out.println("输入的数据为"+Arrays.toString(sz));
	//用第一个数与后面的数进行比较,如果出现大于的数,则换上这个数继续进行比较
	int max=sz[0];
	for(int i=1;i<sz.length;i++){
		if(max<sz[i]){
			max=sz[i];
		}
	}
	System.out.println("该数组中最大的数是"+max);
	}

猜你喜欢

转载自blog.csdn.net/wen459/article/details/89575225