java快速复习04(简洁版)
第五章-程序控制结构
01.编程实现如下功能
某人又100000元,每经过一次路口,需要交费,规则如下:
1)当现金>50000时,每次交5%
2)当现金<=50000时,每次交1000
编程计算该人可以经过多少次路口,要求:使用 while break 方式完成
public class Homework01 {
public static void main(String[] args) {
double deposit = 100000;//存款的数量
int i = 0;//经过的路口数量
while(true) {
if (deposit > 50000) {//过路口
deposit -= deposit * 0.05;//过了路口剩余的钱
} else if (deposit >= 1000) {
deposit -= 1000;
} else {//存款没有1000元了
break;
}
i++;//每循环一次,记录一次
}
System.out.println(i);//打印经过的路口数量
}
}
02.判读一个整数,属于哪个范围: 大于0;小于0;等于0
import java.util.Scanner;
public class Homework02 {
public static void main(String[] args) {
int num;
System.out.print("请输入一个整数:");
Scanner myScanner = new Scanner(System.in);
num = myScanner.nextInt();
if(num > 0){
System.out.println("你输入的整数" + num + "为正数");
} else if (num < 0) {
System.out.println("你输入的整数" + num + "为负数");
} else {
System.out.println("你输入的整数" + num + "为0");
}
}
}
03.判断一个年份是否为闰年
(1)解题思路:年份可以被4整除,但不能被100整除;能被400整除
import java.util.Scanner;
public class Homework03 {
public static void main(String[] args) {
System.out.println("请输入一个年份:");
Scanner myScanner = new Scanner(System.in);
int year = myScanner.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println("你输入的年份" + year +"是闰年");
} else {
System.out.println("你输入的年份不是闰年");
}
}
}
04.判读一个整数是否是水仙花数
1)所谓水仙花数是指一个三位数,其各个位上数字立方和等于其本身.
例如: 153 = 1*1*1 + 5*5*5 + 3*3*3
这个题不难主要在于如何将将一个三位数的每个位数的数字单独取出来。
思路:数字num的百位 = num / 100
数字num的十位 = num % 100 / 10
数字num的个位 = num % 10
import java.util.Scanner;
public class Homework04 {
public static void main(String[] args) {
System.out.println("请输入一个三位数");
Scanner myScanner = new Scanner(System.in);
int num = myScanner.nextInt();
int a, b, c;
a = num / 100;
b = num % 100 / 10;
c = num % 10;
if (num < 1000 && num >99) {
if (num == a*a*a + b*b*b + c*c*c) {
System.out.println("你输入的这个三位数为水仙花数");
} else {
System.out.println("你输入的数字不是一个水仙花数");
}
} else {
System.out.println("你输入的数字不是一个三位数");
}
}
}
05.看看下面代码输出什么?
class Demo {
public static void main(String[] args) {
int m = 0,n = 3;
if(m > 0) {
if(n > 2)
System.out.println("01");
else
System.out.println("02");
}
}
}
这个题目是个小陷阱有两个if,一个if嵌套一个if.首先我们看第一个if ,只有m>0才会执行第二个if里面的内容,但是m=0。
所以这道题的答案是:什么都没有输出
06.输出1-100之间的不能被5整除的数,每5个一行
public class Homework06 {
public static void main(String[] args) {
int a = 0;
for (int i = 1; i<=100; i++) {
if(i % 5 != 0) {
System.out.print(i + "\t");
a++;
if (a % 5 == 0) {
System.out.print('\n');
}
}
}
}
}
07.输出小写的a-z以及大写的Z-A
1)思路分析:
‘b’ = ‘a’ + 1;
‘c’ = ‘a’ + 2;
详情请看ascll码表
public class Homework07 {
public static void main(String[] args) {
for (char c1 = 'a';c1 <= 'z'; c1++ ) {
System.out.print(c1 + " ");
}
System.out.println();//换行
for (char c2 = 'Z';c2 >= 'A'; c2--) {
System.out.print(c2 + " ");
}
}
}
08.1 - 1/2 + 1/3 -1/4 + 1/5…1/100的和
public class Homework08 {
public static void main(String[] args) {
double sum = 0;
for (int i = 1; i <= 100; i++) {
//判断加减,奇数加偶数减
if (i % 2 != 0) {//奇数
sum += (1.0/i);
} else {//偶数
sum -= (1.0/i);
}
}
System.out.println(sum);
}
}
09.求1 + (1+2) + (1+2+3) + (1+2+3+4+) + …+ (1+2+3+…+100)的结果
public class Homework09 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 100; i++) {
for (int j = 1; j <= i; j++) {
sum += j;
}
}
System.out.println(sum);
}
}