zoukankan      html  css  js  c++  java
  • java入门(4)--流程控制

    选择

    程序如果只是逐条地顺序执行,那程序的行为恐怕要简单得多了,但也会失去大部分的强悍功能和精彩。

    正是“分支”打破了顺序执行的呆板局面,给程序注入了真正的生命力。

    java中的分支主要由 选择循环语句提供,其语法基本与 c 语言相同。

    if...else... 恐怕是最为我们熟悉的了。它有多种表现形式。

    if(条件) 语句;

    if(条件){

    语句1;

    语句2;
    ...

    }

    有的编码规范上要求,即便是只有一条语句,也要放在大括号中。

    if(条件){

    语句

    }

    else {

    语句

    }

    if(条件){

    语句

    }

    else if(条件2){

    语句

    }

    else if(条件3){

    语句

    }

    else{

    语句

    }

    下面以多种风格求 a,b,c三个数中最大的数。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int a = 15;
     5         int b = 12;
     6         int c = 29;
     7         
     8         int max = a;
     9         if(b>max) max = b;
    10         if(c>max) max = c;
    11         
    12         System.out.println(max);
    13     }
    14 }

    这种方式简明易懂,只用 if 没有 else

    逻辑上先假设 a 是最大的,然后谁比它大,谁就成为新任盟主。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int a = 15;
     5         int b = 12;
     6         int c = 29;
     7                 
     8         if(a>=b && a>=c) System.out.println(a);
     9         if(b>=a && b>=c) System.out.println(b);
    10         if(c>=a && c>=b) System.out.println(c);
    11     }
    12 }

    这种方法也很容易理解。要注意是大于等于,不是大于哦,如果出现了相等数字 ....

    其实这还是有个 bug 的, 假如 3 个数字相等, 就会输出 3 次的。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int a = 15;
     5         int b = 12;
     6         int c = 29;
     7                 
     8         if(a>b){
     9             if(a>c)
    10                 System.out.println(a);
    11             else
    12                 System.out.println(c);
    13         }
    14         else{
    15             if(b>c)
    16                 System.out.println(b);
    17             else
    18                 System.out.println(c);
    19         }
    20     }
    21 }

    这种方法有点点绕人,当待比较的数字更多的时候,简直就灾难了...

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int a = 15;
     5         int b = 12;
     6         int c = 29;
     7     
     8         if(a>=b && a>=c)
     9             System.out.println(a);
    10         else if(b>=a && b>=c)
    11             System.out.println(b);
    12         else
    13             System.out.println(c);
    14     }
    15 }

    这样也挺好吧? 不会担心数字重复的问题。

     如果有很多个else if 这样的逻辑分支,多数情况可以用 switch 语句来代替,代码更清楚一些。

    如下的代码把分数转化为评语。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int score = 85;
     5         switch(score/10){
     6             case 9:
     7                 System.out.println("优秀");
     8                 break;
     9             case 8:
    10                 System.out.println("良好");
    11                 break;
    12             case 7:
    13                 System.out.println("中等");
    14                 break;
    15             case 6:
    16                 System.out.println("及格");
    17                 break;
    18             default:
    19                 System.out.println("不及格");
    20         }
    21     }
    22 }

    注意,switch 的括号内只能是可枚举的类型,比如:int char 等。用 String 类型是不可以的。

    初学者还要注意,每个 case 块内,不要忘记了 break 语句,否则,程序在执行完该块的内容后还会继续执行下一块中的代码,这往往不是我们所希望的。

    循环

    java 与 c 语言一样,提供了 while for 等循环构造手段。

    最简单的循环莫过于“死循环”。可以通过 while(true) 来实现。

    下面的代码打印出 10000 以内  5 的幂。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int x = 1;
     5         while(true){
     6             if(x > 10000) break; 
     7             System.out.println(x);
     8             x *= 5;
     9         }
    10     }
    11 }

    死循环一般总是要配合 break 语句,以便在适当的时机跳出循环外。

    这个跳出条件也可以转化为“不跳出条件”,直接写在 while 的括号中。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int x = 1;
     5         while(x<=10000){
     6             System.out.println(x);
     7             x *= 5;
     8         }
     9     }
    10 }

    观察这里的 x ,如果你写过很多循环逻辑,就会发现规律:

    这里的变量总是执行:初始化控制跳出时机,改变自身 这3件事情。

    for 语句就可以把这 3 件事情 收纳于一身。

    上面的代码逻辑用 for 表示起来就会更加简洁:

    1 public class A0407
    2 {
    3     public static void main(String[] args){
    4         for(int x=1; x<=10000; x *= 5){
    5             System.out.println(x);
    6         }
    7     }
    8 }

    但,一般情况下,我们更愿意用 for 循环来表达已知次数的循环。

    如下代码求出一个数组中的最大值。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         int[] a = {3,15,22,7,16,4,7};
     5         int max = a[0];
     6         for(int i=1; i<a.length; i++){
     7             if(a[i] > max) max = a[i];
     8         }
     9         System.out.println(max);
    10     }
    11 }

    我们需要注意,for 循环中的控制变量 i 的作用域范围仅仅限于 for 语句块内。

    也就是说,如果紧跟着还有一个 for 语句,它也可以再定义 变量 i, 不必要换另一个名字。

    在循环中,总可以通过 break 语句提前跳出来,也可以通过 continue 语句越过本轮循环,跳到下一轮(但不跳出循环)。

    下面的代码模拟的是一个叫做“拍七”的小游戏。

    这个游戏要求大家从1 到 100 轮流报数,但碰到含有 7 或 7 的倍数的数字要跳过去。

     1 public class A0407
     2 {
     3     public static void main(String[] args){
     4         for(int i=1; i<=100; i++){
     5             if(i%10==7) continue;
     6             if(i/10==7) continue;
     7             if(i%7==0) continue;
     8             System.out.println(i);        
     9         }
    10     }
    11 }

    return 语句

    除了选择和循环, return 语句也是常用的流程控制手段。

    下面的代码,把判断一个数字是否为素数封装为一个方法来调用。

    所谓素数就是除了 1 和它本身外,再没有其它数字可以整除它。

     1 public class A0407
     2 {
     3     static boolean sushu(int n){
     4         for(int i=2; i*i<=n; i++){
     5             if(n%i==0) return false;
     6         }
     7         return true;
     8     }
     9     
    10     public static void main(String[] args){
    11         System.out.println(sushu(3));
    12         System.out.println(sushu(4));
    13         System.out.println(sushu(5));
    14         System.out.println(sushu(91));
    15     }
    16 }
  • 相关阅读:
    Java8新特性简介
    责任链模式
    Bean的生命周期
    APP中https证书有效性验证引发安全问题(例Fiddler可抓https包)
    程序员成长指南
    Go 代码性能优化小技巧
    Go slice 扩容机制分析
    一次 Go 程序 out of memory 排查及反思
    curl 常用操作总结
    Go benchmark 详解
  • 原文地址:https://www.cnblogs.com/gyhang/p/8728908.html
Copyright © 2011-2022 走看看