java數組複習 字符串轉數組 排序 擴容 猜數小遊戲

public static void main(String[] args) {
    *//**
     * 字符串轉爲數組
     *//*
    String str = "helloworld";
    char[] data = str.toCharArray();//將字符串轉爲數組
    for (int i = 0; i < data.length; i++) {
        System.out.print(data[i] + " ");
        data[i] -= 32;
        System.out.print(data[i] + " ");
    }
    System.out.println(new String(data));
}
運行結果截圖
public static void outPut(int[] array){
     if(array != null){
         for (int i = 0; i < array.length; i++) {
             System.out.print(array[i] + " ");
         }
     }
     System.out.println();
 }

 public static void main(String[] args) {
     int[] array = new int[5];
     //填充數組
     Arrays.fill(array,5);
     System.out.println("填充數組:Arrays.fill(array,5)");
     Test.outPut(array);


     //將數組的第二第三元素賦值爲8
     Arrays.fill(array,2,4,8);
     System.out.println("//將數組的第二第三元素賦值爲8 Arrays.fill(array,2,4,8)");
     Test.outPut(array);

     int[] array1 = {7,8,3,2,12,6,3,5,4};
     //對數組的2~6個進行排序
     Arrays.sort(array1,2,7);
     System.out.println("對數組的2~6個進行排序 Arrays.sort(array1,2,7)");
     Test.outPut(array1);

     //對整個數組進行排序
     Arrays.sort(array1);
     System.out.println("//對整個array1數組進行排序");
     Test.outPut(array1);

     //比較數組元素是否相等
     System.out.println("比較數組元素是否相等" + Arrays.equals(array, array1));
     int[] array2 = array1.clone();
     System.out.println("比較數組克隆後元素是否相等" + Arrays.equals(array1, array2));

     //二分法查找指定元素的下標(必須是排好序的)
     Arrays.sort(array1);
     System.out.println("元素3在array1中的位置" + Arrays.binarySearch(array1, 3));
     //如果不存在就返回負數
     System.out.println("元素9在array1中的位置" + Arrays.binarySearch(array1, 9));


 }
結果截圖

public static void main(String[] args) {
    int[] arr = {1,2,3,4,5};
    arr = Arrays.copyOf(arr,arr.length + 1);
    for (int i = 0; i < arr.length; i++) {
        System.out.print(arr[i] + " ");
    }
}
結果
private static Scanner scan;
 public static void main(String[] args) {
     scan = new Scanner(System.in);
     char[] chs = generate();
     System.out.println(chs);
     int count = 0;//猜錯的次數
     while (true) {
         System.out.println("來  ~ 猜");
         String str = scan.next().toUpperCase();
         if(str.equals("EXIT")){
             System.out.println("下次再見");
             break;
         }
         char[] input = str.toCharArray();//字符串轉數組
         int[] result = check(chs,input);
         if(result[0] == chs.length){
             int score = 100 * chs.length - 10 * count;
             System.out.println("猜對了,分數是" + score);
             break;
         }else{
             count++;
             System.out.println("對的個數爲" + result[1] + ",位置對個數爲" + result[0]);
         }
     }
 }
 //生成隨機字符數組
 public static char[] generate(){
     char[] chs = new char[5];
     char[] letters = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
             'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
             'W', 'X', 'Y', 'Z'};
     boolean[] flags = new boolean[letters.length];
     for (int i = 0; i < chs.length; i++) {
         int index;
         do{
             index = (int) (Math.random() * letters.length);
         }while(flags[index] == true);
         chs[i] = letters[index];
         flags[index] = true;
     }
     return chs;
 }
 //對比:隨機字符數組chs與用戶輸入廢人字符數組input
 public static int[] check(char[] chs,char[] input){
     int[] result = new int[2];//(0,0)
     for (int i = 0; i < chs.length; i++) {
         for (int j = 0; j < input.length; j++) {
             if(chs[i] == input[j]){
                 result[1]++;
                 if(i == j){
                     result[0]++;
                 }
                 break;
             }
         }
     }
     return result;
 }
結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章