选择结构
if选择结构
if(布尔表达式){
//代码块1
}else{
//代码块2
}
if(布尔表达式1){
//代码块1
}else if(布尔表达式2){
//代码块2
}else{
//代码块3
}
相互排斥,执行某个代码块后将退出循环。
嵌套if选择结构
if(外层表达式){
if(内层表达式){
//内层代码块1
}else{
//内层代码块2
}
}else{
//外层代码块3
}
分支结构
switch(变量|表达式){
case 值1:
逻辑代码1;
case 值2:
逻辑代码2;
...
}
int weekday = 3;
switch(weekday){
case 1:
System.out.println("数学分析");
break;
case 2:
System.out.println("高等代数");
break;
case 3:
System.out.println("实变函数");
break;
……
default:
//未满足时的逻辑代码
System.out.println("请输入1~7之间的有效数字");
break;
}
匹配的关键字被执行后,并不会自动退出整个结构,而是继续向下执行。
如果有重复的,只需将它们放在一起,即可向下贯穿。
case 1:
System.out.println("数学分析");
break;
case 2:
case 5:
System.out.println("高等代数");
break;//此时case 2 5 的情况都是高等代数
case 3:
System.out.println("实变函数");
break;
……
break关键字
break主要用在循环语句或者switch语句中,用来跳出整个语句块。
break跳出最里面的循环,并且继续执行该循环下面的语句。
循环
概念:通过某个条件,重复的执行一段逻辑代码。
while循环
while(布尔表达式){
//代码块
}
当表达式为true,则执行代码块,完成后,重新判断表达式,若仍为true,则继续执行。直到判断为false,跳出循环。
若一直为true,则成为死循环。
若首次判断为false,则一次都不执行。
int i = 1;
int sum = 0;
while(i <= 100){
if(i % 2 == 0){
sum = sum+i;
}
i++;
}
while(true)死循环
循环次数明确的循环结构:
- 初始部分 :用以做判断的变量;
- 循环的条件:决定是否继续循环
- 循环操作:单次执行的逻辑代码
- 迭代部分:控制循环条件改变
do while循环
do{
//逻辑代码
}while(布尔表达式)
先执行一次,再进行表达式判断
如果结果为true,则再次循环
如果结果为false,则退出循环
Scanner input = new Scanner(System.in);
char answer;
do{
System.out.println("抄写一遍作业代码");
System.out.println("请老师评价:");
answer = input.next().charAt(0);
}while(answer != 'y');
System.out.println("作业已完成");
适用循环次数不确定的情形。
while和do while 的对比
- 首次即有入口条件,先判断,再执行,适用于循环次数明确的场景
- 首次没有入口条件,先执行,再判断,适用于循环次数不明确的场景
for循环
for(初始部分;布尔表达式;迭代部分){
//代码块
}
for(;;)死循环
- 首次执行初始部分(仅一次)
- 对布尔表达式进行判断,结果为true,则执行逻辑代码
- 本次执行完毕,执行迭代部分,再次判断
- 直至布尔表达式结果为false时,跳出循环
Scanner input = new Scanner(System.in);
System.out.println("输入一个整数:");
int n = input.nextInt();
long fac = 1L;//用int类型只能计算到12的阶乘,否则就会超出int取值范围
for(int i = 1;i<=n;i++){
fac = fac*i;
}
System.out.println(n + "的阶乘为" + fac);
增强for循环
for(声明语句 : 表达式)
{
//代码句子
}
声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。
表达式:表达式是要访问的数组名,或者是返回值为数组的方法。
int [] numbers = {10, 20, 30, 40, 50};
for(int x : numbers ){
System.out.print( x );
System.out.print(",");
}
String [] names ={"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
结果:10,20,30,40,50,James,Larry,Tom,Lacy,
continue:结束本次循环。进入下一次循环
Scanner input = new Scanner(System.in);
double sum = 0D;
for(int i = 1;i<=5;){
System.out.print("请输入第" + i + "为学生成绩");
double score = input.nextDouble();
if(score < 0 || score > 100){
continue;
}
sum += score;
i++;
}
double ave = sum/5;
System.out.print("平均分为:" + ave);
或用do-while结构
Scanner input = new Scanner(System.in);
double sum = 0D;
double score;
for(int i = 1;i<=5;){
do{
System.out.print("请输入第" + i + "为学生成绩:");
score = input.nextDouble();
}while(score < 0 || score > 100);
sum += score;
i++;
}
double ave = sum/5;
System.out.print("平均分为:" + ave);
使用continue和break以及for、if写一个与电脑猜拳的小程序
(三局两胜)
package com.self;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int pcount = 0;
int ccount = 0;
for(int i = 1;i<= 3 ;){
int computer = ((int) (Math.random()*10))%3+1;
System.out.println("请输入猜拳编号(1.剪刀 2.石头 3.步):");
int player = input.nextInt();
//比较
if(player == computer ){
//平局
System.out.println("不错哦,再接再励");
continue;
}else if( (player == 1 && computer ==3) ||(player == 2 && computer == 1) ||(player == 3 && computer ==2)){
System.out.println("恭喜,大神");
pcount++;
}else{
System.out.println("哎呀,给你机会你不中用啊");
ccount++;
}
if(pcount ==2){
System.out.println("玩家获胜");
break;
}else if(ccount ==2){
System.out.println("电脑获胜");
break;
}
i++;
}
}
}