zoukankan      html  css  js  c++  java
  • 03_Java流程控制

     1 /**
     2   * 需求: 随机产生一个1-100内的数,代表人的年龄,根据年龄输出是什么年级段的人
     3   *          青少年 [10  -  20)
     4   *          壮年 [20  -  30)
     5   *          中年 [30  -  50)
     6   *          老年 [50  -  80)
     7   *          晚年 [80  -  )
     8  * @author jliu.l
     9  * @2020年7月3日
    10  * 
    11  */
    12 public class Demo01 {
    13 
    14     public static void main(String[] args) {
    15         Scanner scanner = new Scanner(System.in);
    16         //准备数据
    17         System.out.print("请输入年龄:");
    18         int    age = scanner.nextInt();
    19         
    20         //处理数据
    21         switch(age/10) {
    22         case 1:
    23             System.out.println("青少年");
    24             break;
    25         case 2:
    26             System.out.println("壮年");
    27             break;
    28         case 3:
    29         case 4:
    30             System.out.println("中年");
    31             break;
    32         case 5:
    33         case 6:
    34         case 7:
    35             System.out.println("老年");
    36             break;
    37         default :
    38             System.out.println("晚年");
    39         }
    40     }
    41 }
     1 /**
     2   * 需求:用while和for循环计算100以内奇数及偶数的和,并输出。
     3  * @author jliu.l
     4  * @2020年7月3日
     5  * 
     6  */
     7 public class Demo02 {
     8     
     9     public static void main(String[] args) {
    10         forMethod();
    11         whileMethod();
    12 
    13     }
    14     
    15     /**
    16      * 
    17      */
    18     private static void forMethod() {
    19         // TODO 自动生成的方法存根
    20         int jcount = 0;
    21         int ocount = 0;
    22         for(int i =1;i <=100;i++) {
    23             if(i %2 == 0) {
    24                 ocount +=i;
    25             }else {
    26                 jcount +=i;
    27             }
    28         }
    29         System.out.println("for循环");
    30         System.out.println("100以内偶数和:"+ocount);
    31         System.out.println("100以内奇数和:"+jcount);
    32     }
    33 
    34     /**
    35      * 
    36      */
    37     private static void whileMethod() {
    38         // TODO 自动生成的方法存根
    39         int i = 0;
    40         int jcount = 0;
    41         int ocount = 0;
    42         while (i <= 100) {
    43             if(i %2 == 0) {
    44                 ocount +=i;
    45             }else {
    46                 jcount +=i;
    47             }
    48             i++;
    49         }
    50         System.out.println("while循环");
    51         System.out.println("100以内偶数和:"+ocount);
    52         System.out.println("100以内奇数和:"+jcount);
    53     }
    54 }
     1 /**需求:用while和for循环输出1-1000之间能被5整除的数,且每行输出3个。
     2  * @author jliu.l
     3  * @2020年7月3日
     4  * 
     5  */
     6 public class Demo03 {
     7 
     8     /**
     9      * @param args
    10      */
    11     public static void main(String[] args) {
    12         System.out.println("调用for循环:");
    13         System.out.println("-------------------------------------");
    14         forMethod();
    15         System.out.println("调用while循环:");
    16         System.out.println("-------------------------------------");
    17         whileMethod();
    18     }
    19 
    20     /**
    21      * 
    22      */
    23     private static void whileMethod() {
    24         // TODO 自动生成的方法存根
    25         int count  = 0;    //每行输出的个数
    26         for(int i = 1;i <= 1000;i++) {
    27             if(i % 5 ==0) {
    28                 count++;
    29                 System.out.print(i+"	");
    30                 if(count % 3==0) {
    31                     System.out.println();
    32                 }
    33             }
    34         }
    35         System.out.println("
    "+"-------------------------------------");
    36         
    37     }
    38 
    39     /**
    40      * 
    41      */
    42     private static void forMethod() {
    43         // TODO 自动生成的方法存根
    44         int count  = 0;    //每行输出的个数
    45         int i = 1;
    46         while(i <= 1000) {
    47             if(i % 5 ==0) {
    48                 count++;
    49                 System.out.print(i+"	");
    50                 if(count % 3==0) {
    51                     System.out.println();
    52                 }
    53             }
    54             i++;
    55         }
    56         System.out.println("
    "+"-------------------------------------"+"
    ");
    57     }
    58 }
     1 /**
     2   * 随机产生一个数(1-1000),如果这个数能被37整除,则结束整个程序,
     3   * 并输出这个数及随机次数,如果这个数能被2整除,则继续产生随机数并输出。
     4  * @author jliu.l
     5  * @2020年7月3日
     6  * 
     7  */
     8 public class Demo10 {
     9     
    10     public static void main(String[] args) {
    11         int count = 0;
    12         int num = 0;
    13         
    14         while(true) {
    15             Boolean b =false;
    16             num = (int) (Math.random()*1000+1);
    17             count++;
    18             if(!b)
    19             System.out.println("第"+count+"次随机:"+num);
    20             if(num %2 ==0) {
    21                 b = true;
    22                 System.out.println("第"+count+"次随机:"+num+"能被2整除");
    23                 continue;
    24             }
    25 
    26             if(num % 37 == 0) {
    27                 System.out.println(num+"能被37整除");
    28                 break;
    29             }
    30             
    31             
    32         }
    33         
    34         System.out.println(count);
    35     }
    36 }
     1 /**
     2  * 需求:产生随机数(1-100),当随机数是88的时候,我们就停止,并且计算产生了几次循环!
     3  * @author jliu.l
     4  * @2020年7月3日
     5  * 
     6  */
     7 public class Demo04 {
     8 
     9     /**
    10      * @param args
    11      */
    12     public static void main(String[] args) {
    13         // TODO 自动生成的方法存根
    14         int count = 0;
    15         while(true) {
    16             count++;
    17             int num = (int)(Math.random()*99+1);
    18             System.out.println(num);
    19             if(num == 88) {
    20                 break;
    21             }  
    22         }
    23         System.out.println("计算产生了循环"+count+"次");
    24     }
    25 }
     1 /**
     2  * 需求:实现十进制正数转化为二进制数。
     3  * @author jliu.l
     4  * @2020年7月3日
     5  * 
     6  */
     7 public class Demo05 {
     8 
     9     /**
    10      * @param args
    11      */
    12     public static void main(String[] args) {
    13         // TODO 自动生成的方法存根
    14         Scanner scanner = new Scanner(System.in);
    15         System.out.print("请输入一个正整数:");
    16         int num = scanner.nextInt();
    17         int mod = 0;    //存放余数
    18         int sum = 0; //存放和
    19         int k = 1;    //控制位数
    20         int num1 = num;
    21         
    22         while(num != 0) {
    23             mod = num% 2;
    24             num /= 2;
    25             //System.out.print(mod);
    26             sum = sum + mod * k;
    27             k *= 10;
    28         }
    29         System.out.println("十进制数:"+num1+"  转为二进制为:"+sum);
    30     }
    31 }
     1 /**
     2  * 闰年的判断规则如下:
     3  * (1)若某个年份能被4整除但不能被100整除,则是闰年。
     4  * (2)若某个年份能被400整除,则也是闰年
     5  * @author jliu.l
     6  * @2020年7月3日
     7  * 
     8  */
     9 public class Demo08 {
    10     public static void main(String[] args) {
    11         
    12         System.out.print("请输入一个年份:");
    13         @SuppressWarnings("resource")
    14         int year = new Scanner(System.in).nextInt();
    15         
    16         if( ((year %4 == 0)&& (year % 100== 0)) || year%400==0 ) {
    17             System.out.println(year+"是闰年");
    18         }else {
    19             System.out.println(year+"不是闰年");
    20         }    
    21     }
    22 }
     1 /**
     2  * 使用do-while实现:随机产生100个数,
     3  * 如果该数能够被3整数,则输出"该数能被3整除"
     4  * 如果该数能够被5整数,则输出"该数能被5整除"
     5  * 如果该数能够被3整数同时也能被5整除,则输出"该数能被3和5同时整除"
     6  * @author jliu.l
     7  * @2020年7月3日
     8  * 
     9  */
    10 public class Demo09 {
    11 
    12     public static void main(String[] args) {
    13         // TODO 自动生成的方法存根
    14         int i =0;
    15         do {
    16             int num = (int) (Math.random()*100+1);
    17             if(num %3==0 && num %5==0) {
    18                 System.out.println(num+"该数能被3和5同时整除");
    19             }else if(num %5==0) {
    20                 System.out.println(num+"该数能被5整除");
    21             }else if(num %3==0) {
    22                 System.out.println(num+"该数能被3整除");
    23             }
    24             i++;
    25         } while (i<100);
    26     }
    27 }
     1 /**
     2  * 功能:有限5位数,个位数为6且能被3整除的五位数共有多少?
     3  * @author jliu.l
     4  * @2020年7月4日
     5  * 
     6  */
     7 public class Demo01 {
     8 
     9     /**
    10      * @param args
    11      */
    12     public static void main(String[] args) {
    13         // TODO 自动生成的方法存根
    14         int count  = 0;
    15         for(int i = 10000;i<100000;i++) {
    16             int gw = i % 10;    //取个位
    17             if(gw == 6 && (i%3==0)) {
    18                 count++;
    19                 System.out.println("个位数为6且能被3整除的五位数有:"+i);
    20             }
    21         }
    22         System.out.println("个位数为6且能被3整除的五位数共有:"+count);
    23     }
    24 }
     1 /**
     2  * 功能完全数:如果一个数恰好等于他的因子之和,则成为“完全数”。
     3  * 如:6的因子是1、2、3,而6=1+2+3,则6是个“完全数”。试求出1000以内的全部“完全数”。
     4  * @author jliu.l
     5  * @2020年7月4日
     6  * 
     7  */
     8 public class Demo02 {
     9 
    10     /**
    11      * @param args
    12      */
    13     public static void main(String[] args) {
    14         // TODO 自动生成的方法存根
    15         for(int i = 2;i<=1000;i++) {
    16             int count = 0;
    17             for(int k = 1;k<i;k++) {
    18                 if(i % k == 0) {
    19                     count += k;
    20                 }
    21             }
    22         if(count == i) {
    23             System.out.println(count);
    24             }
    25         }
    26     }
    27 }
    /**
     * 功能:学校2009年培养学生900人,每年增长25%,
     * 请问按此速度增长,到哪一年培训学生人数将达到1万人
     * @author jliu.l
     * @2020年7月4日
     * 
     */
    public class Demo03 {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            // 900*(1.25)*1.25
            int count = 0;
            while(true) {
                count++;
                double k = count*(1+0.25);
                if(900*k >10000) {
                    System.out.println(900*k+"按此速度增长,到"+(2009+count)+"年培训学生人数将达到1万人");
                    break;
                }
            }
        }
    }
    /**
     * 实现用户登录时的信息验证:登录时提示用户输入用户名和密码。
     * 验证用户名和密码的正确性系统提供用户有3次机会输入用户名和密码,
     * 如果超过3次仍不能匹配,程序结束。
     * @author jliu.l
     * @2020年7月4日
     * 
     */
    public class Demo04 {
    
        public static void main(String[] args) {
            int count = 0;
            int num = 3;
            while(true) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("输入用户名:");
                String username = scanner.nextLine();
                System.out.print("输入密码:");
                String pws = scanner.nextLine();
                
                if("admin".equals(username) && "1234".equals(pws)) {
                    System.out.println("登陆成功......");
                    break;
                }else {
                    count++;
                    System.out.println("用户名或密码不正确!请重新输入。你还有"+(num-count)+"次机会。");
                }
                if(count >= 3) {
                    System.out.println("你已经输入3次,程序结束");
                    break;
                }
            }    
        }
    }
     1 /**
     2  * 功能:四个连续的3位自然数的和是一个在400到440之间的三位数,
     3  * 并且能被9整除,这四个自然数分别是多少?
     4  * @author jliu.l
     5  * @2020年7月4日
     6  * 
     7  */
     8 public class Demo05 {
     9 
    10     /**
    11      * @param args
    12      */
    13     public static void main(String[] args) {
    14         // TODO 自动生成的方法存根
    15         for(int i =100;i<120;i++) {
    16             int count = i+i+1+i+2+i+3;
    17             if( ((count>=400) && (count<= 440)) && (count % 9 == 0)) {
    18                 System.out.println(i+"	"+(i+1)+"	"+(i+2)+"	"+(i+3)+"	");
    19             }
    20         }
    21     }
    22 }
     1 /**
     2  * 需求:从控制台输入3个整数,然后按从小到大排序出来
     3  * @author jliu.l
     4  * @2020年7月2日
     5  * 
     6  */
     7 public class Demo01 {
     8 
     9     public static void main(String[] args) {
    10         //准备数据
    11         Scanner sc = new Scanner(System.in);
    12         System.out.print("请任意输入三个整数,以空格隔开:");
    13         int a = sc.nextInt();
    14         int b = sc.nextInt();
    15         int c = sc.nextInt();
    16         System.out.print("排序前的顺序为:"+a+"		"+b+"	"+c+"
    ");
    17         
    18         //处理输出数据
    19         if(a>b) {
    20             if(c<b) {
    21                 System.out.println("从小到大排序后:"+c+"		"+b+"	"+a);
    22             }else if(c>a) {
    23                     System.out.println("从小到大排序后:"+b+"		"+a+"	"+c);
    24             }else {
    25                     System.out.println("从小到大排序后:"+b+"		"+c+"	"+a);
    26             }
    27         }else {    //a<b
    28             if(c<a) {
    29                 System.out.println("从小到大排序后:"+c+"		"+a+"	"+b);
    30             }else if (c>b) {
    31                 System.out.println("从小到大排序后:"+a+"		"+b+"	"+c);
    32             }else {
    33                 System.out.println("从小到大排序后:"+c+"		"+b+"	"+a);
    34             }
    35         }
    36     }
    37 }
  • 相关阅读:
    Python-Basis-9th
    Python-Basis-8th
    Python-Basis-7th
    Ubuntu-Basis-4th
    Ubuntu-Basis-3rd
    Ubuntu-Basis-2nd
    Ubuntu-Basis-1st
    疯狂java第五章&&第六章-面向对象
    疯狂java第四章-流程控制与数组
    疯狂java第三章-数据类型和运算符
  • 原文地址:https://www.cnblogs.com/jliu-l/p/13234019.html
Copyright © 2011-2022 走看看