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语句,条件为输入的字符不为'\n'. */ 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 + " " ); } }

 
 
style="HEIGHT: 130px" id="tad2" height="0" marginheight="0" src="/u/t5/include/ad2.asp?pdate=2011-03-21 20:25:35&ba=Java&sa=J2SE" frameborder="0" width="100%" marginwidth="0" scrolling="no">
id="Iframe1" height="0" marginheight="0" src="/u/t5/include/ad3.asp?pdate=2011-03-21 20:25:35&ba=Java&sa=J2SE" frameborder="0" width="100%" marginwidth="0" scrolling="no">

 

#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 + " " ); } } /* import java.util.Scanner; public class ProgramEx14 { public static boolean isValidate(int y, int m, int d, int[] monthOfDays) { if (y<=0) { return false; } if (m<=0 || m>12) { return false; } if (d<=0 || d>monthOfDays[m-1]) { return false; } return true; } 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[] monthOfDays = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (((y%4==0 && y%100!=0) || (y%400==0))) { monthOfDays[1] = 29; } if (isValidate(y, m, d, monthOfDays) == false) { System.out.println(""); return; } int sum = 0; for (int i=0; i<m-1; i++) { sum = sum + monthOfDays[i]; } sum = sum + d; System.out.println("这一天是这一年的第"+sum+"天"); } } */


第十五题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序15】 * 题目:输入三个整数x,y,z,请把这三个数由小到大输出。 * 1.程序分析:我们想办法把最小的数放到x上,先将x与y进行比较,如果x>y则将x与y的值进行交换,然后再用x * 与z进行比较,如果x>z则将x与z的值进行交换,这样能使x最小。 */ import java.util.Scanner; public class ProgramEx15 { public static void main(String[] args) { System.out.println( " 请输入三个整数x,y,z: " ); Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); int y = scanner.nextInt(); int z = scanner.nextInt(); if (x > y) { int m = x; x = y; y = m; } if (x > z) { int p = x; x = z; z = p; } if (y > z) { int n = y; y = z; z = n; } System.out.print(x + " , " + y + " , " + z); } }

第十六题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序16】 * 题目:输出9*9口诀。 * 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。 */ public class ProgramEx16 { public static void main(String[] args) { for ( int i = 1 ; i <= 9 ; i ++ ) { for ( int j = 1 ; j <= i; j ++ ) { System.out.print( " " + i + " * " + j + " = " + i * j + ' \t ' ); } System.out.println(); } } }

第十七题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序17】 * 题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩 * 下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下 * 的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。 * 1.程序分析:采取逆向思维的方法,从后往前推断。 */ public class ProgramEx17 { public static void main(String[] args) { int m = 1 ; for ( int i = 1 ; i < 10 ; i ++ ) { m = (m + 1 ) * 2 ; } System.out.print( " 第一天共摘了 " + m + " 只桃子 " ); } }

第十八题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序18】 * 题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向 * 队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。 * 1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除, 则表明此数不是素数,反 * 之是素数。 */ public class ProgramEx18 { public static void main(String[] args) { String[] m = { " a " , " b " , " c " }; String[] n = { " x " , " y " , " z " }; for ( int i = 0 ; i < 3 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { if ( ! (i == 0 && j == 0 ) && ! (i == 2 && (j == 1 || j == 2 ))) { System.out.println(m[i] + " " + n[j] + " 比赛 " ); } } } } }

第十九题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序19】 * 题目:打印出如下图案(菱形) * * * *** * ***** * ******* * ***** * *** * * * 1.程序分析:先把图形分成两部分来看待,前四行一个规律,后三行一个规律,利用双重 for循环,第一层控制 * 行,第二层控制列。 * public class ProgramEx19 { public static void main(String[] args) { for (int i=1; i<=4; i++) { for (int j=1; j<=4-i; j++) { System.out.print(" "); } for (int j=1; j<=2*i-1; j++) { System.out.print("*"); } System.out.println(); } for (int i=3; i>=1; i--) { for (int j=1; j<=4-i; j++) { System.out.print(" "); } for (int j=1; j<=2*i-1; j++) { System.out.print("*"); } System.out.println(); } } } */ public class ProgramEx19 { public static void main(String[] args) { int n = 0 ; for ( int i = 0 ; i < 7 ; i ++ ) { for ( int j = 0 ; j < 4 + n; j ++ ) { if (j < 3 - i || j < i - 3 ) { System.out.print( " " ); } else { System.out.print( " * " ); } } if (i < 3 ) { n ++ ; } else { n -- ; } System.out.println(); } } }

第二十题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序20】 * 题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。 * 1.程序分析:请抓住分子与分母的变化规律。 */ public class ProgramEx20 { public static void main(String[] args) { double result = 0 ; double a = 2.0 ; double b = 1.0 ; for ( int i = 1 ; i <= 20 ; i ++ ) { result = result + a / b; double m = b; b = a; a = a + m; } System.out.print(result); } }
 
 

 

 

 
第二十一题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序21】 * 题目:求1+2!+3!+...+20!的和 */ public class ProgramEx21 { public static void main(String[] args) { int result = 0 ; int m = 1 ; for ( int i = 1 ; i <= 20 ; i ++ ) { m = m * i; result = result + m; } System.out.print(result); } }

第二十二题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序22】 题目:利用递归方法求5!。 1.程序分析:递归公式:fn=fn_1*4! * public class ProgramEx22 { public static void main(String[] args) { int m = 1; for (int i=1; i<=5; i++) { m = m * i; } System.out.print(m); } } */ public class ProgramEx22 { public static void main(String[] args) { System.out.print(f( 5 )); } public static int f( int n) { if (n == 0 ) { return 1 ; } return n * f(n - 1 ); } }

第二十三题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序23】 * 题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问 * 第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个 * 人多大? * 1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数, * 依次类推,推到第一人(10岁),再往回推。 */ public class ProgramEx23 { public static void main(String[] args) { System.out.print(f( 5 )); } public static int f( int n) { if (n == 1 ) { return 10 ; } return f(n - 1 ) + 2 ; } }

第二十四题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序24】 * 题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。 * import java.util.Scanner; public class ProgramEx24 { public static void main(String[] args) { System.out.print("请输入一个不多于五位的正整数:"); Scanner scanner = new Scanner(System.in); String str = scanner.next(); String[] strNum = str.split(""); int length = strNum.length - 1; System.out.println("长度为:"+length); System.out.print("逆序输出:"); for (int i=length-1; i>=0; i--) { String m = strNum[i]; strNum[i] = strNum[length-i]; strNum[length-i] = m; System.out.print(str.charAt(i)); } } } */ import java.util.Scanner; public class ProgramEx24 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Integer number = scanner.nextInt(); int length = number.toString().length(); System.out.print( " 该整数是 " + length + " 位数 " ); for ( int i = length - 1 ; i >= 0 ; i -- ) { System.out.print(number.toString().charAt(i)); } } }

第二十五题
Java code
            
            
package com.supersoft.exercise; /** * @author JamesLiu * *【程序25】 * 题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。 * import java.util.Scanner; public class ProgramEx25 { public static void main(String[] args) { System.out.print("请输入一个五位数"); Scanner scanner = new Scanner(System.in); int a = scanner.nextInt(); int m1 = a/10000; int m2 = a%10; int n1 = a%10000/1000; int n2 = a%100/10; if (m1==m2 && n1==n1) { System.out.print("是回文字符串"); } else { System.out.print("不是回文字符串"); } } } */ import java.util.Scanner; public class ProgramEx25 { public static void main(String[] args) { System.out.print( " 请输入一个五位数 " ); Scanner scanner = new Scanner(System.in); String str = scanner.next(); int sum = 0 ; for ( int i = 0 ; i < str.length() / 2 ; i ++ ) { if (str.charAt(i) == str.charAt(str.length() - 1 - i)) { sum = sum + 1 ; } } if (sum == 2 ) { System.out.print( " 是回文字符串 " ); } else { System.out.print( " 不是回文字符串 " ); } } }
  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值