简体   繁体   English

如何在 output 中使用 boolean 方法打印返回类型?

[英]How to print return type with boolean method in output?

I created method that called bubble sorted method, but I want to that this method can return with output boolean type, if the array of elements sorted this method can return in output true, if the array of elements is not sorted, than in output print false. I created method that called bubble sorted method, but I want to that this method can return with output boolean type, if the array of elements sorted this method can return in output true, if the array of elements is not sorted, than in output print错误的。

public static boolean sorted(int[] arr) 
{ 
    int temp = arr[0]; 
    for (int i = 0; i < arr.length; i++) 
    { 
        for (int j = 0; j < arr.length-1; j++) 
        { 
            if (arr[j] > arr[j + 1]) 
            { 
               temp = arr[j];  //2 
               arr[j] = arr[j + 1]; 
               arr[j + 1] = temp; 
           } 
     } 
} 

Create a method to check if the array is sorted:创建一个方法来检查数组是否已排序:

public static boolean isSorted(int[] arr){ 
    for (int i = 0; i < arr.length - 1; i++) 
        if (arr[i] > arr[i + 1]) 
               return false;
   return true
} 

and used it as you want.并随心所欲地使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM