java初学者48个小例子

上传人:第*** 文档编号:34051277 上传时间:2018-02-20 格式:DOC 页数:31 大小:101KB
返回 下载 相关 举报
java初学者48个小例子_第1页
第1页 / 共31页
java初学者48个小例子_第2页
第2页 / 共31页
java初学者48个小例子_第3页
第3页 / 共31页
java初学者48个小例子_第4页
第4页 / 共31页
java初学者48个小例子_第5页
第5页 / 共31页
点击查看更多>>
资源描述

《java初学者48个小例子》由会员分享,可在线阅读,更多相关《java初学者48个小例子(31页珍藏版)》请在金锄头文库上搜索。

1、【程序 1】 题目:古典问题:有一对兔子,从出生后第 3 个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少? /这是一个菲波拉契数列问题public class lianxi01 public static void main(String args) System.out.println(第 1 个月的兔子对数: 1);System.out.println(第 2 个月的兔子对数: 1);int f1 = 1, f2 = 1, f, M=24;for(int i=3; i =90 分的同学用 A 表示,60-89 分之间的用 B 表示

2、,60 分以下的用 C 表示。 import java.util.*;public class lianxi05 public static void main(String args) int x;char grade;Scanner s = new Scanner(System.in);System.out.print( 请输入一个成绩: ); x = s.nextInt(); grade = x = 90 ? A: x = 60 ? B:C;System.out.println(等级为:+grade); 【程序 6】 题目:输入两个正整数 m 和 n,求其最大公约数和最小公倍数。 /*在

3、循环中,只要除数不等于 0,用较大数除以较小的数,将小的一个数作为下一轮循环的大数,取得的余数作为下一轮循环的较小的数,如此循环直到较小的数的值为 0,返回较大的数,此数即为最大公约数,最小公倍数为两数之积除以最大公约数。* /import java.util.*;public class lianxi06 public static void main(String args) int a ,b,m;Scanner s = new Scanner(System.in);System.out.print( 键入一个整数: ); a = s.nextInt();System.out.print(

4、 再键入一个整数: ); b = s.nextInt();deff cd = new deff();m = cd.deff(a,b);int n = a * b / m;System.out.println(最大公约数: + m);System.out.println(最小公倍数: + n); class deffpublic int deff(int x, int y) int t;if(x = 0 & ch = a & ch A & ch 0 & x 10 & x 20 & x 40 & x 60 & x 100) y = 20 * 0.175 + 40 * 0.08 + 40 * 0.0

5、15 + (x - 100) * 0.01;System.out.println(应该提取的奖金是 + y + 万);【程序 13】 题目:一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少? public class lianxi13 public static void main(String args) for(int x =1; x 12 | day 31) System.out.println(输入错误,请重新输入!);e=1 ; while( e=1);for (int i=1; i y) int t = x;x = y;y = t;i

6、f(x z) int t = x;x = z;z = t;if(y z) int t = y;y = z;z = t;System.out.println( 三个数字由小到大排列为: +x + + y + + z);class inputpublic int input() int value = 0;Scanner s = new Scanner(System.in);value = s.nextInt();return value; 【程序 16】题目:输出 9*9 口诀。 public class lianxi16 public static void main(String args)

7、 for(int i=1; i=0; i-) System.out.print(chi);【程序 25】 题目:一个 5 位数,判断它是不是回文数。即 12321 是回文数,个位与万位相同,十位与千位相同。 import java.util.*;public class lianxi25 public static void main(String args) Scanner s = new Scanner(System.in);int a;doSystem.out.print(请输入一个 5 位正整数:);a = s.nextInt();while(a99999);String ss =St

8、ring.valueOf(a);char ch = ss.toCharArray();if(ch0=ch4&ch1=ch3)System.out.println(这是一个回文数);else System.out.println(这不是一个回文数); /这个更好,不限位数import java.util.*;public class lianxi25a public static void main(String args) Scanner s = new Scanner(System.in);boolean is =true;System.out.print(请输入一个正整数:);long a

9、 = s.nextLong();String ss = Long.toString(a);char ch = ss.toCharArray();int j=ch.length;for(int i=0; iZ) System.out.println(输入错误,请重新输入);ch=getChar();return ch; 【程序 27】 题目:求 100 之内的素数 /使用除 sqrt(n)的方法求出的素数不包括 2 和 3public class lianxi27 public static void main(String args) boolean b =false;System.out.p

10、rint(2 + );System.out.print(3 + );for(int i=3; i aj) int t = ai;ai = aj;aj = t;for(int i=0; i= aa.length-1) bb.length-1 = num;for(i=0; i= ai) bi = ai; else bi = num;break;for(int j=i+1; j=0; j=j-1) System.out.print(aj+ );【程序 32】 题目:取一个整数 a 从右端开始的 47 位。 import java.util.*;public class lianxi32 public

11、 static void main(String args) Scanner s = new Scanner(System.in);System.out.print(请输入一个 7 位以上的正整数:);long a = s.nextLong();String ss = Long.toString(a);char ch = ss.toCharArray();int j=ch.length;if (j max) max = ai;idx1 = i; if(ai =m; i-) ai = ai-m;for(int i=0; i 1) if(arrindex = true) countNum +; i

12、f(countNum = 3) countNum =0;arrindex = false;leftCount -;index +;if(index = n) index = 0;for(int i=0; i s2.charAt(i) result = false;break; else if(s1.charAt(i) =100)System.out.println (809*+i+=+800*+i+9*+i+=+b); 【程序 43】 题目:求 07 所能组成的奇数个数。 /组成 1 位数是 4 个。/组成 2 位数是 7*4 个。/组成 3 位数是 7*8*4 个。/组成 4 位数是 7*8

13、*8*4 个。/.public class lianxi43 public static void main (String args) int sum=4;int j;System.out.println(组成 1 位数是 +sum+ 个);sum=sum*7;System.out.println(组成 2 位数是 +sum+ 个);for(j=3;j=6 偶数,不是,重新输入fun fc = new fun();for(i=2;i=6 偶数,不是,重新输入for(int i=3;i50);for(int i=1;i9999); int a=new int4; a0 = num/1000;

14、/取千位的数字 a1 = (num/100)%10; /取百位的数字 a2 = (num/10)%10; /取十位的数字 a3 = num%10; /取个位的数字 for(int j=0;j4;j+) aj+=5; aj%=10; for(int j=0;j=1;j+) temp = aj; aj = a3-j; a3-j =temp; System.out.print(加密后的数字为:); for(int j=0;j4;j+) System.out.print(aj); 【程序 49】 题目:计算字符串中子串出现的次数 import java.util.*;public class lianxi49 public static void main(String args)Scanner s = new Scanner(System.in);System.out.print(请输入字符串:);String str1 = s.nextLine();System.out.print(请输入子串:);String str2 = s.nextLine();int count=0; if(str1.equals()|str2.equals() System.out.println(你没有输入字符串或子串,无法比较!); System.exit(0); else for(int i=

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号