文章目录
7)控制流程
表达式
表达式是由变量、运算符和方法调用组成的构造,这些构造根据语言的语法构造,计算结果为单个值
编写复合表达式时,要明确并用括号指出应首先计算哪些运算符。这种做法使代码更易于阅读和维护
语句
语句大致相当于自然语言中的句子。一条语句构成一个完整的执行单元。通过以分号 ( ;)结束表达式
还有另外两种语句:声明语句和控制流语句。声明语句声明一个变量,控制流语句控制语句执行的顺序
块
是一组位于一对大括号之间的零个或多个语句,可以在任何允许使用单个语句的地方使用。
顺序结构
程序从上往下一条一条执行
if选择结构
语法格式
if(布尔表达式){
//如果布尔表达式为true,则执行语句
}
if(布尔表达式){
//如果布尔表达式为true,则执行语句
}else{
//如果布尔表达式为false,则执行语句
}
if(布尔表达式1){
//如果布尔表达式1为true,则执行语句
}else if(布尔表达式2){
//如果布尔表达式2为true,则执行语句
}else if(布尔表达式3){
//如果布尔表达式3为true,则执行语句
}else{
//如果以上表达式都不为true,则执行语句
}
switch选择结构
switch可以使用byte,short,int,char,String,enum
String在Java1.7之前是不支持的, Java从1.7开始支持switch用String的,编译后是把String转化为hash值,其实还是整数
语法格式
switch(expression){
case value:
//语句
break;
case value:
//语句
break;
default:
//语句
}
while循环结构
语法结构
while(expression){
//只要表达式为真,一直执行语句,直到为false
}
do{
//至少执行一次的语句
}while(expression);
while先判断后执行语句,dowhile是先执行后判断
for循环结构
语句结构
for(初始化;布尔表达式;更新){
//代码
}
增强的for循环
语句结构
for(声明语句:表达式){
//代码块
}
声明语句:声明新的局部变量,该变量类型和数组元素类型一致,其值和此时数组元素值相等
表达式:要访问的数组名,或者是返回值为数组的方法
break、continue
break 结束整个循环
continue 结束本次循环
带标签的break和continue
标签是指后面跟一个冒号的标识符(不建议使用)
练习题
(1)计算0-100奇数和偶数的和
/***
* 计算0-100之间的奇数和偶数的和
*/
public class Exercise {
public static void main(String[] args) {
int oddSum = 0;
int evenSum = 0;
for (int i = 0; i <= 100; i++) {
if (i % 2 == 0) {
evenSum += i;
} else {
oddSum += i;
}
}
System.out.println("偶数和为:" + evenSum);
System.out.println("奇数和为:" + oddSum);
}
}
控制台输出
偶数和为:2450
奇数和为:2500
(2)计算BMI(肥胖指数)
根据键盘输入的身高体重,判断肥胖指数(BMI),BMI的计算公式是 体重(kg) / (身高*身高)
import java.util.Scanner;
/***
* 根据键盘输入的身高体重,判断肥胖指数(BMI)
*/
public class Exercise {
public static void main(String[] args) {
float bmi = 0f;
float weight = 0f;
float height = 0f;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入体重(kg):");
weight = scanner.nextFloat();
System.out.println("请输入身高(m)");
height = scanner.nextFloat();
bmi = weight/(height * height);
if(bmi < 18.5){
System.out.println("当前BMI是:" + bmi);
System.out.println("身体状态是:体重过轻");
}else if(18.5 <= bmi && bmi < 24){
System.out.println("当前BMI是:" + bmi);
System.out.println("身体状态是:正常体重");
}else if(24 <= bmi && bmi < 27){
System.out.println("当前BMI是:" + bmi);
System.out.println("身体状态是:体重过重");
}else if(27 <= bmi && bmi < 30){
System.out.println("当前BMI是:" + bmi);
System.out.println("身体状态是:轻度肥胖");
}else if(30 <= bmi && bmi < 35){
System.out.println("当前BMI是:" + bmi);
System.out.println("身体状态是:中度肥胖");
}else {
System.out.println("当前BMI是:" + bmi);
System.out.println("身体状态是:重度肥胖");
}
}
}
控制台输出
请输入体重(kg):
85
请输入身高(m)
1.78
height = 1.78
weight = 85.0
当前BMI是:26.827423
身体状态是:体重过重
(3)判断某一年是否为闰年
通过Scanner输入一个年份,然后判断该年是否是闰年
闰年判断标准(满足任何一个)
1、如果能够被4整除,但是不能被100整除
2、能够被400整除
import java.util.Scanner;
/***
* 判断某一年是不是为闰年
*/
public class Exercise {
public static void main(String[] args) {
int year = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入年份:");
while (scanner.hasNextInt()) {
year = scanner.nextInt();
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
System.out.println(year + "是闰年");
} else {
System.out.println(year + "是平年");
}
System.out.println("请输入年份:");
}
}
}
控制台输出
请输入年份:
1990
1990是平年
(4)月份判断季节
通过Scanner输入月份,然后使用switch 判断季节
import java.util.Scanner;
/***
* 判断月份属于的季节
*/
public class Exercise {
public static void main(String[] args) throws Exception {
int month = 0;
Scanner scanner = new Scanner(System.in);
System.out.println("请输入月份:");
while (scanner.hasNextInt()) {
month = scanner.nextInt();
if (month>0 && month<=3) {
System.out.println(month + "月是春天");
} else if(month>3 && month<=6) {
System.out.println(month + "月是夏天");
}
else if(month>6 && month<=9) {
System.out.println(month + "月是秋天");
}
else if(month>9 && month<=12) {
System.out.println(month + "月是冬天");
}else {
System.out.println("!!!!!!!输入正确的月份!!!!!!!!");
}
System.out.println("请输入月份:");
}
System.out.println("输入了非法字符,程序退出");
}
}
(5)计算阶乘
通过Scanner获取一个整数,然后使用while计算这个整数的阶乘,N的阶乘等于 N* (N-1) * (N-2) * … * 1
import java.util.Scanner;
/***
* 计算阶乘
*/
public class Exercise {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输要计算的整数:");
while (scanner.hasNextInt()) {
long result = 1;
int i = scanner.nextInt();
while (i > 0) {
result *= i;
i -= 1;
}
System.out.println("结果为:" + result);
(6)细胞分裂
第一天1个,第二天2个,第三天4个,第四天8个,以此类推。求第十天有多少细胞?
/***
* 细胞分裂
*/
public class Exercise {
public static void main(String[] args) {
int day = 10;
long count = 1;
for (int i = 1; i <= day; i++) {
count = count * 2;
}
System.out.println(day + "天结束细胞总量为:" + count);
}
}
10天结束细胞总量为:1024
(7)被5整除的数
用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
/***
* 输出1-1000之间能被5整除的数,并且每行输出3个
*/
public class Exercise {
public static void main(String[] args) {
int num = 1000;
for (int i = 1; i < 1000; i++) {
if (i % 5 == 0){
System.out.print(i);
if(i % 3 == 0){
System.out.print("\n");//第三个数字结尾换行
}else {
System.out.print("\t");//另外两个数字结尾tab
}
}
}
}
}
(8)打印九九乘法表
/***
* 打印乘法表
*/
public class Exercise {
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j +"×" + i +"=" + i * j);
System.out.print("\t");
}
System.out.println();
}
}
}
(9)打印三角形
/***
* 打印三角形
*/
public class Exercise {
public static void main(String[] args) {
int line = 20;
for (int i = 0; i < line; i++) {
for (int j = line; j > i; j--) {
System.out.print(" ");
}
for (int j = 0; j < (2 * i + 1); j++) {
System.out.print("*");
}
System.out.println();
}
}
}
(10)利息计算
每年投资12000,投资回报率为20%,持续投资多少年,总收入达到100万。
复利公式:F = p* ( (1+r)^n ); (F 最终收入, p 本金 ,r 年利率 ,**n **存了多少年)
/***
* 每年投资12000,投资回报率为20%,持续投资多少年,总收入达到100万
*/
public class Exercise {
public static void main(String[] args) {
int assets = 0;
int principal = 0;
int investment = 12000;
float APR = 0.2f;
int year = 0;
while (assets<1000000){
principal = assets+investment;//年初向总资产中投入12000
assets=(int)(principal*(1+APR));//年末所得总资产
year++;
System.out.println("第"+year+"年总资产"+assets);
}
System.out.println("需要"+year+"年资产达到"+assets);
}
}
(11)寻找指定规则的数
寻找某两个数相除,其结果 离黄金分割点 0.618最近
分母和分子不能同时为偶数
分母和分子 取值范围在[1-20]
/***
* 寻找某两个数相除,其结果 离黄金分割点 0.618最近
*/
public class Exercise {
public static void main(String[] args) {
float f = 0f;
float resalt = 0f;
float preResalt = 1f;
int currentI = 0;
int currentJ = 0;
//只遍历了分母大于分子的数
for (int i = 1; i < 20; i++) {
for (int j = 20; j > i; j--) {
f = (float) i / j;
resalt = f - 0.618f;//每次相除后进行比较
resalt = (resalt > 0) ? resalt : -(resalt);//比较结果取绝对值(没用math)
if(resalt < preResalt){
preResalt = resalt; //如果比较值比上一次小,将结果替换
currentI = i;//记录当时最小时的i
currentJ = j;//记录当时最小时的j
}
}
}
System.out.println("离黄金分割两个数相除最近的是:"+currentI + "/" + currentJ + "=" + (float) currentI / currentJ);
}
}
(12)寻找所有的水仙花数
水仙花数定义:一定是3位数,每一位的立方,加起来恰好是这个数本身,比如153=1*1*1+5*5*5+3*3*3
/***
* 寻找所有的水仙花数
*/
public class HelloWorld {
public static void main(String[] args) {
int ge = 0;
int shi = 0;
int bai = 0;
for (int i = 100; i < 1000; i++) {
ge = i/1%10;
shi = i/10%10;
bai = i/100%10;
if(i == (ge*ge*ge)+(shi*shi*shi)+(bai*bai*bai)) {
System.out.println(i+"为水仙花数");
}
}
}
}
(13)小学算术题
求方框的数字
□ + □ = 8
+ +
□ - □ = 6
‖ ‖
14 10
/***
* 小学算术题
*/
public class HelloWorld {
public static void main(String[] args) {
int a = 0;//最大值8
int b = 0;//最大值8
int c = 0;//最大值14
int d = 0;//最大值10
for (a = 0; a <= 8; a++) {
for (b = 0; b <= 8; b++) {
for (c = 0; c <= 14; c++) {
for (d = 0; d <= 10; d++) {
if((a+b)==8&&(b+d)==10&&(c-d)==6&&(a+c)==14){
System.out.print(a+"\t");
System.out.print(b+"\t");
System.out.print(c+"\t");
System.out.print(d+"\t");
System.out.println();
}
}
}
}
}
}
}
结果3 5 11 5