- //录入5个数取最大和最小值并存到数组中
- /*
- * 方案一:利用已知循环次数来进行创建数组,对其进行赋值操作!
- * */
- Scanner sc=new Scanner(System.in);
- int[] arr=new int[5]; //定义数组
- for (int i = 0; i < 5; i++) { //数组赋值
- String s;
- System.out.println("输入第"+(i+1)+"个数");
- arr[i]=sc.nextInt();
- }
- int max,min;
- min=max=arr[0];
- System.out.print("打印数组\n");
- for (int i = 1; i < arr.length-1; i++) {
- if (min>arr[i]) {
- min=arr[i];
- }
- if(max<arr[i]){
- max=arr[i];
- }
-
- }
- for (int i = 0; i < arr.length; i++) {
- System.out.print(arr[i]+"\t");
- }
- System.out.println();
- System.out.println("最大值为: "+max+", 最小值为: "+min);
复制代码 不用java Array类做!
|
|