编程语言
首页 > 编程语言> > Java黑皮书编程练习题7.19

Java黑皮书编程练习题7.19

作者:互联网

import java.util.Scanner;

public class Exercise07_19 {
    public static void main(String[] args) {
        Scanner input =new Scanner(System.in);
        System.out.println("Enter the size of the list : ");
        int size =input.nextInt();
        System.out.println("Enter the contents of the list : ");
        int[] list=new int[size];
        for (int i =0;i<list.length;i++){
            list[i]=input.nextInt();
        }
        System.out.print("The list has "+size+" integers");
        for (int i =0;i<list.length;i++){
            System.out.print(list[i]+" ");
        }
        System.out.println();
        if (isSorted(list))
            System.out.println("The list is already sorted");
        else
            System.out.println("The list is not sorted");

    }
    public static boolean isSorted(int[] list){
        boolean bool=true;
        for (int i=0;i<list.length;i++){
            for (int j=i+1;j<list.length;j++){
                if (list[j]<list[i])
                    bool=false;
                break;
            }
        }
        return bool;
    }
}

标签:练习题,Java,Scanner,int,list,System,Enter,黑皮,size
来源: https://blog.csdn.net/AJJGL/article/details/122265313