JAVA练习题

今日练习题

通过键盘输入一个年份,输出判断某一年是否为闰年

1public class A1 {
2public static void main(String[] args) {
3 int year = 0;
4 Scanner sc = new Scanner(System.in);
5 while (true) {
6 System.out.println("请输入年份:");
7 String ss = sc.nextLine();
8 try {
9year = Integer.parseInt(ss);
10if (year > 0) {
11break;
12}
13
System.out.println("年份需要大于0!");
14} catch (Exception e) {
15System.out.println("输入的年份不合法!");
16}
17}
18boolean run = (year % 4 == 0 && year % 100 != 0) || (year % 400 ==
0);
19if (run)
20System.out.println(year + "年是闰年");
21else
22System.out.println(year + "年不是闰年");
23
24}
	}

2利用条件运算符的嵌套来完成此题:所有成绩为整数,学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示

1public class A2 {
2public static void main(String[] args) {
3 Scanner sc=new Scanner(System.in);
4 int score=0;
5 while (true) {
6 System.out.println("成绩:");
7 String ss=sc.nextLine();
8 try {
9score=Integer.parseInt(ss);
10if(score>=0 && score<=100) {
11break;
12}
13System.out.println("成绩应该是0到100之间!");
14} catch (Exception e) {
15System.out.println("成绩格式不合法!");
16}
17 }
18 int level=score/10;
19 char res='\0';
// 允许的数据类型为3种整型(byte short int)、字符、字符串(hashcode值比对)、enum枚举类型

20
21 switch (level) {
22 case 10:
23 case 9:
24res='A';
25break;
26 case 8:
27 case 6:
28 case 7:
29res='B';
30break;
31default:
32 res='C';
33 break;
34}
35}
36}

求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。输出结果的形式如:2+22+222=246。输入a和n

public class A3 {
public static void main(String[] args) {
3 int res = 0;
4 Scanner sc = new Scanner(System.in);
5 int a=inputNumber(sc, 1, 9);
6 int n=inputNumber(sc, 1, 8);//由于res为int决定的
7 String outStr = "";8
for (int i = 1; i <= n; i++) {
9
int aa = generateNumber(a, i);
10 res+=aa;
11 outStr += aa;
12 outStr += "+";
13 }
14 outStr=outStr.substring(0,outStr.length()-1);
15 System.out.println(outStr+"="+res);
16}
17
18
public static int generateNumber(int a, int n) {
19 String ss = "";
20 for (int i = 0; i < n; i++) {
21ss = ss + a;
22 }
23 return Integer.parseInt(ss);
24}
25
26public static int inputNumber(Scanner sc, int min, int max) {
27 int res = 0;
28 while (true) {
29 System.out.println("请输入整数");
30 String ss = sc.nextLine();
31 try {
32 res = Integer.parseInt(ss);
33 if (res >= min && res <= max)
34break;
35System.out.println("输入的数据应在" + min + "到" + max + "之
间!");
36} catch (Exception e) {
37System.out.println("数据格式不合法!");
38}
39}
40return res;
41}
42}

一个数如果恰好等于它的因子之和,这个数就称为"完数"。例如6=1+2+3.编程找出1000以内的所有完数

1public class A4 {
2
3public static void main(String[] args) {
//
int k=7;
4for (int k=1; k < 1000; k++) {
5 int sum = 0;
6 for (int i = 1; i < k; i++) {
7 if (k % i == 0) {
8 sum += i;
9}
10 }
11 if (k == sum)
12System.out.println(k + "是完数");
13}
14}
15}
1public class A4 {
2public static void main(String[] args) {
3for (int k = 1; k < 1000; k++)
4if (wanShu(k)) System.out.println(k + "是完数");
5 }
6 public static boolean wanShu(int k) {
7 int sum = 0;
8 for (int i = 1; i < k; i++)
9if (k % i == 0) sum += i;
10return k == sum;
11
12
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值