java经典小程序

以下答案源于java菜鸟学堂(144648357)群共享
第一题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * * 【程序1】 * 题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一 * 对兔子,假如兔子都不死,问每个月的兔子总数为多少? * 1.程序分析: 兔子的规律为数列1,1,2,3,5,8,13,21.... */ import java.util.Scanner; public class ProgramEx1 { public static void main(String[] args) { while (true) { System.out.print("请输入你要计算的月数:"); Scanner scanner = new Scanner(System.in); int month = 3; try{ month = scanner.nextInt(); if (month<=0) { throw new Exception(); } } catch (Exception e){ System.out.println("哈哈,非常不好意思你输入有错误哦,重新输入"); continue; } if (month<3) { System.out.println("第一个月兔子总数为:1"); return; } int rabbitSum1 = 1; int rabbitSum2 = 1; for (int i=3; i<=month; i++) { int temp = rabbitSum1; rabbitSum1 = rabbitSum1 + rabbitSum2; rabbitSum2 = temp; System.out.println(""+i+"个月兔子总数为:"+rabbitSum1); } break; } } }

第二题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * * 【程序2】 * 题目:判断101-200之间有多少个素数,并输出所有素数。 * 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, * 则表明此数不是素数,反之是素数。 */ public class ProgramEx2 { public static void main(String[] args) { // TODO Auto-generated method stub for (int i=101; i<200; i+=2) { boolean f=true; for (int j=2; j<i;j++) { if(i%j == 0) { f = false; break; } } if (!f) { continue; } System.out.println(" "+i); } } }

第三题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序3】 * 题目:打印出所有的"水仙花数",所谓"水仙花数"是指一个三位数,其各位数字立方和等于该数本身。例如: * 153是一个"水仙花数",因为153=1的三次方+5的三次方+3的三次方。 * 1.程序分析:利用for循环控制100-999个数,每个数分解出个位,十位,百位 */ public class ProgramEx3 { public static void main(String[] args) { for (int i=100; i<=999; i++) { int a = i/100; int b = i%100/10; int c = i%10; if (i == a*a*a + b*b*b + c*c*c) { System.out.println(i); } } } }

第四题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序4】 * 题目:将一个正整数分解质因数。例如:输入90,打印出90=2*3*3*5。 * 程序分析:对n进行分解质因数,应先找到一个最小的质数k,然后按下述步骤完成: * (1)如果这个质数恰等于n,则说明分解质因数的过程已经结束,打印出即可。 * (2)如果n<>k,但n能被k整除,则应打印出k的值,并用n除以k的商,作为新的正整数你n,重复执行第一步。 * (3)如果n不能被k整除,则用k+1作为k的值,重复执行第一步。 */ public class ProgramEx4 { public static void main(String[] args) { int k=90; System.out.print("90="); for (int n=2; n<=k; n++) { if (k%n == 0) { if (n != k) { System.out.print(n+"*"); k = k/n; n = 2; } else { //System.out.print("90="); System.out.print(k); } } } } }

第五题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * * 【程序5】 * 题目:利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下 * 的用C表示。 * 1.程序分析:(a>b)?a:b这是条件运算符的基本例子 */ import java.io.*; public class ProgramEx5 { public static void main(String[] args) throws Exception { int m; BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); m = (int)br.read(); //char a = 'a'; //char b = 'b'; //char c = 'c'; char n = (m<60)?"c":((m>=90)?"a":"b"); System.out.println(n); } }

第六题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序6】 * 题目:输入两个正整数m和n,求其最大公约数和最小公倍数。 * 1.程序分析:利用辗除法。 */ import java.util.Scanner; public class ProgramEx6 { public static void main(String[] args) throws Exception { System.out.print("请输入两个整数"); Scanner scan = new Scanner(System.in); int m = scan.nextInt(); int n = scan.nextInt(); int a, b; if (m<n) { b = m; m = n; n = b; } a = m%n; while(a!=0) { m = n; n = a; a = m%n; m = m*n; } System.out.println("最小公倍数为:"+m); //System.out.println("最大公约数为:"+n); } }

第七题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序7】 * 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 * 1.程序分析:利用while语句,条件为输入的字符不为' '. */ import java.util.Scanner; public class ProgramEx7 { public static void main(String[] args) { System.out.print("请输入一行字符串:"); Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); int m1 = 0; int m2 = 0; int m3 = 0; int m4 = 0; for (int i=0; i<str.length(); i++) { char n = str.charAt(i); if (n == ' ') { m1++; } else if ((n>='a' && n<='z')||(n>='A' && n<='Z')) { m2++; } else if (n>='0' && n<='9') {8 m3++; } else { m4++; } } System.out.println("空格的个数为:"+m1); System.out.println("英文字母的个数为:"+m2); System.out.println("数字的个数为:"+m3); System.out.println("其他字符的个数为:"+m4); } }

第八题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序8】 * 题目:求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加), * 几个数相加有键盘控制。 * 1.程序分析:关键是计算出每一项的值。 */ import java.util.Scanner; public class ProgramEx8 { public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int result = 0; String m = ""; for (int i=1; i<=n; i++) { m = m + 2; int s = Integer.parseInt(m); result = result + s; } System.out.println(result); } }

第九题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序9】 * 题目:一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程 找出1000以内的所有完 * 数。 */ public class ProgramEx9 { public static void main(String[] args) { for (int i=1; i<=1000; i++) { int n = 0; for (int j=1; j<=i/2;j++) { if (i%j == 0) { n = n+j; } } if (i == n) { System.out.println(i); } } } }

第十题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序10】 * 题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多 * 少米?第10次反弹多高? */ public class ProgramEx10 { public static void main(String[] args) { double height = 100; double h = 100; for (int i=1; i<=10; i++) { h = 0.5 * h; height = height + h; } System.out.println("一共经过"+height+""); System.out.println("第十次反弹"+h+""); } }

 
 
回复次数:131

 

#1楼 得分:0回复于:2011-03-21 20:31:14
第十一题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * * 【程序11】 * 题目:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少? * 1.程序分析:可填在百位、十位、个位的数字都是1、2、3、4。 * 组成所有的排列后再去 掉不满足条件的排列。 */ public class ProgramEx11 { public static void main(String[] args) { int sum = 0; int m = 0; for (int a=1; a<=4; a++) { for (int b=1; b<=4; b++) { if (a!=b) { for (int c=1; c<=4; c++) { if (b!=c && a!=c) { m = a*100 + b*10 +c; sum = sum + 1; System.out.print(m+" "); //System.out.print(a+""+b+""+c); } } } } } System.out.println("一共有"+sum+""); } }

第十二题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序12】 * 题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万 * 元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部 * 分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可 * 提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数? * 1.程序分析:请利用数轴来分界,定位。注意定义时需把奖金定义成长整型。 */ import java.util.Scanner; public class ProgramEx12 { public static void main(String[] args) { System.out.print("请输入利润(注:利润>0):"); Scanner scanner = new Scanner(System.in); int m = scanner.nextInt(); long n = 0L; if (m<=10) { n = (long)(0.1 * m); } else if (m<=20) { n = (long)(1 + (m-10)*0.075); } else if (m<=40) { n = (long)(1.75 + (m-20)*0.05); } else if (m<=60) { n = (long)(1.85 + (m-40)*0.03); } else { n = (long)(1.91 + (m-60)*0.01); } System.out.print("所得奖金为:"+n+"万元"); } }

第十三题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序13】 * 题目:一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少? * 1.程序分析:在10万以内判断,先将该数加上100后再开方,再将该数加上268后再开方,如果开方后的结果满足 * 如下条件,即是结果。 */ public class ProgramEx13 { public static void main(String[] args) { long x, y; for (long i=1; i<100000;i++) { x = (long)Math.sqrt(i+100); y = (long)Math.sqrt(i+268); if (x*x == (i+100) && y*y == (i+268)) { System.out.println(i); } } } }

第十四题
Java code
package com.supersoft.exercise; /** * @author JamesLiu * *【程序14】 * 题目:输入某年某月某日,判断这一天是这一年的第几天? * 1.程序分析:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且 * 输入月份大于3时需考虑多加一天。 */ import java.util.Scanner; public class ProgramEx14 { public static void main(String[] args) { System.out.print("请输入年year:"); Scanner scanner = new Scanner(System.in); int y = scanner.nextInt(); System.out.print("请输入月month:"); int m = scanner.nextInt(); System.out.print("请输入日day:"); int d = scanner.nextInt(); int sum = 0; for (int i=1; i<m; i++) { if ((i==4) || (i==6) || (i==9) || (i==11)) { sum = sum + 30; } else if (i==2) { if (((y%4==0 && y%100!=0) || (y%400==0))) { sum = sum + 29; } else { sum = sum + 28; } } else { sum = sum + 31; } } sum = sum + d; System.out.print("这一天是这一年中的第"+sum+""); } }
原文地址:https://www.cnblogs.com/to-creat/p/5600446.html