java4.23作业

一、选择题

1、D
2、B
3、D
4、B
5、C
6、C
7、C
8、B
9、D
10、B

二、编程题

1、根据下面数学函数,编写程序根据x的值,计算y的值,最后输出x和y的值。

public class Demo {
	public static void main(String[] args) {
		
		int x = -5;
		int y = 0;
		if (x < 0) {
			y = -1;
		} else if (x == 0) {
			y = 0;
		} else if (x > 0) {
			y = 1;
		}
		System.out.println(x+","+y); //-5,-1
	}
}

2、给定一个字符变量,判断该变量是否为元音字母并输出。

public class Demo {
	public static void main(String[] args) {
		char ch;
		switch (ch) {
			case 'a':
				System.out.println("这是元音字母 a");
				break;
			case 'e':
				System.out.println("这是元音字母 e");
				break;
			case 'i':
				System.out.println("这是元音字母 i");
				break;
			case 'o':
				System.out.println("这是元音字母 o");
				break;
			case 'u':
				System.out.println("这是元音字母 u");
				break;
			default:
				System.out.println("输入的不属于元音字母");
		}		
	}
}

3、 使用while循环求1到5的平方和。

public class Demo {
	public static void main(String[] args) {
		int n = 1;
		int sum = 0;
		while (n <= 5) {
			sum += n * n;
			n ++;
		}
		System.out.println(sum); //55
	}
}

4 、编写一个程序,求出200到300之间的数,且满足条件:它们三个数字之积为42,三个数字之和为
12。

public class Demo {
	public static void main(String[] args) {
		int ge, shi, bai;
		for (int i = 0; i <= 300; i ++) {
			if(i >= 200) {
				bai = i / 100;
				shi = (i - bai * 100) / 10;
				ge = i - bai * 100 - shi * 10;
				if((bai * shi * ge) == 42 && (bai + shi + ge) == 12) {
					System.out.println(i); //237 273
				}
				
			}
		}
	}
}

5、计算100之内,所有奇数之和

public class Demo {
	public static void main(String[] args) {
		int sum = 0;
		for(int i = 1; i <= 100; i ++) {
			if (i % 2 != 0) 
			sum += i;
		}
		System.out.println(sum); //2500
	}
}

6、寻找所有的水仙花数

public class Demo {
	public static void main(String[] args) {
		int ge, shi, bai;
		for (int i = 0; i <= 999; i ++) {
			if(i >= 100) {
				bai = i / 100;
				shi = (i - bai * 100) / 10;
				ge = i - bai * 100 - shi * 10;
				if((bai * bai * bai + ge * ge * ge + shi * shi * shi) == i) {
					System.out.println(i); //153 370 371 407
				}
			}
		}	
	}
}

7、深秋,树叶开始掉落。第一天树叶开始掉落了一半还多一片,第二天又落下了剩下的一半零一片。如此往复循环,直到第十五天,树上还剩一片叶子。问:这颗大树总共有多少片叶子

public class Demo {
	public static void main(String[] args) {
		int total = 0;
		 for(int i = 0; i < 15; i ++) {
			total = (total + 1) * 2;
		 }
		 System.out.println(total); //65534
	}
}

8、有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?

public class Demo {
	public static void main(String[] args) {
		int userCase = 0;
		for (int big = 0; big <= 100; big++) {
            for (int mid = 0; mid <= 100; mid++) {
                for (int small = 0; small <= 100; small++) {
                    if (
                    		((big * 3 + mid * 2 + (small / 2) * 1) == 100) 
                    		&&
                    		((big + mid + small) == 100)
                    		&&
                    		(small % 2 == 0)
                		) {
	                    	userCount ++;
	                    	System.out.println( "case "+ userCase +": 大马:" + big +" 中马:" + mid + " 小马:"+ small);
	                		/*case 1: 大马:2 中马:30 小马:68
	                		case 2: 大马:5 中马:25 小马:70
	                		case 3: 大马:8 中马:20 小马:72
	                		case 4: 大马:11 中马:15 小马:74
	                		case 5: 大马:14 中马:10 小马:76
	                		case 6: 大马:17 中马:5 小马:78
	                		case 7: 大马:20 中马:0 小马:80*/
	                  }
	             }
	        }
		}
	}
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值