Fork me on GitHub

流程控制

流程控制

1.用户交互Scanner

image-20200617225021827

 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);
        }
         //凡是属于IO流的类如果不关闭就会一直占用资源。要养成良好的习惯用完就关掉
         scanner.close();
    }
 }
 使用next方式接收:
 hello world
 输出的内容为:hello

 

 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.hasNextLine()){
             String str = scanner.nextLine();
             System.out.println("输出的内容为:"+str);
        }
         scanner.close();
    }
 }
 使用nextLine方式接收
 hello world
 输出的内容为:hello world

区别 image-20200617230059891

 

2.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();
    }
 }

image-20200617231144116

 

 public class Demo05 {
     public static void main(String[] args) {
         //输入多个数字,并求他们的总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入并且输出执行结果
         Scanner scanner = new Scanner(System.in);
 
         //和
         double sum = 0;
         //计算输入了多少个数字
         int m = 0;
 
         //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
         while(scanner.hasNextDouble()){
             double x = scanner.nextDouble();
             m = m + 1;
             sum = sum + x;
 
        }
         System.out.println(m+"个数的和为"+sum);
         System.out.println(m+"个数的平均值为"+(sum/m));
         scanner.close();
    }
 }

image-20200617231728861

 

3.顺序结构

java基本结构就是顺序结构,即代码按照顺序一句一句执行,除非有特殊情况。

4.选择结构

if

 public class IfDemo01 {
     public static void main(String[] args) {
         Scanner scanner = new Scanner(System.in);
 
         System.out.println("请输入内容:");
         String s = scanner.nextLine();
 
         //equals:判断字符串是否相等
         if(s.equals("Hello")){
             System.out.println(s);
        }
         scanner.close();
    }
 }

image-20200618152945014

if else

 import java.util.Scanner;
 
 public class IfDemo02 {
     public static void main(String[] args) {
         //考试成绩大于60分就是及格,小于60分就是不及格。
         Scanner scanner = new Scanner(System.in);
         System.out.println("请输入成绩");
 
         int score = scanner.nextInt();
         if(score>=60){
             System.out.println("及格");
        }else{
             System.out.println("不及格");
        }
    }
 }

image-20200618153425046

 

多选择结构 if,else if ,else

 import java.util.Scanner;
 
 public class IfDemo03 {
     public static void main(String[] args) {
         //考试成绩大于60分就是及格,小于60分就是不及格。
         Scanner scanner = new Scanner(System.in);
         System.out.println("请输入成绩");
 
         int score = scanner.nextInt();
         if(score == 100){
             System.out.println("恭喜考了满分!");
        }else if(score<100 && score >= 90){
             System.out.println("A级");
        }else if(score<90 && score >= 80){
             System.out.println("B级");
        }else if(score<80 && score >= 70){
             System.out.println("C级");
        }else if(score<70 && score >= 60){
             System.out.println("D级");
        }else if(score<60 && score >= 0){
             System.out.println("E级");
        }else{
             System.out.println("输入不合法");
        }
    }
 }

image-20200618154130148

switch多选择结构 image-20200618162800877

 

 public class SwitchDemo01 {
     public static void main(String[] args) {
         //case 穿透
         char grade = 'C';
 
         switch (grade){
             case 'A':
                 System.out.println("优秀");
                 break;
             case 'B':
                 System.out.println("良好");
             case 'C':
                 System.out.println("及格");
             case 'D':
                 System.out.println("再接再厉");
             case 'E':
                 System.out.println("挂科");
             default:
                 System.out.println("输入不合法");
        }
    }
 }

image-20200618163258039

 

不写break得话就会一直执行下去,所以一般每个case后面都会使用break

 public class SwitchDemo02 {
     public static void main(String[] args) {
         String name = "同同";
         //JDK7的新特性,表达式结果可以是字符串
         //字符的本质还是数字
 
         switch (name){
             case "TongTong":
                 System.out.println("TongTong");
                 break;
             case "同同":
                 System.out.println("同同");
                 break;
             default:
                 System.out.println("啥也不是");
        }
    }
 }

image-20200618163621928

 

利用idea查看源码

image-20200618183648129

image-20200618183704822

 

然后资源管理器中打开箭头所标的位置,找到自己需要查看的java文件的.class文件,并且复制该.class文件

image-20200618183906868

然后打开java文件的地址

image-20200618184018501

 

将该.class文件复制到放java文件的文件夹下。随后在idea中打开即可。

看一下两个文件的源文件和源码

 public class SwitchDemo02 {
     public static void main(String[] args) {
         String name = "同同";
         //JDK7的新特性,表达式结果可以是字符串
         //字符的本质还是数字
 
         //反编译 java--->class(字节码文件) --->反编译
         switch (name){
             case "TongTong":
                 System.out.println("TongTong");
                 break;
             case "同同":
                 System.out.println("同同");
                 break;
             default:
                 System.out.println("啥也不是");
        }
    }
 }

 

 package struct;
 
 public class SwitchDemo02 {
     public SwitchDemo02() {
    }
 
     public static void main(String[] args) {
         String name = "同同";
         byte var3 = -1;
         switch(name.hashCode()) {
         case -952184536:
             if (name.equals("TongTong")) {
                 var3 = 0;
            }
             break;
         case 688512:
             if (name.equals("同同")) {
                 var3 = 1;
            }
        }
 
         switch(var3) {
         case 0:
             System.out.println("TongTong");
             break;
         case 1:
             System.out.println("同同");
             break;
         default:
             System.out.println("啥也不是");
        }
 
    }
 }

5.循环结构

while循环

 

 public class WhieDemo01 {
     public static void main(String[] args) {
         //输出1-100
         int i = 0;
         while(i<100){
             i++;
             System.out.println(i);
        }
    }
 }

程序在运行中,应该避免死循环

 

 public class WhileDemo02 {
     public static void main(String[] args) {
         //计算1+2+3+……+100
         int i = 1;
         int sum = 0;
         while(i<=100){
             sum += i;
             i++;
        }
         System.out.println(sum);
    }
 }

do while循环

 public class WhileDemo03 {
     public static void main(String[] args) {
         //计算1+2+3+……+100
         int i = 1;
         int sum = 0;
         do{
             sum += i;
             i++;
        }while(i<=100);
         System.out.println(sum);
    }
 }

whie和do while的区别,while先判断再执行,do while一定会先执行一次再判断。

for循环

 public class ForDemo01 {
     public static void main(String[] args) {
         int a = 1;//初始化条件
         while(a<=100){
             System.out.println(a);//循环体
             a += 2;
        }
 
         System.out.println("While 循环结束");
 
         for(int i=1;i<=100;i++){
             System.out.println(i);
        }
         System.out.println("for循环结束");
    }
 }

for循环是一种支持迭代的通用结构,是最有效、最灵活的循环结构。

输入 100.for 回车,有意想不到的效果

 public class ForDemo02 {
     public static void main(String[] args) {
         //练习1:计算0-100之间的奇数和偶数的和
         int oddsum = 0;
         int evensum = 0;
 
         for(int i =0;i<100;i++){
             if(i%2!=0){
                 oddsum += i;
            }else{
                 evensum += i;
            }
        }
         System.out.println("奇数的和:"+oddsum);
         System.out.println("偶数的和:"+evensum);
    }
 }
 public class ForDemo03 {
     public static void main(String[] args) {
         //练习2:用for循环输出1-1000之间能被5整除的数,并且每行输出3个
         for(int i = 0;i<=1000;i++){
             if(i%5==0){
                 System.out.print(i+"\t");
            }
             if(i%(5*3)==0){
                 System.out.println();
            }
        }
    }
 }

print和println的区别是后者输出后会换行

 public class ForDemo04 {
     public static void main(String[] args) {
         //打印99乘法表
         for(int i = 1;i <= 9;i++){
             for(int j=1;j<=i;j++){
                 System.out.print(j+"*"+i+"="+i*j+" ");
            }
             System.out.println();
        }
    }
 }

image-20200618191716883

 

增强for循环 jdk5才引入,主要用在遍历数组、集合

 public class ForDemo05 {
     public static void main(String[] args) {
         //定义一个数组
         int[] numbers = {10,20,30,40,50};
         for(int x : numbers){
             System.out.println(x);
        }
    }
 }
 public class ForTest{
     @Test
     public void test1(){
         Collection collection = new ArrayList();
         collection.add(123);
         collection.add(456);
         collection.add(new Person("Ryan", 21));
         collection.add(new String("Tom"));
         collection.add(false);
 
         // for(集合元素的类型 局部变量 : 集合对象)
         // 内部仍然调用了迭代器
         for (Object o : collection) {
             System.out.println(o);
        }
    }
 }

 

image-20200810230348872

     @Test
     public void test2(){
         String[] arr = new String[]{"MM","MM","MM"};
         // 增强for循环
         for (int i = 0; i < arr.length; i++) {
             arr[i] = "GG";
        }
         for (int i = 0; i < arr.length; i++) {
             System.out.println(arr[i]);
        }
         System.out.println("==================================");
         String[] arr2 = new String[]{"MM","MM","MM"};
         for (String s : arr2) {
             s = "GG";
        }
         for (int i = 0; i < arr2.length; i++) {
             System.out.println(arr2[i]);
        }
    }
   

image-20200810230852688

注意区别,增强型for循环是创建了一个新的数组s来存放arr2数组的值,所以改变的是s的值,并没有改变原数组的值。

 

6.break,continue,goto

image-20200618192658068

 

 public class GotoDemo01 {
     public static void main(String[] args) {
         //打印101-150之间的所有质数
         int count = 0;
         outer:for(int i=101;i<150;i++){
             for(int j=2;j<i/2;j++){
                 if(i%j==0){
                     continue outer;
                }
            }
             System.out.println(i+" ");
        }
    }
 }

打印三角形

 public class TestDemo {
     public static void main(String[] args) {
         //打印三角形5行
         for(int i = 1;i <=5;i++){
             for(int j = 5;j>=i;j--){
                 System.out.print(" ");
            }
             for(int j = 1;j<=i;j++){
                 System.out.print("*");
            }
             for(int j = 1; j < i;j++){
                 System.out.print("*");
            }
             System.out.println();
        }
    }
 }

仔细体会三个for循环分别打出什么来;体会不到就先把另外两个的*换成“ ”

学会使用Debug

右键打断点,然后点击运行旁边的小虫(Debug)

image-20200618194747875

然后点击上述位置即可一步一步执行。

 

 

posted @ 2020-09-12 10:47  一直在努力的小白  阅读(234)  评论(0)    收藏  举报