Java-流程控制语句

1. if条件语句

单分支 if 语句

...CodeA 
if (布尔表达式) {
    语句组; 
}
...CodeB

演示:判断一个年份是否是闰年

import java.util.Scanner;
public class Demo{
     public static void main(String[] args){
          Scanner input = new Scanner(System.in);
          System.out.print("Enter a year");
          int year = input.nextInt();
          if ((year % 4 == 0 && year % 100 !=0) || (year % 400 == 0)) {                  
              System.out.println(year + "是闰年"); 
          }
          System.out.println("判断结束!");
     }
}

双分支if-else语句

...CodeA 
if (布尔表达式) { 
    语句组A; 
} else { 
    语句组B; 
}
....CodeB

演示:判断一个点是否在圆里面

import java.util.Scanner; 
public class Demo{
     public static void main(String[] args) {
     Scanner input = new Scanner(System.in); 
     System.out.print("请输入点坐标:"); 
     double x = input.nextDouble(); 
     double y = input.nextDouble(); 
     double ox = 0; 
     double oy = 0; 
     double radius = 10;
     double distance = Math.sqrt(Math.pow(x - ox,2) + Math.pow(y - oy,2));
     if (distance > radius) { 
     System.out.println("Point ("+ x +"," + y + ") is not in the circle");
     } else { 
     System.out.println("Point ("+ x +"," + y + ") is in the circle"); 
     }
     System.out.println("Compute Complete!");
     }
}

多分支if-else-if语句

...CodeA 
if (布尔表达式1) { 
    语句组A; 
} else if (布尔表达式2) { 
    语句组B; 
} else if (布尔表达式3) { 
    语句组C; 
} else { 
    语句组D; 
}
...CodeB

演示:BMI指数计算

import java.util.Scanner; 
public class Demo { 
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); 
        System.out.print("请输入体重(kg)和身高(m):"); 
        double weight = input.nextDouble(); 
        double height = input.nextDouble();
        double BMI = weight / height / height;
        if (BMI < 18.5) { 
            System.out.println("偏瘦"); 
        } else if (BMI < 25.0) { 
            System.out.println("正常");
        } else if (BMI < 30.0) { 
            System.out.println("超重"); 
        } else { 
            System.out.println("过胖");
        }
    
    }
}

2. switch分支语句

与if分支语句一样,都是对条件的判断。switch一般用在条件较多的情况下,但是有一个重要的细节:if语言可以对区间值或固定值进行判断,switch只能对固定值进行判断。

switch (变量) { 
    case1: //if (变量==值1) {语句组A;} 
        语句组A; 
        break; 
    case2: 
        语句组B;
        break;
    ... 
    case 值n: //if (变量==值n) {语句组N;} 
        语句组N; 
    default: // else {语句组N+1;} 
        语句组N+1; 
        break; 
}

switch的一些使用细节

  • switch所传入的变量,char,byte,short,int,String或者枚举类型
  • 值1,值2,一直到值n,这几个值必须是同一个数据类型的
  • 当变量匹配的相关case的值的时候,执行case中的语句,直到遇到break结束;如果该case语句中没有break,则继续向下执行,直到遇到另外一个break结束

3. 循环语句

循环主要解决具有规律性的且具有重复性的代码问题,避免程序冗余

//将一个数字所有位数拆开 相加 
//number = 123 
number % 10 = 3 
number /= 10 12 
number % 10 = 2 
number /= 10 1 
number % 10 = 1 
number /=10 0

循环四要素

  • 循环的初始化:循环的第1次执行从哪里开始
  • 循环的继续条件:循环从当前轮是否向后执行下一轮
  • 循环体:需要被循环执行的部分
  • 循环的步长、周期:当前循环到下一轮循环之间的变化

我们常见的循环问题可以分为两大类:for循环、while循环
for循环(已知循环次数)

for (1循环的初始化;2.循环的继续条件;4.循环的步长) { 
    3.循环体 
}
// 1-2-3-4-2-3-4-2-3-4-2不满足则结束循环

演示:

import java.util.Scanner; 
public class Demo { 
    public static void main(String[] args) {
         for (int i = 1; i <= 5; i++) { 
             System.out.print("*"); 
         }
         System.out.println();
         System.out.println("=============================");
         for (int j = 1; j <= 4; j++) {
             for (int i = 1; i <= 5; i++) { 
                 System.out.print("*");     
             }
             System.out.println();
         }
         System.out.println("=============================");
         for (int line = 1; line <= 7; line++) {
             for (int i = 1; i <= line; i++) { 
                 System.out.print("*"); 
             }
             System.out.println();
         }
         System.out.println("=============================");
    }
}

while循环(循环次数未知,循环结束条件已知)

1.循环的初始化 
while (2.循环继续条件) { 
    3.循环体 
    4.循环的步长、周期 
}

演示:猜数字

import java.util.Scanner; 
public class Demo{ 
    public static void main(String[] args) {
         int com = (int) (Math.random() * 101);
         Scanner input = new Scanner(System.in); 
         while (true) { 
             System.out.print("请输入一个数字:"); 
             int num = input.nextInt();
             if (num > com) { 
                 System.out.println("high"); 
             }else if(num < com) { 
                 System.out.println("low"); 
             }else { 
                 System.out.println("niubility!"); 
             break;
             }
         }
    }
}        

4. 跳转语句

break语句

在循环语句中叫做终止语句,终止的是break最近的一层循环

continue语句

在循环语句中叫做跳过语句,跳过本次循环开启下一轮循环

5. 练习题

判断回文数

public class Demo{
    public static void main (String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("请输入一个数字");
        int num = input.nextInt();
        int a = num % 10;
        int b = num / 100;
        if (a == b){
            System.out.println(num + "是回文数字");
        }else{
            System.out.println(num + "不是回文数字");
        }
    
    }
    
}

判断素数个数:

public class Demo{
     public static void main(String[] args) { 
         int count = 0;
         for (int number = 2; number <= 1000; number++) { 
             boolean flag = true; 
             for (int i = 2; i <= number / 2; i++) { 
                 if (number % i == 0) {
                     flag = false; break; 
                 }
             }
             if (flag) { 
                 System.out.print(number + "\t"); 
                 count++; 
                 if (count % 8 == 0) { 
                     System.out.println();
                 } 
             } 
         } 
    } 
}

判断圆的位置

import java.util.Scanner; 
public class Demo{ 
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); 
        System.out.print("请输入圆1的坐标以及半径:"); 
        double ox1 = input.nextDouble(); 
        double oy1 = input.nextDouble(); 
        double r1 = input.nextDouble(); 
        System.out.print("请输入圆2的坐标以及半径:");
        double ox2 = input.nextDouble(); 
        double oy2 = input.nextDouble(); 
        double r2 = input.nextDouble();
        double distance = Math.sqrt(Math.pow(ox1 - ox2,2) + Math.pow(oy1 - oy2,2));
        if (distance <= r1 - r2) { 
            System.out.println("圆2在圆1内"); 
        } else if (distance < r1 + r2) { 
            System.out.println("圆2与圆1相交"); 
        } else { 
            System.out.println("圆2在圆1外"); 
        } 
    } 
}

求两个数字的最大公约数

import java.util.Scanner; 
public class Demo{ 
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in); 
        System.out.print("请输入两个数字:"); 
        int num1 = input.nextInt(); 
        int num2 = input.nextInt(); 
        int min = num1 < num2 ? num1 : num2;
        int gcd = 1;
        for (int i = min; i > 0; i--) { 
            if (num1 % i == 0 && num2 % i == 0) { 
                gcd = i; break; 
            } 
        }
        System.out.println("最大公约数为:" + gcd); 
    } 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值