zoukankan      html  css  js  c++  java
  • No1_2. 流程控制_java学习笔记

      1 import java.util.Scanner;
      2 import java.lang.Math;
      3 
      4 public class HelloForWhile {
      5 
      6     /**
      7      * 文档注释,程序名称:HelloForWhile 流程控制 开发时间:2016-03-07 作者:嘿嘿
      8      * */
      9     public static void main(String[] args) {
     10         // TODO Auto-generated method stub
     11         System.out.println("test");
     12         int y = 50;
     13         int x = 800;
     14 
     15         // 复合语句又称块语句,用{}括起来
     16         {
     17             boolean z = y > x;
     18             System.out.println("【1】y>x成立吗?" + z);
     19         }
     20 
     21         // if语句,仅有一条语句可以省略大括号;
     22         if (x > y)
     23             System.out.println("【2】x大于y成立");
     24         // if...else...
     25         if (x > y) {
     26             System.out.println("【3】x大于y!");
     27         } else {
     28             System.out.println("【4】x小于等于y!");
     29         }
     30         // if...else if...多分支语句
     31         int math = 79;
     32         if (math > 90) {
     33             System.out.println("数学成绩“优”!");
     34         } else if (math > 80) {
     35             System.out.println("数学成绩“良”!");
     36         } else if (math > 60) {
     37             System.out.println("数学成绩“合格”!");
     38         } else {
     39             System.out.println("数学成绩“不及格”!");
     40         }
     41 
     42         // 多分支语句,switch语句表达式中的值必须是整型或者字符型。
     43         // 1.1,“ok”都是非法的
     44         int season = 3;
     45         System.out.println("输入个数字代表季节吧:" + season);
     46         switch (season) {
     47         case 1:
     48             System.out.println("Spring");
     49             break;
     50         case 2:
     51             System.out.println("Summer");
     52             break;
     53         case 3:
     54             System.out.println("August");
     55             break;
     56         case 4:
     57             System.out.println("Winter");
     58             break;
     59         default:
     60             System.out.print("season的值不是1-4的整数,不代表任何季节~");
     61         }
     62 
     63         // 示例一:验证登录信息的合法性
     64         Scanner scan = new Scanner(System.in);
     65         System.out.println("请输入用户名:");
     66         String username = scan.nextLine();
     67         System.out.println("请输入密码");
     68         String userpw = scan.nextLine();
     69         if (!username.equals("sunshine")) { // 为什么此处不可以用:(username="sunshine")
     70             System.out.println("用户名不正确!");
     71         } else if (!userpw.equals("123456")) {
     72             System.out.println("密码不正确!");
     73         } else {
     74             System.out.println("恭喜您,登录成功!");
     75         }
     76 
     77         // 示例二:为新员工分配部门
     78         System.out.println("员工姓名?");
     79         String staffname = scan.nextLine();
     80         System.out.println("员工编程语言?");
     81         String language = scan.nextLine();
     82 
     83         switch (language.hashCode()) { // 不清楚hashCode的用法
     84         case 3254818:
     85         case 2301506:
     86         case 2269730:
     87             System.out.println("员工" + staffname + "被分配到JAVA程序开发部门。");
     88             break;
     89         case 3140:
     90         case 2112:
     91             System.out.println("员工" + staffname + "被分配到C#程序开发部门。");
     92             break;
     93         default:
     94             System.out.println("本公司不需要" + language + "语言的程序人员。" + (char) 97);
     95 
     96         }
     97         int hash1 = "JAVA".hashCode();
     98         System.out.println(hash1);
     99 
    100         /******************************************
    101          * 循环语句(此处注释应该用于注释文档的)
    102          ****************************************/
    103         // while循环语句
    104         int tenadd = 1, addten = 0;
    105         while (tenadd <= 10) {
    106             addten = addten + tenadd;
    107             tenadd = tenadd + 1;
    108         }
    109         System.out.println("10以内的整数累加是" + addten);
    110 
    111         // do...while循环语句
    112         int doa = 100;
    113         while (doa == 60) {
    114             System.out.println("ok! doa=100");
    115             doa--;
    116         }
    117         int dob = 50;
    118         do {
    119             System.out.println("ok! dob=50,不判断条件,先执行该语句!!");
    120             dob--;
    121         } while (dob == 100);
    122 
    123         // for循环语句
    124         // for(表达式 1;表达式2;表达式3;){语句块}
    125         // 输出菱形!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    126 
    127         System.out.println("要输出菱行了,请输入单数整数行数");
    128         int n = scan.nextInt();
    129         for (int stari = -(n - 1) / 2; stari <= (n - 1) / 2; stari++) {
    130             for (int stara = 1; stara <= Math.abs(stari); stara++) {
    131                 System.out.print(" ");
    132             }
    133             for (int starj = 0; starj < -Math.abs(stari) * 2 + n; starj++) {
    134                 System.out.print("*");
    135             }
    136             System.out.println();
    137         }
    138 
    139         // foreach语句~~在遍历数组时,为程序员提供方便(java5之后加的)
    140         // for(元素变量x:遍历对象obj){ 引用了x的JAVA语名;}
    141 
    142         int arr[] = { 5, 2, 0 };
    143         System.out.println("一维数组中的元素分别为:");
    144         for (int fx : arr) {
    145             System.out.print(fx + "	");
    146         }
    147         System.out.print("
    ");
    148 
    149         // 示例三:使用while循环遍历数组
    150         String[] season1 = new String[] { "春", "夏", "秋", "冬" };
    151         System.out.println("一年四季有啥:");
    152         for (String fy : season1) {
    153             System.out.print(fy + " ");
    154         }
    155         System.out.println("
    " + "一年四季有啥:");
    156         int seasonCount = 0;
    157         while (seasonCount < season1.length) {
    158             System.out.print(season1[seasonCount++] + " ");
    159         }
    160         System.out.println();
    161 
    162         // 示例四:使用for循环输出九九乘法表
    163 
    164         for (int ninex = 1; ninex <= 9; ninex++) {
    165             for (int niney = 1; niney <= ninex; niney++) {
    166                 System.out.print(niney + "*" + ninex + "=" + ninex * niney
    167                         + "  ");
    168             }
    169             System.out.println();
    170         }
    171         /******************************************
    172          * 跳转语句(break
    eturncontinue)
    173          ****************************************/
    174         // break语句,强制退出循环。使用break语句计算1~100之间所有连续整数的和,但累加超1000跳出循环
    175         int sumHundred = 0;
    176         for (int sumx = 1; sumx <= 100; sumx++) {
    177             if (sumHundred > 1000) {
    178                 System.out.println(sumx + "以内的整数累加是" + sumHundred);
    179                 break;
    180             }
    181             sumHundred = sumHundred + sumx;
    182         }
    183         // continue语句,只能用在forwhiledo...while循环中,跳过其后面的语句,进行下次循环
    184         // 用while循环输出10以内的所有奇数~~
    185 
    186         int odd = 0;
    187         System.out.print("10以内所有奇数是:");
    188         while (odd <= 10) {
    189             if (odd % 2 == 0) {
    190                 odd = odd + 1;
    191                 continue;
    192             } else {
    193 
    194                 System.out.print(odd + " ");
    195                 odd = odd + 1;
    196             }
    197         }
    198         System.out.println();
    199 
    200         // return语句可以从一个方法返回,并把控制权交给调用它的语句;
    201         // return [表达式];
    202         /****************** 还没有实践这个语句。。 ***********************/
    203         // 示例五:终止循环
    204         // 中断单重循环的例子
    205         System.out.println("中断单重循环的例子,看到鬼就跳出来。");
    206         String[] array = new String[] { "钢铁侠", "金刚狼", "贞子", "变形金刚", "蝙蝠侠",
    207                 "贞子", "绿巨人" };
    208         for (String gost : array) {
    209             if (gost == "贞子") {
    210                 System.out.println("遇到鬼了");
    211                 break;
    212             } else {
    213                 System.out.println(gost);
    214             }
    215         }
    216         // 中断双重循环的例子
    217         int[][] myScores = new int[][] { { 78, 98, 67 }, { 80, 89, 80 },
    218                 { 78, 93, 99 } };
    219         System.out.println("寶寶這次考試成績:
     数学	 语文 	英语");
    220         No1: for (int[] is : myScores) {
    221             for (int q : is) {
    222                 System.out.println(q + "	");
    223                 if (q < 70) {
    224                     System.out.println("
        等等" + q + "分的是什么?这个为什么不及格?");
    225                     break No1;
    226                 }
    227             }
    228         }
    229         // 示例六:循环体的过滤器
    230         System.out.println("最近一直在追美剧,最近还看了贞子,查一下看了几遍?");
    231         int girlgost = 0;
    232         for (String gost1 : array) {
    233             if (gost1 == "贞子") {
    234                 girlgost = girlgost + 1;
    235                 continue;
    236             }
    237             System.out.println("最近看的美剧有" + gost1);
    238         }
    239         System.out.println("您一共看了" + girlgost + "次贞子");
    240 
    241         // 作业一:使用for循环输出空心的菱形
    242         // 作业二:使用for循环输出杨辉三解。。。。
    243         // 作业三:使用while循环语句计算1+1/2!+1/3!+。。。+1/20!之和
    244 
    245     }// main
    246 
    247 }// HelloForWhile

     输出结果:

    test
    【1】y>x成立吗?false2】x大于y成立
    【3】x大于y!
    数学成绩“合格”!
    输入个数字代表季节吧:3
    August
    请输入用户名:
    sunshine
    请输入密码
    123456
    恭喜您,登录成功!
    员工姓名?
    liuyq
    员工编程语言?
    java
    员工liuyq被分配到JAVA程序开发部门。
    2269730
    10以内的整数累加是55
    ok! dob=50,不判断条件,先执行该语句!!
    要输出菱行了,请输入单数整数行数
    45
                          *
                         ***
                        *****
                       *******
                      *********
                     ***********
                    *************
                   ***************
                  *****************
                 *******************
                *********************
               ***********************
              *************************
             ***************************
            *****************************
           *******************************
          *********************************
         ***********************************
        *************************************
       ***************************************
      *****************************************
     *******************************************
    *********************************************
     *******************************************
      *****************************************
       ***************************************
        *************************************
         ***********************************
          *********************************
           *******************************
            *****************************
             ***************************
              *************************
               ***********************
                *********************
                 *******************
                  *****************
                   ***************
                    *************
                     ***********
                      *********
                       *******
                        *****
                         ***
                          *
    一维数组中的元素分别为:
    5    2    0    
    一年四季有啥:
    春 夏 秋 冬 
    一年四季有啥:
    春 夏 秋 冬 
    1*1=1  
    1*2=2  2*2=4  
    1*3=3  2*3=6  3*3=9  
    1*4=4  2*4=8  3*4=12  4*4=16  
    1*5=5  2*5=10  3*5=15  4*5=20  5*5=25  
    1*6=6  2*6=12  3*6=18  4*6=24  5*6=30  6*6=36  
    1*7=7  2*7=14  3*7=21  4*7=28  5*7=35  6*7=42  7*7=49  
    1*8=8  2*8=16  3*8=24  4*8=32  5*8=40  6*8=48  7*8=56  8*8=64  
    1*9=9  2*9=18  3*9=27  4*9=36  5*9=45  6*9=54  7*9=63  8*9=72  9*9=81  
    46以内的整数累加是1035
    10以内所有奇数是:1 3 5 7 9 
    中断单重循环的例子,看到鬼就跳出来。
    钢铁侠
    金刚狼
    遇到鬼了
    寶寶這次考試成績:
     数学     语文     英语
    78    
    98    
    67    
    
        等等67分的是什么?这个为什么不及格?
    最近一直在追美剧,最近还看了贞子,查一下看了几遍?
    最近看的美剧有钢铁侠
    最近看的美剧有金刚狼
    最近看的美剧有变形金刚
    最近看的美剧有蝙蝠侠
    最近看的美剧有绿巨人
    您一共看了2次贞子
    View Code
  • 相关阅读:
    【转】从源码分析Handler的postDelayed为什么可以延时?
    android系统中如何通过程序打开某个AccessibilityService
    【转】在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
    【转】Android 增,删,改,查 通讯录中的联系人
    【转】Android调用Sqlite数据库时自动生成db-journal文件的原因
    【转】Android辅助功能AccessibilityService自动全选择文字粘贴模拟输入
    【转】Android中保持Service的存活
    Multi-label && Multi-label classification
    第2章 排序 || 第17节 三色排序练习题
    第2章 排序 || 第15节 有序数组合并练习题
  • 原文地址:https://www.cnblogs.com/sunshine-habit/p/5252476.html
Copyright © 2011-2022 走看看