Java教程

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黑皮书编程练习题7.19的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!