Java基础题新手练习(二)
作者:保护眼睛 时间:2022-03-10 00:11:57
数字9 出现的次数
编写程序数一下 1到 100 的所有整数中出现多少个数字9
源码
public static int Getnum(){
int count=0;
for(int i=1;i<=100;i++){
if (i % 10 == 9)
{ count++;
}
if (i / 10 == 9)
{ count++;
}
}
return count;
}
运行结果:
输出闰年
输出 1000 - 2000 之间所有的闰年
源码
public static void SoutLeapyear(){
for(int year=1000;year<=2000;year++)
if(year%100!=0&&year%4==0||year%400==0){
System.out.println(year+"是闰年");
}
}
运行结果:
打印素数
打印 1 - 100 之间所有的素数
源码
public static void PrintPrimeNum(){
for (int i = 2; i < 100; i++) {
int j;
for (j = 2; j < (int) (Math.sqrt(i) + 1); j++) {
if (i % j == 0) {break;
}
}
if (j > (int) Math.sqrt(i)) {
System.out.print(i + " ");
}
}
}
运行结果:
判定素数
给定一个数字,判定一个数字是否是素数
源码
public static void PrintPrimeNum(){
for (int i = 2; i < 100; i++) {
int j;
for (j = 2; j < (int) (Math.sqrt(i) + 1); j++) {
if (i % j == 0) {
break;
}
}
if (j > (int) Math.sqrt(i)) {
System.out.print(i + " ");
}
}
}
运行结果:
年龄打印
根据输入的年龄, 来打印出当前年龄的人是少年(低于18), 青年(19-28), 中年(29-55), 老年(56以上)
源码
public static void JudgeAge(){
Scanner scanner =new Scanner(System.in);
int age = scanner.nextInt();
if(age<18)
System.out.println("是少年");
else if(age>=19&&age<=29)
System.out.println("是青年");
else if(age>=20&&age<=55)
System.out.println("是中年");
else if(age>=56&&age<=100)
System.out.println("是老年");
elseSystem.out.println("输入有误");
}
运行结果:
打印 X 图形
KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的X形图案。
源码
public static void PrintX(){
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i=1;i<=num;i++){
for(int j=1;j<=num;j++){
if((i==j) || (i+j==num+1))
System.out.print("x");
else{
System.out.print(" ");
}
}
System.out.println();
}
}
运行结果:
猜数字游戏
完成猜数字游戏 ,用户输入数字,判断该数字是大于,小于,还是等于随机生成的数字,等于的时候退出程序。
源码
public static void guessNumber(){
Scanner scanner = new Scanner(System.in);
Random random = new Random();//用来生成随机数
int randNum = random.nextInt(100);
while (true) {
System.out.println("请输入你要猜的数字:");
int num = scanner.nextInt();
if(num < randNum) {
System.out.println("小了");
}else if(num == randNum) {
System.out.println("猜对了");
break;
}else {
System.out.println("大了!");
}
}
}
运行结果:
来源:https://blog.csdn.net/qq_45859087/article/details/115470432
标签:Java,基础,练习
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JDK1.8安装与配置超详细教程
2023-09-20 16:17:50
![](https://img.aspxhome.com/file/2023/9/62809_0s.jpg)
mybatis-plus 新增/修改如何实现自动填充指定字段
2023-11-28 22:20:53
![](https://img.aspxhome.com/file/2023/3/60593_0s.jpg)
java必学必会之线程(2)
2023-11-09 10:22:35
![](https://img.aspxhome.com/file/2023/2/58742_0s.png)
Spring Cache简单介绍和使用大全
2023-11-25 04:46:46
![](https://img.aspxhome.com/file/2023/3/60063_0s.png)
Spring Security基本原理详解
2022-12-24 15:57:54
![](https://img.aspxhome.com/file/2023/9/62459_0s.jpg)
Java针对ArrayList自定义排序的2种实现方法
2022-01-30 03:47:34
linux(center OS7)安装JDK、tomcat、mysql 搭建java web项目运行环境
2022-07-04 07:02:37
MyBatis JdbcType 与Oracle、MySql数据类型对应关系说明
2023-08-23 02:23:06
![](https://img.aspxhome.com/file/2023/6/58276_0s.jpg)
Spring自动注入失败的解决方法
2022-08-13 03:41:31
spring的Cache注解和redis的区别说明
2023-10-20 02:01:33
![](https://img.aspxhome.com/file/2023/9/62969_0s.png)
Java的JNI快速入门教程(推荐)
2022-11-22 16:10:48
谈谈你可能并不了解的java枚举
2023-11-09 21:08:55
![](https://img.aspxhome.com/file/2023/1/59021_0s.png)
浅谈SpringMVC+Spring3+Hibernate4开发环境搭建
2023-08-06 13:49:19
![](https://img.aspxhome.com/file/2023/6/58036_0s.png)
java web服务器实现跨域访问
2023-09-17 06:55:08
Java详细讲解不同版本的接口语法和抽象类与接口的区别
2022-09-30 01:46:38
![](https://img.aspxhome.com/file/2023/5/61875_0s.png)
java 接口回调实例详解
2023-11-19 15:44:24
java求余的技巧汇总
2023-08-06 06:33:16
![](https://img.aspxhome.com/file/2023/8/57898_0s.jpg)
C#复制数组的两种方式及效率比较
2023-07-15 04:19:12
![](https://img.aspxhome.com/file/2023/3/66133_0s.png)
java 微信随机红包算法代码实例
2022-12-03 12:29:08
Java ClassLoader虚拟类实现代码热替换的示例代码
2022-06-25 03:08:52