public class Prog1{ public static void main(String[] args){ int n = 10; [size=10.5000pt] [size=10.5000pt] System.out.println("第"+n+"个月兔子总数为"+fun(n)); } private static int fun(int n){ if(n==1 || n==2) return 1; else return fun(n-1)+fun(n-2); } } |
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{ public static void main(String[] args){ int n = -1; try{ n = Integer.parseInt(args[0]); }catch(ArrayIndexOutOfBoundsException e){ [size=10.5000pt] [size=10.5000pt] [size=10.5000pt] System.out.println("请输入成绩"); return; } grade(n); } [size=10.5000pt] //成绩等级计算 private static void grade(int n){ if(n>100 || n<0) [size=10.5000pt] [size=10.5000pt] System.out.println("输入无效"); else{ [size=10.5000pt] [size=10.5000pt] String str = (n>=90)?"分,属于A等":((n>60)?"分,属于B等":"分,属于C等"); System.out.println(n+str); } } |
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |