public class Prog1{ [size=10.5000pt] public static void main(String[] args){ [size=10.5000pt] [size=10.5000pt] int n = 10; [size=10.5000pt] [size=10.5000pt] System.out.println("第"+n+"个月兔子总数为"+fun(n)); [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] private static int fun(int n){ [size=10.5000pt] [size=10.5000pt] if(n==1 || n==2) [size=10.5000pt] [size=10.5000pt] return 1; [size=10.5000pt] [size=10.5000pt] else [size=10.5000pt] [size=10.5000pt] return fun(n-1)+fun(n-2); [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt]} |
public class Prog2{ public static void main(String[] args){ int m = 1; int n = 1000; int count = 0; //统计素数个数 for(int i=m;i<n;i++){ if(isPrime(i)){ count++; System.out.print(i+" "); if(count%10==0){ System.out.println(); } } } System.out.println(); System.out.println("在"+m+"和"+n+"之间共有"+count+"个素数"); } //判断素数 private static boolean isPrime(int n){ boolean flag = true; if(n==1) flag = false; else{ for(int i=2;i<=Math.sqrt(n);i++){ if((n%i)==0 || n==1){ flag = false; break; } else flag = true; } } return flag; } } |
public class Prog3{ public static void main(String[] args){ for(int i=100;i<1000;i++){ if(isLotus(i)) System.out.print(i+" "); } System.out.println(); } //判断水仙花数 private static boolean isLotus(int lotus){ int m = 0; int n = lotus; int sum = 0; m = n/100; n -= m*100; sum = m*m*m; m = n/10; n -= m*10; sum += m*m*m + n*n*n; if(sum==lotus) return true; else return false; } } |
public class Prog4{ public static void main(String[] args){ int n = 13; decompose(n); } private static void decompose(int n){ System.out.print(n+"="); for(int i=2;i<n+1;i++){ while(n%i==0 && n!=i){ n/=i; System.out.print(i+"*"); } if(n==i){ System.out.println(i); break; } } } } |
public class Prog5{ [size=10.5000pt] public static void main(String[] args){ [size=10.5000pt] [size=10.5000pt] int n = -1; [size=10.5000pt] [size=10.5000pt] try{ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] n = Integer.parseInt(args[0]); [size=10.5000pt] [size=10.5000pt] }catch(ArrayIndexOutOfBoundsException e){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] System.out.println("请输入成绩"); [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] return; [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt] grade(n); [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] //成绩等级计算 [size=10.5000pt] private static void grade(int n){ [size=10.5000pt] [size=10.5000pt] if(n>100 || n<0) [size=10.5000pt] [size=10.5000pt] System.out.println("输入无效"); [size=10.5000pt] [size=10.5000pt] else{ [size=10.5000pt] [size=10.5000pt] String str = (n>=90)?"分,属于A等":((n>60)?"分,属于B等":"分,属于C等"); [size=10.5000pt] [size=10.5000pt] System.out.println(n+str); [size=10.5000pt] [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt] [size=10.5000pt]} [size=10.5000pt]} |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |