day07-匿名对象的介绍
This commit is contained in:
@@ -1,15 +1,34 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user