Files
javaSE251223/day07/src/com/inmind/scanner01/Demo03.java
2025-12-29 10:56:52 +08:00

35 lines
1009 B
Java
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.inmind.scanner01;
import java.util.Scanner;
/*
常用类-Scanner-练习2_录入n个整数求最大值
*/
public class Demo03 {
public static void main(String[] args) {
//n可以手动赋值也可以用户赋值
Scanner sc = new Scanner(System.in);
System.out.println("请输入要计算的整数个数:");
int n = sc.nextInt();
//获取n个整数值for循环保存到一个容器数组
int[] arr = new int[n];
//n次循环
for (int i = 1; i <= n; i++) {
System.out.println("请输入第"+i+"个整数");
arr[i-1] = sc.nextInt();
}
//对容器中的值,取最大值
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
int temp = arr[i];
if (temp > max) {
max = temp;
}
}
//打印结果
System.out.println("您输入的"+n+"个整数的最大值为:"+max);
}
}