簡體   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錯誤的。

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; 
           } 
     } 
} 

創建一個方法來檢查數組是否已排序:

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
} 

並隨心所欲地使用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM