实现抓娃娃游戏功能关键算法。Java使用数组、循环结构语句实现输入十个数字判断最大值,Java判断最大值。

请你在娃娃机里放十个娃娃,每个娃娃对应一个数字,该数字表示娃娃的大小。要求通过计算能输出最大的娃娃对应的数字,你可以这样做

  • ① 定义一个大小为 10 的整形数组 a;
  • ② 从键盘输入 10 个整数,放置到数组 a 中;
  • ③ 输出数组 a 中的最大值。

注意:使用数组、循环结构语句实现。

public class week02 {
    
    
	/**
	 * 传递一个整形数组,获取数组中的最大值
	 * @param intArrays
	 * @return
	 */
	public static int maxNum(int[] intArrays){
    
    
		int max = intArrays[0];//保存最大值
		for (int j = 0; j < intArrays.length; j++) {
    
    
				//依次和数组中的值判断大小来获取最大值
				if (max < intArrays[j]) {
    
    
					max = intArrays[j];
				}
		}
		return max;
	}
	
	public static void main(String[] args) {
    
    
		Scanner in = new Scanner(System.in);
		int[] intArrays = new int[10];
		for (int i = 0; i < intArrays.length; i++) {
    
    
			System.out.printf("输入第%d个值:",i+1);
			intArrays[i] = in.nextInt();
		}
		//得到最大值
		int max =maxNum(intArrays);
		System.out.println(max);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_41915181/article/details/102970256