Java经典基础练习21-30
【程序21】
题目:求1+2!+3!+…+20!的和
- 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);
- }
- }
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!。
- <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;
- }
- }
<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岁。请问第五个人多大?
- <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);
- }
- }
<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位
- 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]);
- }
- }
- }
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是回文数,个位与万位相同,十位与千位相同。
- 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(”这不是一个回文数”);
- }
- }
- }
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("这不是一个回文数");}}
}
- //这个更好,不限位数
- 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(”这不是一个回文数”);
- }
- }
- }
//这个更好,不限位数
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】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续 判断第二个字母。
- 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;
- }
- }
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
- 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 + ” ”);
- }
- }
- }
- }
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个数进行排序
- 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] + ” ”);
- }
- }
- }
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矩阵对角线元素之和
- 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);
- }
- }
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】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
//此程序不好,没有使用折半查找插入
- 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] + ” ”);
- }
- }
- }
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相关推荐
- Java经典基础与高级面试36题和答案
在Java面试的首轮,经常会问很多关于Java面试基础以及高级的问题,今天收集相关Java面试36题和答案分享出来. 1."static"关键字是什么意思?Java中是否可以覆盖( ...
- java由大到小输出整数xvz_【视频+图文】Java经典基础练习题(三):输入3个整数,并将其由小到大输出...
java经典实例书店书畅想畅销书 109.6元 包邮 (需用券) 去购买 > 目录https://www.cnblogs.com/Qpgshare/p/12588923.html一.视频讲解 h ...
- 牛客网---Java题库(21~30)
21.lterator和Listlterator的区别是什么? literator可用来遍历Set和List集合,但是Listlterator只能用来遍历List. Iterator对集合只能是前向遍 ...
- java判断五位数回文数_【视频+图文】Java经典基础练习题(五):键盘输入一个五位数,判断这个数是否为回文数...
能解决题目的代码并不是一次就可以写好的 我们需要根据我们的思路写出后通过debug模式找到不足再进行更改 多次测试后才可得到能解决题目的代码! 通过学习,练习[Java基础经典练习题],让我们一起来培 ...
- Java经典基础面试题
有时候面试,你不知道面试官会问什么问题,觉得有水平的面试官,应该会问你比较深奥的题,但是你也不要抱有这样的心理,有的面试官,喜欢不按常理出牌,会问你比较简单得问题,今天小编就为大家整理了一份,比较基础 ...
- java定义猴子类的题_【视频+图文】Java经典基础练习题(六):猴子吃桃子问题...
目录 一.具体题目 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天 早上又将剩下的桃子吃掉一半 ,又多吃了一个 . 以后每天早上都吃了前一天剩下的一半零一个. 到第10天早上想再 ...
- 【图文分析】Java经典基础练习题(六):猴子吃桃子问题
文章目录 一.具体题目 二.思路分析(逆向思维) 三.代码+结果 代码: 结果: 四.彩蛋 一.具体题目 猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天 早上又将剩下的桃子吃掉 ...
- Java语言基础案例2-1 商品入库
现要对华为和小米两种手机产品进行入库,本案例要求编写一个模拟商品入库的程序,可以在控制台输入入库商品的数量,最后打印出仓库中所有商品详细信息以及所有商品的总库存数和库存商品总金额. 商品信息如下: 品 ...
- java基础案例教程第二章_第2章Java编程基础_补充案例教程.doc
第2章Java编程基础_补充案例教程 博学谷--让IT教学更简单,让IT学习更有效博学谷--让IT教学更简单,让IT学习更有效 PAGE \* MERGEFORMAT34PAGE \* MERGEFO ...
最新文章
- c# html文本编辑器,C#实现简单文本编辑器
- 将表里的数据批量生成INSERT语句的存储过程 增强版
- java 32位jdk_jdk9 32位下载 jdk9.0(Java SE Development Kit 9) v9.0.4 官方版 32位 下载-脚本之家...
- 用好这几个工具,能大幅提升你的 Git/GitHub 操作效率!
- Kali Linux 无线渗透测试入门指南 翻译完成!
- 【PostgreSQL-9.6.3】表继承
- 支付宝玉伯:我心目中的优秀API
- 文本数据增强(data augmentation)nlpaug使用
- Ubuntu安装最新版nodejs
- 企业如何用好云、管好云?
- Linux 五个最牛视频编辑软件
- 关于信息墒与压缩编码基础的学习
- 2021年中国物流仓储系统集成商竞争力排行TOP20
- 金融python入门书籍推荐_学习金融工程,有哪些推荐的入门书籍?
- LeetCode 全站第一,牛逼!
- 使用blender和mmd模型进行3D辅助绘图
- java web实验_javaweb实验报告
- Mendix与JEECG对比
- BootDo:修改启动时的象形文字
- 云主机上手教程:轻量应用服务器体验
热门文章
- 按复杂度有效性递减排序_十大经典排序算法:python源码实现,通俗深入讲解
- 从“惊群”的现象来看并发锁,“死锁”问题的解决方案丨Redis单线程|共享内存|无锁实现|原子操作CAS
- 揭秘《双11星秀猫巅峰时刻》功能设计
- 数据库(二)表的设计
- 链表操作——两数相加
- 理解MySQL——索引与优化篇
- 【客户案例】母婴行业头部品牌是如何做私域的
- 系统工程理论与实践投稿经验_畜牧期刊发表服务最快最好网站_【100%录用和发表、万佳论文网】...
- UML状态图画法参考博文整理
- 如何使用沙箱测试单笔转账到支付宝账号(php版)