欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

JAVA入门——thirdclass

时间:2023-08-02
控制语句

控制语句: 把语句组合成能完成一定功能的小逻辑模块。

它分为三类: 顺序、选择和循环。

“顺序结构”代表“先执行 a, 再执行 b ”的逻辑。

“条件判断结构”代表“如果 …, 则 …”的逻辑。

“循环结构”代表“如果 …, 则重复执行 …”的逻辑。


实际上,任何软件和程序,小到一个练习,大到一个操作系统,本质上都是由“变量、选择语句、循环语句”组成。

条件判断结构(选择结构)

条件判断结构有: if 结构和 switch 结构。
而 if 结构又可以分为 if 单分支结构、if-else 双分支结构、 if-else if-else 多分支结构。

- if 单分支结构


语法结构:

if(布尔表达式){ 语句块 }

tip:

如果 if 语句不写{},则只能作用于后面的第一条语句。最好任何时候都写上{},即使里面只有一句话。

【示例】if 单分支结构(掷骰子游戏)

public class Test1 { public static void main(String[ ] args) { //通过掷三个骰子看看今天的手气如何?int i = (int)(6 * Math.random()) + 1;//通过Math.random()产生随机数 int j = (int)(6 * Math.random()) + 1; int k = (int)(6 * Math.random()) + 1; int count = i + j + k; //如果三个骰子之和大于 15,则手气不错 if(count > 15) { System.out.println("今天手气不错"); }//如果三个骰子之和在 10 到 15 之间,则手气一般 if(count >= 10 && count <= 15) { //错误写法:10<=count<=15 System.out.println("今天手气很一般"); }//如果三个骰子之和小于 10,则手气不怎么样 if(count < 10) { System.out.println("今天手气不怎么样");}System.out.println("得了" + count + "分"); } }

Math 类的使用
 Math.random()该方法用于产生 0 到 1 区间的 double 类型的随机数,但是不包括 1。
//产生一个[0,1)之间的随机数。
Math.random():
//返回指定范围的随机数(m-n之间)的公式:
Math.random()(n-m)+m;
或者
Math.random()
(n+1-m)+m
 int i = (int) (6 * Math.random()); //产生:[0,5]之间的随机整数

- if-else 双分支结构

语法结构:

if(布尔表达式){ 语句块 1}else{ 语句块 2 }

【示例】if-else 双分支结构

public class Test2 {public static void main(String[ ] args) { //随机产生一个[0.0, 4.0)区间的半径,并根据半径求圆的面积和周长 double r = 4 * Math.random(); //Math.pow(r, 2)求半径 r 的平方 double area = 3.14* r*r; double circle = 2 * Math.PI * r; System.out.println("半径为: " + r); System.out.println("面积为: " + area); System.out.println("周长为: " + circle); //如果面积>=周长,则输出"面积大于等于周长",否则,输出周长大于面积 if(area >= circle) { System.out.println("面积大于等于周长"); } else { System.out.println("周长大于面积"); } } }

条件运算符有时候可用于代替 if-else。

//使用 if-elseint a = 3; int b = 4; int c2 = 0; if(a

- if-else if-else 多分支结构


语法结构:

if(布尔表达式 1) { 语句块 1; } else if(布尔表达式 2) { 语句块 2; }…… else if(布尔表达式 n){ 语句块 n; } else { 语句块 n+1; }

【示例】if-else if-else 多分支结构

public class Test5 { public static void main(String[ ] args) {int age = (int) (100 * Math.random()); System.out.print("年龄是" + age + ", 属于"); //15 岁以下儿童;15-24 青年;25-44 中年;45-64 中老年;65-84 老年;85 以上老 寿星 if (age < 15) { System.out.println("儿童, 喜欢玩!"); } else if (age < 25) { System.out.println("青年, 要学习!"); } else if (age < 45) { System.out.println("中年, 要工作!");} else if (age < 65) { System.out.println("中老年, 要补钙!"); } else if (age < 85) { System.out.println("老年, 多运动!"); } else { System.out.println("老寿星, 古来稀!"); } } }

- switch 多分支结构(多值情况)


语法结构:

switch (表达式) { case 值 1: 语句块 1; [break]; case 值 2: 语句块 2; [break]; … … … … … [default: 默认语句块;] }

switch 中表达式的值,是 int(byte、short、char 也可,long 不行)、枚举,字符串。

example1:

//grade 表示大学年级//条件语句版int grade = 1;if(grade==1) {System.out.println("大学一年级");}else if(grade==2){System.out.println("大学二年级");}else if(grade==3) {System.out.println("大学三年级");}else{System.out.println("大四了");}//switch版switch (grade){case 1:System.out.println("大学一年级");break;case 2:System.out.println("大学二年级");break;case 3:System.out.println("大学三年级");break;default:System.out.println("大四了,要毕业了");break;}

example2:

public class TestSwitch02 {public static void main(String[] args){int month = 2; //1 表示1 月,2 表示2 月,....if(month==1 | |month==2 | |month==3){System.out.println("春季");}else if(month==4 | |month==5 | |month==6){System.out.println("夏季");}else if(month==7 | |month==8 | |month==9){System.out.println("秋季");}else{System.out.println("冬季");}System.out.println("========使用 switch 改造上面的代码 ,switch 特别适合多值判断=============");switch (month){case 1:case 2:case 3:System.out.println("春季");break;case 4:case 5:case 6:System.out.println("夏季");break;case 7:case 8:case 9:System.out.println("秋季");break;default:System.out.println("冬季");}}}

switch 接收字符串
【示例】JDK7.0 之后可以直接使用字符串。

String str = "audi";switch (str){ case "audi":System.out.println("我买了个奥迪车");break;case "benz":System.out.println("我买了个奔驰车");break;default:System.out.println("比亚迪, 挺好! ");}

循环结构

循环结构分两大类, 一类是当型, 一类是直到型。
口 当型:
当布尔表达式条件为true 时, 反复执行某语句, 当布尔表达式的值为 false 时才停止 循环, 比如: while 与 for 循环。
口 直到型:
先执行某语句, 再判断布尔表达式, 如果为true, 再执行某语句, 如此反复, 直到布 尔表达式条件为 false 时才停止循环, 比如 do-while 循环。

- while 循环


语法结构:

while (布尔表达式) { 循环体;}

【示例】while 循环结构: 求 1 到 100 之间的累加和

public class Test7 {public static void main(String[ ] args) {int i = 0;int sum = 0;// 1 +2 +3 + … +100=?while (i < = 100) {sum + = i;//相当于 sum = sum+i;i++;}System.out.println("Sum= " + sum);}}

- do-while 循环

语法结构:

do { 循环体;//do-while 循环的循环体至少执行一次。} while(布尔表达式) ;

【示例】do-while 循环结构: 求 1-100 之间的累加和

public class Test8 {public static void main(String[ ] args) {int i = 0;int sum = 0;do {sum + = i; // sum = sum + ii++;} while (i < = 100); //此处的 ;不能省略 System.out.println("Sum= " + sum);}}

practice:
while/for 循环 0-130 之间的数字, 每行显示 5 个数字。

public class xunhuan { public static void main(String[] args) { int i=0; int j; while(i<=130) { for (j = 0; j < 5; j++) { System.out.print(i); i++; System.out.print("t"); } System.out.print("n"); } }}

使用print运行结果如下:

若均用println则会:

tip:
printprintlnprintf的区别
print将它的参数显示在命令窗口,并将输出光标定位在所显示的最后一个字符之后。
println 将它的参数显示在命令窗口,并在结尾加上换行符,将输出光标定位在下一行的开始。
//println(“test”)相当于print(“testn”)就是一般的输出字符串
printf是格式化输出的形式。

- for循环


语法结构:

for (初始表达式; 布尔表达式; 迭代因子) { 循环体;//for 循环在执行条件判定后,先执行的循环体部分,再执行步进}//口 初始化部分设置: 循环变量的初值//口 条件判断部分为: 布尔表达式//口 迭代因子: 控制循环变量的增减

【示例】for 循环

public class Test10{ public static void main(String args[ ]) { int sum = 0;//1.求 1-100 之间的累加和 for (int i = 0; i <= 100; i++) { sum += i; } System.out.println("Sum= " + sum);//2.循环输出 9-1 之间的数 for(int i =9;i>0;i--){ System.out.print(i+"、 "); } System.out.println();//3.输出 90-1 之间能被 3 整除的数 for(int i =90;i>0;i-=3){ System.out.print(i+"、 "); } System.out.println(); }}


【示例】逗号运算符

public class Test11 { public static void main(String[ ] args) { for(int i = 1, j = i + 10; i < 5; i++, j = i * 2) { System.out.println("i = " + i + " j = " + j); } }}


【示例 】无限循环

public class Test12 { public static void main(String[ ] args) { for ( ; ; ) { // 无限循环: 相当于 while(true) System.out.println("嗲嗲学习记"); } }}

【示例】初始化变量的作用域

public class Test13 { public static void main(String[] args) { //根据变量作用域来判断循环前or里来定义变量 for(int i = 1; i < 10; i++) { System.out.println(i+" "); } System.out.println(i);//编译错误,无法访问在 for 循环中定义的变量 i } }

- 嵌套循环

【示例】嵌套循环

public class Test14 {public static void main(String args[ ]) { for (int i =1; i < =5; i++) {for(int j=1; j<=5; j++){ System.out.print(i+" ");}System.out.println();}}}


【示例】使用嵌套循环实现九九乘法表

public class xunhuan { public static void main(String[] args) { int i,j; for(i=1;i<=9;i++){//for (int i=1;i<10;i++) for(j=1;j<=i;j++){ System.out.print(j+"*"+i+"="+i*j+"t");//前面是j后面是i //System.out.print(j + "*" + i + " =" + (i * j < 10 ? (" " + i * j) : i * j) + " ")两者输出区别在于结果为个位数时前面的位数会有空格 } System.out.println(); } }}

【示例】使用嵌套循环,打印输出 5*5 的方阵

public class qiantaoxunhuan { public static void main(String[] args) { int i; for(i=1;i<=5;i++){ if(i%2==1) { System.out.print("* " + "# " + "* " + "# " + "* "); } else{ System.out.print("# "+"* "+"# "+"* "+"* "); } System.out.println(); } }}

- break 语句和 continue 语句

break 用于强行退出整个循环 continue 用于结束本次循环,继续下一次

【示例】 break 语句

//产生 100 以内的随机数, 直到随机数为 88 时终止循环public class Test16 { public static void main(String[ ] args) { int total = 0;//定义计数器 System.out.println("Begin"); while (true) { total++;//每循环一次计数器加 1 int i = (int) Math.round(100 * Math.random()); //当 i 等于 88 时, 退出循环 if (i = = 88) { break; } } //输出循环的次数 System.out.println("Game over, used " + total + " times."); }}

【示例】continue 语句

//把 100~150 之间不能被 3 整除的数输出, 并且每行输出 5 个public class Test17 { public static void main(String[ ] args) { int count = 0;//定义计数器 for (int i = 100; i < 150; i++) {//如果是 3 的倍数,则跳过本次循环,继续进行下一次循环 if (i % 3 = = 0){ continue; }//否则 (不是 3 的倍数) 输出该数 System.out.print(i + "、 "); count++;//每输出一个数,计数器加 1//根据计数器判断每行是否已经输出了 5 个数 if (count % 5 = = 0) { System.out.println(); } } }}

【示例】带标签的 continue
“标签”是指后面跟一个冒号的标识符, 例如: “ label:” 。对 Java 来说唯一用到标 签的地方是在循环语句之前。

//控制嵌套循环跳转(打印 101-150 之间所有的质数)public class Test18 { public static void main(String args[ ]) { outer: for (int i = 101; i < 150; i++) { for (int j = 2; j < i / 2; j++) { if (i % j = = 0){ continue outer; //符合某条件, 跳到外部循环继续 } } System.out.print(i + " "); } } }

example:

薪水计算器:
(1) 通过键盘输入用户的月薪, 每年是几个月薪水。
(2) 输出用户的年薪
(3) 输出一行字“如果年薪超过 10 万, 恭喜你超越 90%的国人”, “如果年薪超过
20 万, 恭喜你超越 98%的国人”。
(4) 键盘输入数字 88, 则退出程序 (使用 break 退出循环)
(5) 键盘输入 66, 直接显示“重新开始计算…”, 然后算下一个用户的年薪。

import java.util.Scanner;public class SalaryCalculator { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.println("***********我的薪水计算器***********"); System.out.println("1.输入 88,退出程序n2.输入 66,计算下一个年薪"); while (true) { System.out.println("请输入月薪:"); int monthSalary = s.nextInt(); System.out.println("请输入一年几个月薪资:"); int months = s.nextInt(); int yearSalary = monthSalary * months; //年薪 System.out.println("年薪是:" + yearSalary); if (yearSalary >= 200000) { System.out.println("恭喜你超越 98%的国人"); } else if (yearSalary >= 100000) { System.out.println("恭喜你超越 90%的国人"); } System.out.println("输入 88,退出系统;输入 66,继续计算。"); int comm = s.nextInt(); if (comm == 88) { System.out.println("系统退出 !"); break; } if (comm == 66) { System.out.println("继续计算下一个薪资"); continue; } } }}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。