Java学习之在十个数中选出最大值和最小值案例
程序员文章站
2024-03-15 22:00:18
...
案例代码:
import java.util. Scanner;
public class TenNum{
public static void main( String[] args){
Scanner sc = new Scanner( System.in);
System. out. print("请输入第1个整数:");
int max = sc.nextInt();
int min = max;
for( int i = 2; i <= 10; i++){
System.out.print("请输入第"+i+"个整数:");
int num = sc.nextInt();
if( num > max)
max = num;
if( num < min)
min = num;
}
System.out.println( "最大值 = " + max);
System.out.println( "最小值 = " + min);
}
}
案例效果图: