【程序21】   
题目:求1+2!+3!+…+20!的和

[java] view plain copyprint?
  1. public class lianxi21 {
  2. public static void main(String[] args) {
  3. long sum = 0;
  4. long fac = 1;
  5. for (int i = 1; i <= 20; i++) {
  6. fac = fac * i;
  7. sum += fac;
  8. }
  9. System.out.println(sum);
  10. }
  11. }
public class lianxi21 {public static void main(String[] args) {long sum = 0;long fac = 1;for (int i = 1; i <= 20; i++) {fac = fac * i;sum += fac;}System.out.println(sum);}
}

【程序22】   
题目:利用递归方法求5!。

[java] view plain copyprint?
  1. <pre name=“code” class=“java”>public class lianxi22 {
  2. public static void main(String[] args) {
  3. int n = 5;
  4. rec fr = new rec();
  5. System.out.println(n + ”! = ” + fr.rec(n));
  6. }
  7. }
  8. class rec {
  9. public long rec(int n) {
  10. long value = 0;
  11. if (n == 1) {
  12. value = 1;
  13. } else {
  14. value = n * rec(n - 1);
  15. }
  16. return value;
  17. }
  18. }
<pre name="code" class="java">public class lianxi22 {public static void main(String[] args) {int n = 5;rec fr = new rec();System.out.println(n + "! = " + fr.rec(n));}
}class rec {public long rec(int n) {long value = 0;if (n == 1) {value = 1;} else {value = n * rec(n - 1);}return value;}
}

【程序23】   
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

[java] view plain copyprint?
  1. <pre name=“code” class=“java”>public class lianxi23 {
  2. public static void main(String[] args) {
  3. int age = 10;
  4. for (int i = 2; i <= 5; i++) {
  5. age = age + 2;
  6. }
  7. System.out.println(age);
  8. }
  9. }
<pre name="code" class="java">public class lianxi23 {public static void main(String[] args) {int age = 10;for (int i = 2; i <= 5; i++) {age = age + 2;}System.out.println(age);}
}

【程序24】   
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。   
//使用了长整型最多输入18位

[java] view plain copyprint?
  1. import java.util.*;
  2. public class lianxi24 {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in);
  5. System.out.print(”请输入一个正整数:”);
  6. long a = s.nextLong();
  7. String ss = Long.toString(a);
  8. char[] ch = ss.toCharArray();
  9. int j = ch.length;
  10. System.out.println(a + ”是一个” + j + “位数。”);
  11. System.out.print(”按逆序输出是:”);
  12. for (int i = j - 1; i >= 0; i–) {
  13. System.out.print(ch[i]);
  14. }
  15. }
  16. }
import java.util.*;public class lianxi24 {public static void main(String[] args) {Scanner s = new Scanner(System.in);System.out.print("请输入一个正整数:");long a = s.nextLong();String ss = Long.toString(a);char[] ch = ss.toCharArray();int j = ch.length;System.out.println(a + "是一个" + j + "位数。");System.out.print("按逆序输出是:");for (int i = j - 1; i >= 0; i--) {System.out.print(ch[i]);}}
}

【程序25】   
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。

[java] view plain copyprint?
  1. import java.util.*;
  2. public class lianxi25 {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in);
  5. int a;
  6. do {
  7. System.out.print(”请输入一个5位正整数:”);
  8. a = s.nextInt();
  9. } while (a < 10000 || a > 99999);
  10. String ss = String.valueOf(a);
  11. char[] ch = ss.toCharArray();
  12. if (ch[0] == ch[4] && ch[1] == ch[3]) {
  13. System.out.println(”这是一个回文数”);
  14. } else {
  15. System.out.println(”这不是一个回文数”);
  16. }
  17. }
  18. }
import java.util.*;public class lianxi25 {public static void main(String[] args) {Scanner s = new Scanner(System.in);int a;do {System.out.print("请输入一个5位正整数:");a = s.nextInt();} while (a < 10000 || a > 99999);String ss = String.valueOf(a);char[] ch = ss.toCharArray();if (ch[0] == ch[4] && ch[1] == ch[3]) {System.out.println("这是一个回文数");} else {System.out.println("这不是一个回文数");}}
}
[java] view plain copyprint?
  1. //这个更好,不限位数
  2. import java.util.*;
  3. public class lianxi25a {
  4. public static void main(String[] args) {
  5. Scanner s = new Scanner(System.in);
  6. boolean is = true;
  7. System.out.print(”请输入一个正整数:”);
  8. long a = s.nextLong();
  9. String ss = Long.toString(a);
  10. char[] ch = ss.toCharArray();
  11. int j = ch.length;
  12. for (int i = 0; i < j / 2; i++) {
  13. if (ch[i] != ch[j - i - 1]) {
  14. is = false;
  15. }
  16. }
  17. if (is == true) {
  18. System.out.println(”这是一个回文数”);
  19. } else {
  20. System.out.println(”这不是一个回文数”);
  21. }
  22. }
  23. }
//这个更好,不限位数
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 = s.nextLong();String ss = Long.toString(a);char[] ch = ss.toCharArray();int j = ch.length;for (int i = 0; i < j / 2; i++) {if (ch[i] != ch[j - i - 1]) {is = false;}}if (is == true) {System.out.println("这是一个回文数");} else {System.out.println("这不是一个回文数");}}
}

【程序26】   
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续   判断第二个字母。

[java] view plain copyprint?
  1. import java.util.*;
  2. public class lianxi26 {
  3. public static void main(String[] args) {
  4. getChar tw = new getChar();
  5. System.out.println(”请输入星期的第一个大写字母:”);
  6. char ch = tw.getChar();
  7. switch (ch) {
  8. case ‘M’:
  9. System.out.println(”Monday”);
  10. break;
  11. case ‘W’:
  12. System.out.println(”Wednesday”);
  13. break;
  14. case ‘F’:
  15. System.out.println(”Friday”);
  16. break;
  17. case ‘T’: {
  18. System.out.println(”请输入星期的第二个字母:”);
  19. char ch2 = tw.getChar();
  20. if (ch2 == ‘U’) {
  21. System.out.println(”Tuesday”);
  22. } else if (ch2 == ‘H’) {
  23. System.out.println(”Thursday”);
  24. } else {
  25. System.out.println(”无此写法!”);
  26. }
  27. }
  28. ;
  29. break;
  30. case ‘S’: {
  31. System.out.println(”请输入星期的第二个字母:”);
  32. char ch2 = tw.getChar();
  33. if (ch2 == ‘U’) {
  34. System.out.println(”Sunday”);
  35. } else if (ch2 == ‘A’) {
  36. System.out.println(”Saturday”);
  37. } else {
  38. System.out.println(”无此写法!”);
  39. }
  40. }
  41. ;
  42. break;
  43. default:
  44. System.out.println(”无此写法!”);
  45. }
  46. }
  47. }
  48. class getChar {
  49. public char getChar() {
  50. Scanner s = new Scanner(System.in);
  51. String str = s.nextLine();
  52. char ch = str.charAt(0);
  53. if (ch < ‘A’ || ch > ‘Z’) {
  54. System.out.println(”输入错误,请重新输入”);
  55. ch = getChar();
  56. }
  57. return ch;
  58. }
  59. }
import java.util.*;public class lianxi26 {public static void main(String[] args) {getChar tw = new getChar();System.out.println("请输入星期的第一个大写字母:");char ch = tw.getChar();switch (ch) {case 'M':System.out.println("Monday");break;case 'W':System.out.println("Wednesday");break;case 'F':System.out.println("Friday");break;case 'T': {System.out.println("请输入星期的第二个字母:");char ch2 = tw.getChar();if (ch2 == 'U') {System.out.println("Tuesday");} else if (ch2 == 'H') {System.out.println("Thursday");} else {System.out.println("无此写法!");}};break;case 'S': {System.out.println("请输入星期的第二个字母:");char ch2 = tw.getChar();if (ch2 == 'U') {System.out.println("Sunday");} else if (ch2 == 'A') {System.out.println("Saturday");} else {System.out.println("无此写法!");}};break;default:System.out.println("无此写法!");}}
}class getChar {public char getChar() {Scanner s = new Scanner(System.in);String str = s.nextLine();char ch = str.charAt(0);if (ch < 'A' || ch > 'Z') {System.out.println("输入错误,请重新输入");ch = getChar();}return ch;}
}

【程序27】   
题目:求100之内的素数   
//使用除sqrt(n)的方法求出的素数不包括2和3

[java] view plain copyprint?
  1. public class lianxi27 {
  2. public static void main(String[] args) {
  3. boolean b = false;
  4. System.out.print(2 + “ ”);
  5. System.out.print(3 + “ ”);
  6. for (int i = 3; i < 100; i += 2) {
  7. for (int j = 2; j <= Math.sqrt(i); j++) {
  8. if (i % j == 0) {
  9. b = false;
  10. break;
  11. } else {
  12. b = true;
  13. }
  14. }
  15. if (b == true) {
  16. System.out.print(i + ” ”);
  17. }
  18. }
  19. }
  20. }
  21. // 该程序使用除1位素数得2位方法,运行效率高通用性差。
  22. public class lianxi27a {
  23. public static void main(String[] args) {
  24. int[] a = new int[] { 2, 3, 5, 7 };
  25. for (int j = 0; j < 4; j++)
  26. System.out.print(a[j] + ” ”);
  27. boolean b = false;
  28. for (int i = 11; i < 100; i += 2) {
  29. for (int j = 0; j < 4; j++) {
  30. if (i % a[j] == 0) {
  31. b = false;
  32. break;
  33. } else {
  34. b = true;
  35. }
  36. }
  37. if (b == true) {
  38. System.out.print(i + ” ”);
  39. }
  40. }
  41. }
  42. }
public class lianxi27 {public static void main(String[] args) {boolean b = false;System.out.print(2 + " ");System.out.print(3 + " ");for (int i = 3; i < 100; i += 2) {for (int j = 2; j <= Math.sqrt(i); j++) {if (i % j == 0) {b = false;break;} else {b = true;}}if (b == true) {System.out.print(i + " ");}}}
}// 该程序使用除1位素数得2位方法,运行效率高通用性差。
public class lianxi27a {public static void main(String[] args) {int[] a = new int[] { 2, 3, 5, 7 };for (int j = 0; j < 4; j++)System.out.print(a[j] + " ");boolean b = false;for (int i = 11; i < 100; i += 2) {for (int j = 0; j < 4; j++) {if (i % a[j] == 0) {b = false;break;} else {b = true;}}if (b == true) {System.out.print(i + " ");}}}
}

【程序28】   
题目:对10个数进行排序

[java] view plain copyprint?
  1. import java.util.*;
  2. public class lianxi28 {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in);
  5. int[] a = new int[10];
  6. System.out.println(”请输入10个整数:”);
  7. for (int i = 0; i < 10; i++) {
  8. a[i] = s.nextInt();
  9. }
  10. for (int i = 0; i < 10; i++) {
  11. for (int j = i + 1; j < 10; j++) {
  12. if (a[i] > a[j]) {
  13. int t = a[i];
  14. a[i] = a[j];
  15. a[j] = t;
  16. }
  17. }
  18. }
  19. for (int i = 0; i < 10; i++) {
  20. System.out.print(a[i] + ” ”);
  21. }
  22. }
  23. }
import java.util.*;public class lianxi28 {public static void main(String[] args) {Scanner s = new Scanner(System.in);int[] a = new int[10];System.out.println("请输入10个整数:");for (int i = 0; i < 10; i++) {a[i] = s.nextInt();}for (int i = 0; i < 10; i++) {for (int j = i + 1; j < 10; j++) {if (a[i] > a[j]) {int t = a[i];a[i] = a[j];a[j] = t;}}}for (int i = 0; i < 10; i++) {System.out.print(a[i] + " ");}}
}

【程序29】   
题目:求一个3*3矩阵对角线元素之和

[java] view plain copyprint?
  1. import java.util.*;
  2. public class lianxi29 {
  3. public static void main(String[] args) {
  4. Scanner s = new Scanner(System.in);
  5. int[][] a = new int[3][3];
  6. System.out.println(”请输入9个整数:”);
  7. for (int i = 0; i < 3; i++) {
  8. for (int j = 0; j < 3; j++) {
  9. a[i][j] = s.nextInt();
  10. }
  11. }
  12. System.out.println(”输入的3 * 3 矩阵是:”);
  13. for (int i = 0; i < 3; i++) {
  14. for (int j = 0; j < 3; j++) {
  15. System.out.print(a[i][j] + ” ”);
  16. }
  17. System.out.println();
  18. }
  19. int sum = 0;
  20. for (int i = 0; i < 3; i++) {
  21. for (int j = 0; j < 3; j++) {
  22. if (i == j) {
  23. sum += a[i][j];
  24. }
  25. }
  26. }
  27. System.out.println(”对角线之和是:” + sum);
  28. }
  29. }
import java.util.*;public class lianxi29 {public static void main(String[] args) {Scanner s = new Scanner(System.in);int[][] a = new int[3][3];System.out.println("请输入9个整数:");for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {a[i][j] = s.nextInt();}}System.out.println("输入的3 * 3 矩阵是:");for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {System.out.print(a[i][j] + " ");}System.out.println();}int sum = 0;for (int i = 0; i < 3; i++) {for (int j = 0; j < 3; j++) {if (i == j) {sum += a[i][j];}}}System.out.println("对角线之和是:" + sum);}
}

【程序30】   
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。    
//此程序不好,没有使用折半查找插入

[java] view plain copyprint?
  1. import java.util.*;
  2. public class lianxi30 {
  3. public static void main(String[] args) {
  4. int[] a = new int[] { 1, 2, 6, 14, 25, 36, 37, 55 };
  5. int[] b = new int[a.length + 1];
  6. int t1 = 0, t2 = 0;
  7. int i = 0;
  8. Scanner s = new Scanner(System.in);
  9. System.out.print(”请输入一个整数:”);
  10. int num = s.nextInt();
  11. if (num >= a[a.length - 1]) {
  12. b[b.length - 1] = num;
  13. for (i = 0; i < a.length; i++) {
  14. b[i] = a[i];
  15. }
  16. } else {
  17. for (i = 0; i < a.length; i++) {
  18. if (num >= a[i]) {
  19. b[i] = a[i];
  20. } else {
  21. b[i] = num;
  22. break;
  23. }
  24. }
  25. for (int j = i + 1; j < b.length; j++) {
  26. b[j] = a[j - 1];
  27. }
  28. }
  29. for (i = 0; i < b.length; i++) {
  30. System.out.print(b[i] + ” ”);
  31. }
  32. }
  33. }
import java.util.*;public class lianxi30 {public static void main(String[] args) {int[] a = new int[] { 1, 2, 6, 14, 25, 36, 37, 55 };int[] b = new int[a.length + 1];int t1 = 0, t2 = 0;int i = 0;Scanner s = new Scanner(System.in);System.out.print("请输入一个整数:");int num = s.nextInt();if (num >= a[a.length - 1]) {b[b.length - 1] = num;for (i = 0; i < a.length; i++) {b[i] = a[i];}} else {for (i = 0; i < a.length; i++) {if (num >= a[i]) {b[i] = a[i];} else {b[i] = num;break;}}for (int j = i + 1; j < b.length; j++) {b[j] = a[j - 1];}}for (i = 0; i < b.length; i++) {System.out.print(b[i] + " ");}}
}

Java经典基础练习21-30相关推荐

  1. Java经典基础与高级面试36题和答案

    在Java面试的首轮,经常会问很多关于Java面试基础以及高级的问题,今天收集相关Java面试36题和答案分享出来. 1."static"关键字是什么意思?Java中是否可以覆盖( ...

  2. java由大到小输出整数xvz_【视频+图文】Java经典基础练习题(三):输入3个整数,并将其由小到大输出...

    java经典实例书店书畅想畅销书 109.6元 包邮 (需用券) 去购买 > 目录https://www.cnblogs.com/Qpgshare/p/12588923.html一.视频讲解 h ...

  3. 牛客网---Java题库(21~30)

    21.lterator和Listlterator的区别是什么? literator可用来遍历Set和List集合,但是Listlterator只能用来遍历List. Iterator对集合只能是前向遍 ...

  4. java判断五位数回文数_【视频+图文】Java经典基础练习题(五):键盘输入一个五位数,判断这个数是否为回文数...

    能解决题目的代码并不是一次就可以写好的 我们需要根据我们的思路写出后通过debug模式找到不足再进行更改 多次测试后才可得到能解决题目的代码! 通过学习,练习[Java基础经典练习题],让我们一起来培 ...

  5. Java经典基础面试题

    有时候面试,你不知道面试官会问什么问题,觉得有水平的面试官,应该会问你比较深奥的题,但是你也不要抱有这样的心理,有的面试官,喜欢不按常理出牌,会问你比较简单得问题,今天小编就为大家整理了一份,比较基础 ...

  6. java定义猴子类的题_【视频+图文】Java经典基础练习题(六):猴子吃桃子问题...

    目录 一.具体题目 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天 早上又将剩下的桃子吃掉一半 ,又多吃了一个 . 以后每天早上都吃了前一天剩下的一半零一个. 到第10天早上想再 ...

  7. 【图文分析】Java经典基础练习题(六):猴子吃桃子问题

    文章目录 一.具体题目 二.思路分析(逆向思维) 三.代码+结果 代码: 结果: 四.彩蛋 一.具体题目 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个  第二天 早上又将剩下的桃子吃掉 ...

  8. Java语言基础案例2-1 商品入库

    现要对华为和小米两种手机产品进行入库,本案例要求编写一个模拟商品入库的程序,可以在控制台输入入库商品的数量,最后打印出仓库中所有商品详细信息以及所有商品的总库存数和库存商品总金额. 商品信息如下: 品 ...

  9. java基础案例教程第二章_第2章Java编程基础_补充案例教程.doc

    第2章Java编程基础_补充案例教程 博学谷--让IT教学更简单,让IT学习更有效博学谷--让IT教学更简单,让IT学习更有效 PAGE \* MERGEFORMAT34PAGE \* MERGEFO ...

最新文章

  1. c# html文本编辑器,C#实现简单文本编辑器
  2. 将表里的数据批量生成INSERT语句的存储过程 增强版
  3. java 32位jdk_jdk9 32位下载 jdk9.0(Java SE Development Kit 9) v9.0.4 官方版 32位 下载-脚本之家...
  4. 用好这几个工具,能大幅提升你的 Git/GitHub 操作效率!
  5. Kali Linux 无线渗透测试入门指南 翻译完成!
  6. 【PostgreSQL-9.6.3】表继承
  7. 支付宝玉伯:我心目中的优秀API
  8. 文本数据增强(data augmentation)nlpaug使用
  9. Ubuntu安装最新版nodejs
  10. 企业如何用好云、管好云?
  11. Linux 五个最牛视频编辑软件
  12. 关于信息墒与压缩编码基础的学习
  13. 2021年中国物流仓储系统集成商竞争力排行TOP20
  14. 金融python入门书籍推荐_学习金融工程,有哪些推荐的入门书籍?
  15. LeetCode 全站第一,牛逼!
  16. 使用blender和mmd模型进行3D辅助绘图
  17. java web实验_javaweb实验报告
  18. Mendix与JEECG对比
  19. BootDo:修改启动时的象形文字
  20. 云主机上手教程:轻量应用服务器体验

热门文章

  1. 按复杂度有效性递减排序_十大经典排序算法:python源码实现,通俗深入讲解
  2. 从“惊群”的现象来看并发锁,“死锁”问题的解决方案丨Redis单线程|共享内存|无锁实现|原子操作CAS
  3. 揭秘《双11星秀猫巅峰时刻》功能设计
  4. 数据库(二)表的设计
  5. 链表操作——两数相加
  6. 理解MySQL——索引与优化篇
  7. 【客户案例】母婴行业头部品牌是如何做私域的
  8. 系统工程理论与实践投稿经验_畜牧期刊发表服务最快最好网站_【100%录用和发表、万佳论文网】...
  9. UML状态图画法参考博文整理
  10. 如何使用沙箱测试单笔转账到支付宝账号(php版)