Java流程控制

Scanner对象

  • 基本语法:Scanner s = new Scanner ()

使用next方式接收:

package com.Hellscythe.Scanner;

import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("使用next方式接收:");

        //判断用户有没有输入字符串
        if(scanner.hasNext()){
            //使用next方式接收
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);
            System.out.println(str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉

        scanner.close();
    }
}

使用nextLine方式接收:

package com.Hellscythe.Scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("使用nextLine方式接收:");

        if (scanner.hasNext());{
            //==ture可以省略
            String str = scanner.nextLine();
            System.out.println("输出的内容为:"+str);
            System.out.println(str);

        }

        scanner.close();


    }
}

注:使用next方式接收时遇到有效字符后的第一个空格终止

​ 使用nextLine方式接收时遇到回车终止

​ 代码能够给str赋值

去掉if结构

package com.Hellscythe.Scanner;

import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("输入文字:");

        String str = scanner.nextLine();
        System.out.println("输出的内容为:"+str);//不要if结构



        }

    }

Scanner的应用

package com.Hellscythe.Scanner;

import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        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("输入的不是整数数据");
        }
        System.out.println("请输入小数:");

        if (scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("小数数据:"+f);
        }else{
            System.out.println("输入的不是小数数据");
        }



        scanner.close();
    }
}
package com.Hellscythe.Scanner;

import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);//scanner范式

        //和,先定义一个数
        double sum = 0;
        //计算输入了多少个数字
        int m = 0;

        //通过循环判断是否还有输入,并对里面每一次进行求和和统计
        while (scanner.hasNextDouble()){
            double x = scanner.nextDouble();

            m = m + 1;//m++
            sum = sum + x;
            System.out.println("你输入了第"+m+"个数据,当前结果是"+sum);
        }//注意大括号,该大括号内为while语句,即循环语句。

        System.out.println(m+"个数的和为"+sum);
        System.out.println(m+"个数的平均值是"+(sum/m));

        scanner.close();

    }
}

语言结构

  • 顺序结构:从上到下依次进行,是任何一个算法都离不开的一种基本算法结构。

  • 选择结构

  1. if单选择结构

    package com.Hellscythe.Structure;
    
    import java.util.Scanner;
    
    public class Demo01 {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
    
            System.out.println("请输入内容:");
            String A = scanner.nextLine();
    
            if (A.equals("Hello")==false){
                System.out.println(A);
            }
            //默认为==ture
    
            System.out.println("没了");
    
            scanner.close();
        }
    }
    
  2. if双选择结构

package com.Hellscythe.Structure;


import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入内容:");
        int score = scanner.nextInt();

        if (score>=60){
            System.out.println("及格");
        }else{
            System.out.println("不及格");
        }

        scanner.close();
    }
}
  1. if多选择结构
package com.Hellscythe.Structure;

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("您的成绩(请输入数字):");
        int score = scanner.nextInt();

        if (score>=700 && score<750){
            System.out.println("清北");
        }else if (score>=650 && score<700){
            System.out.println("985");
        }else if (score>=600 && score<650){
            System.out.println("211");
        }else if (score>=580 && score<600) {
            System.out.println("老一本");
        }else if (score>=400 && score<580){
            System.out.println("一本及以下");
        }else if (score>=0 && score<400){
            System.out.println("重开");
        }else{
            System.out.println("你有病?");
        }
    }
}

  1. switch选择结构
package com.Hellscythe.Structure;

public class Demo04 {
    public static void main(String[] args) {

        char 成绩 = 'W';

        switch (成绩){
            case'A':
                System.out.println("满分");
                break;
            case'B':
                System.out.println("优秀");
                break;
            case'C':
                System.out.println("合格");
                break;
            case'D':
                System.out.println("不合格");
                break;
            case'E':
                System.out.println("零分");
                break;
            default:
                System.out.println("无关");

        //反编译 正常:java→class;反编译:class→java可执行文件一定是.class 单.class是计算机读取的 .java才是看得懂的 可以通过反编译学习编程
        //case有穿透性,要用break打断;
        //结尾的的fault保证了代码的严谨性\
        //字符的本质是代码。每一个对象有对应的hashcode
        
        
        }
    }
}
  1. while循环
package com.Hellscythe.Structure;

public class Demo05 {
    public static void main(String[] args) {
         int i = 0;


         while (i<100){
             i++;
             System.out.println(i);
         }

    }
}
package com.Hellscythe.Structure;

public class Demo06 {
    public static void main(String[] args) {


        int i = 0;
        int sum = 0;

        while(i<1000000000){
            i++;
            sum = sum + i;//sum+=i
            System.out.println(sum);
        }

    }
}
  1. dowhile结构
package com.Hellscythe.Structure;

public class Demo07 {
    public static void main(String[] args) {

        int A = 0;
        int sum = 0;

        do {

            sum = sum + A;
            A++;

        }while(A<101);
        System.out.println(sum);


    }

}

  • dowhile与while的区别
package com.Hellscythe.Structure;

public class Demo08 {
    public static void main(String[] args) {

        int a = 1;

        while(a>10){
            a++;
            System.out.println(a);
        }
        System.out.println("=================================================");

        do {
            a++;
        }while(a>10);
        System.out.println(a);
    }
}//while结构逻辑是先判断再执行(a++),而dowhile是先执行再判断。结果为while无输出,dowhile输出2

  1. for循环语句
package com.Hellscythe.Structure;

public class Demo09 {
    public static void main(String[] args) {

        int a = 0;//初始化条件

        while(a<=100){//判断条件
            System.out.println(a);//循环体
            a+=2;//迭代
        }
        System.out.println("while循环结束");
        //
        for (int b = 1;b<=100;b++){
            System.out.println(b);

        }
        //for循环是最有效最灵活的循环语句
        System.out.println("for循环结束");
        //100.for 简便代码

    }
   
}
  • 关于for循环的三个练习

①求100以内所有的奇数和以及偶数和

package com.Hellscythe.Structure;

public class Demo10 {
    public static void main(String[] args) {

        int sum1 = 0;
        int sum2 = 0;

        for (int i = 0; i < 100; i++) {
            if (i%2==0){//意思是i➗2余数为0
                sum1+=i;
            } else{
                sum2+=i;

            }

        }//%求余符号
        System.out.println(sum1);
        System.out.println(sum2);

    }
}

②求1000以内所有能被5整除的数,并将他们3个一行排列

package com.Hellscythe.Structure;

public class Demo11 {
    public static void main(String[] args) {

        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");//横向换行
            }
            if (i%15==0){//每三个换行
                System.out.print("\n");//竖向换行
                //System.out.println(),输出一个即换行,print不换行

            }
            
        }
    }
}

③制作一个9x9乘法表⭐

package com.Hellscythe.Structure;

public class Demo12 {
    public static void main(String[] args) {

        for (int i = 1; i <=9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i+"✖"+j+"="+(i*j)+"\t");
            }
            System.out.println();

        }
    }
    /*
    1.创立9.for 输出基础模型 1x1=1 1x2=2 1x3=3....
    2.补全模型,即完成【1,9】内所有数依次相乘
    3.删减,利用j<=i剪掉一半重复数据
    4.排版,利用\t(换列)和\n(换行)排版
     */
    /*
    1.首先进行一次判断i=1,i<=9,往下
    2.第二次判断j,j<=i,往下
    3.按格式输出i✖j=i*j同时由于有"\t",输出后往右空格
    4.输出一个空元素,同时由于println,换行
    5.再次进行一次判断i=2,i<=9,往下
    6.第四次判断j,注意此时j=1,所以满足j<=i的有两项,即执行两次第二个for语句以及第一个输出语句
    7.两次循环完毕后输出空元素,换行
    8.依次类推,第三行进行3次,第四行4次....直到i>9
     */

}

8.关于break和continue

package com.Hellscythe.Structure;

public class Demo13 {
    public static void main(String[] args) {

        int j = 0;
        while (j < 100){
            j++;
            System.out.print(j);
            if (j>30){
                System.out.println("===");
                continue;
                
            }
        }
    }
}
package com.Hellscythe.Structure;

public class Demo14 {
    public static void main(String[] args) {

        int i = 0;
        while (i < 100){
            i++;
            System.out.print(i);
            if (i>30){
                System.out.println("===");
                break;
                
            }
        }
    }
}
  • break会强制结束循环,而continue只会结束当前循环。一旦读取到break,整个循环结束,而读取到continue之后仅仅是后面的语句不再读取。

9.三角形练习和debug

package com.Hellscythe.Structure;

public class Demo15 {
    public static void main(String[] args) {
        
        for (int i = 0; i <= 9; i++) {
            
            for (int j = 9; j >= i; j--) {
                System.out.print(" ");
            }
            for (int k = 0; k <= i; k++){
                System.out.print("*");
            }
            for (int l = 1; l <= i; l++) {
                System.out.print("*");
            }
            
            System.out.println("");

        }
    }
}

  • debug可以逐步检查代码,方便找出问题

使用方法:

image-20210728105225930image-20210728105243392

posted @ 2021-07-26 14:34  TheCasually  阅读(34)  评论(0)    收藏  举报