Java典型題目 >>>>>>長期更新

1,數組順時針旋轉90度java

 1  
 2 //數組順時針旋轉90°  3 //列變行(正 0列-->0行,1列-->1行...)  4 //行變列(反 0行-->length-1列,1行-->length-2列...)
 5 public class Rotate {  6     public static void getRotate(int[][] x) {  7 
 8         int[][] b = new int[x[0].length][x.length];  9         // 轉換
10         for (int i = 0; i < x.length; i++) { 11             for (int j = 0; j < x[i].length; j++) { 12                 b[j][x.length - i - 1] = x[i][j]; 13  } 14  } 15         // 輸出
16         for (int i = 0; i < b.length; i++) { 17             for (int j = 0; j < b[i].length; j++) { 18                 System.out.print(b[i][j] + " "); 19  } 20  System.out.println(); 21  } 22 
23  } 24     // 測試
25     public static void main(String[] args) { 26         int[][] a = {{ 0, 1 ,2 }, 27                      { 0, 1 ,5 }, 28                      { 1, 1 ,6 }, 29                      { 1, 2 ,5 }, 30                      { 1, 2 ,5 } 31  }; 32  getRotate(a); 33  } 34 }

 運行結果:數組

1 1 1 0 0
2 2 1 1 1
5 5 6 5 2dom

 

2,九九乘法表測試

 1 /** 八、九九乘法表 */
 2 public class Job8 {  3 
 4     public static void main(String[] args) {  5         for (int i = 1; i <= 9; i++) {  6             for (int j = 1; j <= i; j++) {  7                 System.out.print(j + "*" + i + "=" + i * j + "\t");  8  }  9  System.out.println(); 10  } 11  } 12 }

運行結果:spa

 三、隨機生成4位驗證碼,輸入驗證碼驗證,不區分大小寫。code

 1 import java.util.Scanner;  2 /**
 3  * @author A_zhi  4  * 隨機生成4位驗證碼,輸入驗證碼驗證,不區分大小寫。  5  */
 6 public class Verificationcode {  7 
 8     public static void main(String[] args) {  9         Scanner sc = new Scanner(System.in); 10         String s = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 11         String y = ""; 12         //從字符串s的長度範圍內隨機產生整型數做爲其索引 13         //經過隨機索引返回該索引處的 char 值並放入字符串y
14         for (int i = 0; i < 4; i++) { 15             int a = (int) (Math.random() * s.length()); 16             y += s.charAt(a); 17  } 18         //輸出隨機產生的驗證碼
19         System.out.println("輸入0退出"); 20         System.out.println("驗證碼是:"+y); 21         while (true) { 22             System.out.print("請輸入驗證碼:"); 23             String ins = sc.next(); 24             if("0".equals(ins)){ 25                 System.out.println("已退出輸入"); 26                 break; 27  } 28             if (y.equalsIgnoreCase(ins)) { 29                 System.out.println("輸入驗證碼正確,正在進入系統..."); 30                 break; 31             } else
32                 System.out.println("輸入驗證碼錯誤,請從新輸入"); 33  } 34  sc.close(); 35  } 36 }

運行結果:blog

  這個題是我作老師給的練習「給定一個數組來存儲100個字符,請計算同一個英文字母所出現的次數」以後忽然想到的,生活中驗證碼實在太常見了,因此就試了一下,感受不錯啊。        2016/9/5索引

相關文章
相關標籤/搜索