程序流程控制
发布日期:2021-05-10 18:25:26 浏览次数:18 分类:精选文章

本文共 4150 字,大约阅读时间需要 13 分钟。

##Java���������������������������

���������Java���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

###������������

####switch-case������

Switch-case���������Java���������������������������������������������������������������������������������������������

  • ���������������switch(���������������) {

    case ���������1: ������������
    case ���������2: ������������
    ...
    default: ������������
    }

  • ������������������������������������������������������������������������������

  • ���������������������������������������������������������������

  • ���������������

    public class Structure {      public static void main(String[] args) {          int value = 15;          switch (value) {              case 11:                  System.out.println("11");                  break;              case 15:                  System.out.println("15");                  break;              default:                  System.out.println("���������");          }      }  }

    ###������������

    ������������������������������������������������������

    ####for������

    for������������������������������������������������������

    for(���������; ������������; ������������)

    ���������������

    public class Structure {      public static void main(String[] args) {          Scanner scan = new Scanner(System.in);          System.out.println("������������������������");          int m = scan.nextInt();          int n = scan.nextInt();          System.out.println("������������������" + lcm(m, n));          System.out.println("������������������" + gcd(m, n));      }      private static int lcm(int m, int n) {          return m * n / gcd(m, n);      }      private static int gcd(int m, int n) {          int min = (m < n) ? m : n;          for (int i = min; i > 0; i--) {              if (n % i == 0 && m % i == 0) {                  return i;              }          }          return 1;      }  }

    ####while������

    while������������������������������������������������

    while(���������������)

    ���������������

    public class Structure {      public static void main(String[] args) {          Scanner scan = new Scanner(System.in);          System.out.println("������������������������");          int m = scan.nextInt();          int n = scan.nextInt();          System.out.println("������������������" + findLcm(m, n));          System.out.println("������������������" + findGcd(m, n));      }      private static int findLcm(int m, int n) {          int i = 1;          while (i < m * n / gcd(m, n)) {              if ((m * i) % n == 0) {                  return m * i;              }              i++;          }          return m * n;      }      private static int findGcd(int m, int n) {          int min = (m < n) ? m : n;          int j = min;          while (j > 0) {              if (n % j == 0 && m % j == 0) {                  return j;              }              j--;          }          return 1;      }  }

    ####do-while������

    do-while���������while������������������������

    do {
    ���������
    } while(������)

    do-while���������������������������������������������������������������������������

    ���������������

    public class Structure {      public static void main(String[] args) {          Scanner scan = new Scanner(System.in);          System.out.println("������������������");          int num = scan.nextInt();          System.out.println("������������������������");          do {              System.out.println(num);              num /= 10;          } while (num != 0);      }  }

    ###break���continue

    ���������������������break���continue���������������������������

    • break���������������������������������������
    • continue���������������������������������������������������������

    ���������

    public class Structure {      public static void main(String[] args) {          for (int i = 0; i < 10; i++) {              System.out.println("i = " + i);              if (i > 5) {                  break;              }          }      }  }

    ������������������������������������������������������Java������������������������������

    上一篇:项目1:家庭收支记录软件
    下一篇:JAVA变量和运算符

    发表评论

    最新留言

    不错!
    [***.144.177.141]2025年04月29日 19时52分01秒