java流程控制及其循环语句

  1. 用户交互Scanner(IO流跟电脑打交道)

    • 基础语法:Scanner s = new Scanner (System.in); //表示系统数据输入

    ​ Scanner s = new Scanner (System.out); //表示系统数据输出

    ​ scanner.close:表示关闭数据流

    • 通过Scanner类的next( ) 与nextLine( ) 方法获取输入的字符串,在读取前我们一般需要使用 hasNEXT( ) 与hasNextLine( ) 判断是否还有输入的数据

    • if判断语句

              Scanner scanner = new Scanner(System.in);   //创建一个扫描对象,用于接受键盘数据
              System.out.println("使用next方式接受:");
              if (scanner.hasNext()){                     //判断用户是否输入字符串,在hasNext()后面不写内容默认
                  String str = scanner.next();            //使用next方式接受
                  System.out.println("输入的内容为:"+str);
                   }
              scanner.close();                            //关闭数据流 
      
      使用next方式:
      HELLO WORD
      输出内容为:HELLO
      
      #以空格为结束符
      Scanner scanner = new Scanner(System.in); 跟 scanner.close();中的命名必须一致————scanner命名
      
              Scanner scanner = new Scanner(System.in);   //从键盘接受数据
              System.out.println("使用nextLine方式接受");
              if (scanner.hasNextLine()) {                //判断是否有输入
                  String str = scanner.nextLine();        //定义使用nextLine方式接受
                  System.out.println("输出内容为"+str);
              }
              scanner.close();
      
      使用nextLine方式接受
      hello word
      输出内容为hello word
      
      #以回车为结束符
      
              Scanner scanner = new Scanner(System.in);
              int i = 0;
              float f = 0.0F;
              System.out.println("请输入数");
              if (scanner.hasNextInt()){
                  i = scanner.nextInt();
                  System.out.println("整数为:"+i);
              }
              else {
                  System.out.println("输入的不是整数");
              }
              if (scanner.hasNextFloat()){
                  f = scanner.nextFloat();
                  System.out.println("小数为:"+f);
              }
              else{
                  System.out.println("输入的不是小数");
              }
              scanner.close();
      
      请输入数
      你好
      输入的不是整数
      输入的不是小数
          
      请输入数
      1
      整数为:1
      2.6487
      小数为:2.6487
      
  2. 顺序结构(java基本结构)

    从上而下,一句一句的走

     public static void main(String[] args) {
            System.out.println("hello word1");
            System.out.println("hello word2");
            System.out.println("hello word3");
            System.out.println("hello word4");
        }
    
    hello word1
    hello word2
    hello word3
    hello word4
    
  3. 选择结构

    • if单选择结构

      if(表达式){
          // 如果为true就执行,如果为false就不执行,显示结束
      }
      
              Scanner s = new Scanner(System.in);
              System.out.println("请输入内容:");
              String m = s.nextLine();
              if (m.equals("hello")){
                  System.out.println(m);
              }
      
              s.close();
      
      请输入内容:
      hello
      hello
      
    • if双选择结构

              Scanner s = new Scanner(System.in);
              System.out.println("请输入内容:");
              int m = s.nextInt();
              if(m<60){
                  System.out.println("不及格");
              }
              else{
                  System.out.println("及格");
              }
              s.close();
      
      请输入内容:
      60
      及格
      
    • if多选择结构

              Scanner s = new Scanner(System.in);
              System.out.println("请输入内容:");
              int m = s.nextInt();
              if(m<60){
                  System.out.println("不及格");
              }
              else if(m<80){
                  System.out.println("良好");
              }
              else{
                  System.out.println("优秀");
              }
              s.close();
      
      请输入内容:
      90
      优秀
      
    • 嵌套的if结构

              Scanner s = new Scanner(System.in);
              System.out.println("请输入内容:");
              int m = 0;
              while(m<10){
                  m++;
                  if(m<=5){
                      System.out.println("你好");
                  }
                  else{
                      System.out.println("好好学习");
                  }
              }
              s.close();
      
      请输入内容:
      你好
      你好
      你好
      你好
      你好
      好好学习
      好好学习
      好好学习
      好好学习
      好好学习
      
    • switch多选择结构

              Scanner s = new Scanner(System.in);
              System.out.println("请输入内容:");
              int m = 60;
              switch(m){                            //可以是字符,数字,汉字
                  case 50:
                      System.out.println("不及格");
                      break;                        //
                  case 60:
                      System.out.println("及格");
                      break;
                  case 80:
                      System.out.println("优秀");
                      break;
                  default:
                      System.out.println("未知字符"); //可以去掉不使用
              }
              s.close();
      
      请输入内容:
      及格
      
  4. 循环结构

    while : 如果你不加结束命令他会一直走

           Scanner s = new Scanner(System.in);
           double sum=0;
           int m=0;
            System.out.println("请输入数字:");
           while (s.hasNextDouble()){ 
               double g =s.nextDouble();            //将系统输入的值进行一个定义
               m=m+1;                               // m++跟m=m+1都表示一样的含义
               sum=sum+g;
           }
            System.out.println(m+"个数的和为"+sum);
            System.out.println(m+"平均数为"+(sum/m));
            s.close();
    
    请输入数字:
    10
    60
    90
    100
    x
    4个数的和为260.0
    4平均数为65.0
    
           Scanner s = new Scanner(System.in);
           double sum=0;
           int m=0;
            System.out.println("请输入数字:");
           while (s.hasNextDouble()){
               double g =s.nextDouble();
               m++;
               sum=sum+g;
               System.out.println("你输入了第:"+m+"数据"+"当前和为"+sum);
           }
            System.out.println(m+"个数的和为"+sum);
            System.out.println(m+"平均数为"+(sum/m));
            s.close();
    
    请输入数字:
    10
    你输入了第:1数据当前和为10.0
    60
    你输入了第:2数据当前和为70.0
    90
    你输入了第:3数据当前和为160.0
    100
    你输入了第:4数据当前和为260.0
    x
    4个数的和为260.0
    4平均数为65.0
    
    int i = 0;
    int sum = 0;
    while(i<10){
        sum+=i;
        i++;
    }
    System.out.println("数的和为:"+sum);
    
    数的和为:45
    
  5. break & continue(跳出循环)

    break:表示终止命令(辞职)

    continue:表示暂时终止一段(请假)

  6. 练习

    while(ture){              // 进入while死循环
    }
    
    • for循环求奇,偶之和
     public static void main(String[] args) {
            int j =0;
            int o =0;
            for (int s = 0; s < 100; s++) {      // 在for循环中先输出s的值在将s加一
                if(s%2==0){
                    j =+s;
                }
                else{
                    o =+s;
                }
            }
            System.out.println("奇数的和为:"+j);   // 如果输出的是最后总和一定要把输出放到for同齐列
            System.out.println("偶数的和为:"+o);
    
        }
    
    奇数的和为:98
    偶数的和为:99
    
    • 用for语句写出16列的偶数
      public static void main(String[] args) {
              for (int s = 0; s < 100; s++) {
                  if (s % 2 == 0) {
                      System.out.print(s + "\t");
                  }
                  if (s % (2 * 16) == 0) {
                      System.out.println();
                  }
      
              }
          }
                                                      // 0除以2跟0除以2*16都是等于零,所以0单独一行   
      0                                                                                               
      2	4	6	8	10	12	14	16	18	20	22	24	26	28	30	32	
      34	36	38	40	42	44	46	48	50	52	54	56	58	60	62	64	
      66	68	70	72	74	76	78	80	82	84	86	88	90	92	94	96	
      98	
    
    • 用for循环写九九乘法表
    for (int s = 1; s <= 9; s++) {
                for (int m = 1; m <= s; m++) {
                    System.out.print(m+"*"+s+"="+(m*s)+"\t");  //两数相乘一定要加括号
                }
                System.out.println();                          //换行是在表达式输出完成之后在总循环结束之前
            }
    
    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	
    
    • 输出数组—int[ ] m={ }; 在定义数组的时候一定要在类后面加[ ]号
    public static void main(String[] args) {
            int[] m = {20, 60, 80, 100};
            for (int s = 0; s <4; s++) {
                System.out.println(m[s]);       //输出的时候for循环中定义的在[]号内定义的类在[]外
            }
        }
    
    
    public static void main(String[] args) {
            int[] m = {20, 60, 80, 100};
            for (int s :m) {                   //定义的s:定义的类m(声明语句:表达式)
                System.out.println(m[s]);       //输出的时候for循环中定义的在[]号内定义的类在[]外
            }
        }
    
    20
    60
    80
    100
    
    • 打印三角形(采用的是迭代循环的方式——先执行最外层的,然后内层的循环执行,直到不满足题意为止)
        for (int i = 1; i <= 5; i++) {
             for (int r = 5; r >=i; r--) {
                 System.out.print(" 1 ");
                 }
             for (int r = 1; r <= i; r++) {
                 System.out.print(" * ");
             }
             for (int r = 1; r < i; r++) {
                 System.out.print(" * ");
             }
             System.out.println();
         }
         System.out.println("=====================");
    
         for (int s = 1; s <= 5; s++) {
             for (int k = 1; k <= s; k++) {
                 System.out.print(" * ");
             }
             System.out.println();
         }
         1  1  1  1  1  * 
         1  1  1  1  *  *  * 
         1  1  1  *  *  *  *  * 
         1  1  *  *  *  *  *  *  * 
         1  *  *  *  *  *  *  *  *  * 
         =====================
         * 
         *  * 
         *  *  * 
         *  *  *  * 
         *  *  *  *  * 
    
    
    
    • do whlie和while
    int i=0;
            while (i<5) {
                i++;
                System.out.println(i);
            }
    
            System.out.println("===========================");
    
            int j=0;
            do {
                System.out.println(j);
                j++;
            }
            while (j<5);
    
    1
    2
    3
    4
    5    //while循环
    ===========================
    0
    1
    2
    3
    4    //do while循环
    
  7. 快捷键

    • 点击 ctrl + scanner : 可以看到scanner的编写代码

    • ctrl + F12 : 进入代码结构

    • file--project strcet : 打开文档结构

    • 看class文件代码: 打开文档结构--复制地址--去文件资源管理器--打开class文件用idea

    • 输入 100.fors 会自动显示 for (int s = 0; s < 100; s++) {

    • debug:通过debug能看到程序运行的过程,方便差错

posted @   FoxDusk  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示