/*使用一位數組解決 1 1 2 3 5 8 13 數列問題 斐波納契數列 Fibonacci*/ package cn.GM; public class array { public static void main(String[] args) { // TODO Auto-generated ... ...
/*使用一位數組解決 1 1 2 3 5 8 13 數列問題 斐波納契數列 Fibonacci*/ package cn.GM; public class array { public static void main(String[] args) { // TODO Auto-generated method stub int arr[] = new int[30]; arr[0] = 1; arr[1] = 1; for (int i = 2; i < arr.length; i++) { arr[i] = arr[i - 1] + arr[i - 2]; System.out.println(arr[i]); } } }
/* 選擇法。從小到大排列 */ package cn.GM; public class yiwei { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = { 10, 347, 657, 38, 382, 75, 76, 56, 12344, 84654 }; /* 選擇法。從小到大排列 */ for (int i = 0; i < arr.length - 1; i++) { for (int j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } } for (int i : arr) System.out.println(i); } }
/*冒泡法。從小到大排列*/ package cn.GM; public class yiwweiM { public static void main(String[] args) { // TODO Auto-generated method stub int[] arr = {10, 347, 657, 38, 382, 75, 76, 56, 12344, 84654}; /*冒泡法。從小到大排列*/ for(int i = 0;i < arr.length;i++) { for(int j = 0;j < arr.length - 1 -i;j++) { if(arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } for(int i : arr) System.out.println(i); } }
/*人類對象數組——一堆人*/ package cn.GM; public class personarr { public static void main(String[] args) { // TODO Auto-generated method stub //建數組 person arr[] = new person[3]; arr[0] = new person("趙藝",20); arr[1] = new person("歐陽娜娜",17); arr[2] = new person("房祖名",32); for(int x = 0;x < arr.length;x++) arr[x].getInfo(); } } class person{ private String name; private int age; public person(String name,int age) { this.name = name; this.age = age; } public void getInfo() { System.out.println("姓名:" + this.name + " 年齡:" + this.age); } }