day07--常用类-Scanner-练习_录入三个整数求最大值
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
package com.inmind.scanner01;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
//常用类-Scanner-练习2_录入三个整数求最大值(数组),注意3可以写死,最好能动态
|
||||
public class Test02 {
|
||||
public static void main(String[] args) {
|
||||
//1.用户手动输入求最大值的数据的数量
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("请您输入要计算的整数的个数:");
|
||||
int count = sc.nextInt();
|
||||
|
||||
int[] arr = new int[count];
|
||||
|
||||
//2.动态创建数组,不停地让用户输入对应的值,并存放到数组中
|
||||
for (int i = 0; i < count; i++) {
|
||||
System.out.println("请求输入第"+(i+1)+"个整数");
|
||||
int temp = sc.nextInt();
|
||||
arr[i] = temp;
|
||||
}
|
||||
//3.等用户输入完毕,使用数组的遍历来求最大值
|
||||
int max = arr[0];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int temp = arr[i];
|
||||
if (temp > max) {
|
||||
max = temp;
|
||||
}
|
||||
}
|
||||
System.out.println("您输入的"+count+"个整数中,最大值为:"+max);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user