1.数一下 1到 100 的所有整数中出现多少个数字9。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Test { public static void main(String[] args) { int count = 0; for (int i = 0; i < 100 ; i++) { if(i % 10 == 9 ) { count++; } //99中有2个9,必须分开算 if(i / 10 == 9) { count++; } } System.out.println(count); } }
2.求出0~999之间的所有“水仙花数”并输出。(“水仙花数”是指一个三位数,其各位数字的立方和确好等于该数本身,如;153=1+5+3?,则153是一个“水仙花数”。)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class Test{ public static void main(String[] args) { NarcissisticNum(1000); } public static void NarcissisticNum(int num) { for (int i = 0; i < num ; i++) { int bit = i % 10; int hundred = i / 100; int tenPlace = i / 10 % 10; int sum = bit*bit*bit + tenPlace*tenPlace*tenPlace + hundred*hundred*hundred; if(sum == i){ System.out.println(i); } } } }
- 编写代码模拟三次密码输入的场景。
最多能输入三次密码,密码正确,提示“登录成功”,密码错误,
可以重新输入,最多输入三次。三次均错,则提示退出程序
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); for (int i = 0; i < 3; i++) { System.out.println("请输入用户名:"); String name = sc.nextLine(); System.out.println("请输入密码:"); String password = sc.nextLine(); if("Tina".equals(name) && "123456".equals(password)) { System.out.println("登陆成功"); break; }else { if(i == 2) { System.out.println("对不起,输入错误,没有机会了!!"); break; } System.out.println("输入错误,请重新输入"); System.out.println("您还有"+(2-i)+"次机会"); } } } }
- 写一个函数返回参数二进制中 1 的个数
比如: 15 0000 1111 4 个 1
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Test { public static void main(String[] args) { int num = -1; int ret = numOfone(num); System.out.printf("二进制1的个数 = " + ret); } public static int numOfone(int num) { int count = 0; while(num != 0) { count++; num = num & (num - 1); } return count; } }
- 获取一个数二进制序列中所有的偶数位和奇数位,
分别输出二进制序列。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21public class Test { public static void main(String[] args) { int num = 10; odd(num); System.out.println(); even(num); } //奇数位 public static void odd(int num) { for (int i = 30; i >= 0 ; i -= 2) { System.out.print((num >> i) & 1); } } //偶数位 public static void even(int num) { for(int i = 31;i > 0; i -= 2 ) { System.out.print((num >> i) & 1); } } }
- 输出一个整数的每一位
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15import java.util.Scanner; public class Test { public static void main(String[] args) { System.out.println("请输入一个数字"); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); print(num); } public static void print(int num) { if( num!= 0) { print(num / 10); System.out.println(num % 10); } } }
- 完成猜数字游戏
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int num = (int) (Math.random() * 100 + 1); System.out.println("游戏开始,请输入1-100之间的数字"); while (true) { int guess = sc.nextInt(); if (num > guess) { System.out.println("猜小了"); } else if (num < guess) { System.out.println("猜大了"); } else { System.out.println("猜对了"); break; } } } }
最后
以上就是冷傲中心最近收集整理的关于条件和循环练习(2)的全部内容,更多相关条件和循环练习(2)内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复