練習13(一位數組)
阿新 • • 發佈:2017-11-21
pub str print println clas void temp pac left
一堆人
package cn.ch.w; public class PersonArray { public static void main(String args[]) { person per[]=new person[3]; per[0]=new person("草菇",20); per[1]=new person("香菇",22); per[2]=new person("平菇",23); for (int x=0;x<per.length;x++) { per[x].getinfo(); } } }
package cn.ch.w; public 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); } }
一位數組
packagecn.ch.w; public class A { public void getFibonacci(int number){ int nLeft = 0; int nRight = 1; System.out.print(nRight); for (int i = 0; i < number; i++) { int sum = nLeft + nRight; System.out.print(" " + sum); nLeft = nRight; nRight= sum; } } public static void main(String[] args) { new A().getFibonacci(10); } }
選擇法
package cn.ch.w; public class A { public static void main(String[] args) { int[] arr={23,234,3,1,19,21,231,88,90,11}; for(int i=0;i<arr.length-1;i++) { for(int j=i+1;j<10;j++) { if(arr[i]>arr[j]) { int temp; temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } } } for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } } }
冒泡
package cn.ch.w; public class A { public static int main(String[] args) { int[] arr={23,234,3,1,19,21,231,88,90,11}; for(int i=0;i<10;i++) { for(int j=0;j<10-1-i;j++) { if(arr[j]>arr[j+1]) { int temp; temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } for(int i=0;i<arr.length;i++){ System.out.println(arr[i]); } return 0; } }
練習13(一位數組)