java經典演算法之逆序輸出

來源:互聯網
上載者:User

標籤:ati   eve   變數   ++   逆序輸出   str   字元   oid   經典演算法   

 1 /** 2   *輸入字元,將數組逆序輸出 3   */ 4 import java.util.Scanner; 5 public class InReserve   6 { 7     public static void main(String[] args)  8     { 9         reverse();10     }11     public static void reverse(){12 13         String[] arr =new String[5];14         for(int i=0;i<arr.length;i++){15             Scanner input = new Scanner(System.in);16             System.out.println("請輸入第"+(i+1)+"個字元");17             arr[i] = input.next();18         }19 20         for(int p=0,q=arr.length-1;p<q;p++,q--){21             String temp = "";    //聲明臨時變數,儲存arr[p]22             temp = arr[p];23             arr[p] = arr[q];24             arr[q] = temp;        //25         }26         toString(arr);    //        調用toString()方法輸出數組27     }28 29     public static void toString(String[] arr){30         String str = "[";31         for(int i=0;i<arr.length;i++){32             str += arr[i];33             if(i<arr.length-1){34                 str += ",";35             }36         }37         str += "]";38         System.out.println(str);39      }40 }

 

java經典演算法之逆序輸出

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.