Java学习第五天——流程控制

Java流程控制

一、用户交互Scanner

  1. 我们可以通过Scanner类来获取用户的输入

  2. 基本语法:Scanner s = new Scanner(System.in);

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

  • next()

    1. 一定要读取到有效字符后才可以结束输入

    2. 对输入有效字符之前遇到的空白,next()方法会自动将其去掉

    3. 只有输入有效字符后才将后面输入的空白作为分隔符或者结束符

    4. next()方法不能得到有空格的字符串

  • nextLine()

    1. 以Enter为结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符

    2. 可以获得空白

public static void main(String[] args) {
   //创建一个扫描器对象,用于接收键盘数据
   Scanner scanner = new Scanner(System.in);
   System.out.println("使用next方法接收");
   //判断用户有没有输入字符串
   if (scanner.hasNext()){
       String str = scanner.next();  //程序会一直等待用户输入完毕
       System.out.println("输入的内容为:"+str);
  }
   //凡是属于IO流的类如果不关闭会一直占用资源,要养成良好习惯,用完就关掉
   scanner.close();
}
使用next方法接收
Hello World
输入的内容为:Hello
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
public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   System.out.println("请输入数据:");
   String str = scanner.nextLine();
   System.out.println(str);
   scanner.close();
}
请输入数据:
Hello World
Hello World
public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   int i = 0;
   float j = 0.0f;
   System.out.println("请输入整数:");
   if (scanner.hasNextInt()){
       i = scanner.nextInt();
       System.out.println(i);
  }else{
       System.out.println("你输入的不是整数");
  }
   scanner.close();
}
请输入整数:
hello
你输入的不是整数
public static void main(String[] args) {
   Scanner scanner = new Scanner(System.in);
   int count = 0;
   double num = 0;
   System.out.println("请输入数据:");
   while (scanner.hasNextDouble()){
       double i =  scanner.nextDouble();
       num = num+i;
       count++;
       System.out.println("你当前输入的是第"+count+"个数"+i);
  }
   scanner.close();
   System.out.println(count+"个数的总和为"+num);
   System.out.println(count+"个数的平均数为"+(num/count));
}
请输入数据:
453
你当前输入的是第1个数453.0
2
你当前输入的是第2个数2.0
23
你当前输入的是第3个数23.0
43
你当前输入的是第4个数43.0
hello
4个数的总和为521.0
4个数的平均数为130.25

二、顺序结构

  • Java的基本结构就是顺序结构,除非特别指明,否则就按照一字一句执行

  • 顺序结构是最简单的算法结构

  • 它是任何一个算法都离不开的一种算法结构

三、选择结构

  1. if单选择结构

    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       String str = scanner.nextLine();
       //判断字符串是否相等 equals
        if (str.equals("hello")){
            System.out.println(str);
        }
        scanner.close();
    }
  2. if双选择结构

    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();
    }
  3. if多选择结构

    public static void main(String[] args) {
       Scanner scanner = new Scanner(System.in);
       System.out.println("请输入成绩:");
       int score = scanner.nextInt();
       if(score<=100 && score>=80){
           System.out.println("优秀");
      }else if(score<80 && score>=60){
           System.out.println("及格");
      }else if(score<60 && score>=0){
           System.out.println("不及格");
      }else{
           System.out.println("成绩不合法!");
      }
    }
  4. 嵌套的if结构

    使用嵌套的if···else语句是合法的。也就是说可以在另一个if或者else if语句中使用if 或者 else if 语句,你可以像if语句一样嵌套else if ···else

  5. switch多选择结构

    public static void main(String[] args) {
    char i = 'c';
    switch (i){
    case 'A':
    System.out.println("优秀");
    break;
    case 'B':
    System.out.println("良好");
    break;
    case 'C':
    System.out.println("及格");
    break;
    case 'D':
    System.out.println("不及格");
    break;
    default:
    System.out.println("未知错误!");
    }
    }

    Switch语句中的变量类型可以是:byte,short,int或者Char,从JDK7开始Switch支持String类型了。

    public static void main(String[] args) {
    //JDK7新特性,表达式结果可以是字符串

    //反编译 java----class(字节码文件)---反编译(IDEA )
    String str = "张信哲";
    switch (str){
    case "哈哈哈":
    System.out.println("哈哈哈");
    break;
    case "张信哲":
    System.out.println("张信哲");
    break;
    }
    }
    //反编译文件
    package struct;

    public class Demo06 {
    public Demo06() {
    }
    public static void main(String[] args) {
    String str = "张信哲";
    byte var3 = -1;
    switch(str.hashCode()) {
    case 21552072:
    if (str.equals("哈哈哈")) {
    var3 = 0;
    }
    break;
    case 24057937:
    if (str.equals("张信哲")) {
    var3 = 1;
    }
    }
    switch(var3) {
    case 0:
    System.out.println("哈哈哈");
    break;
    case 1:
    System.out.println("张信哲");
    }
    }
    }

四、循环结构

  1. while循环----最基本的循环

    在java5中引入了一种主要用于数组的增强型for循环

    我们大多数情况会让循环停止下来,我们需要一个让表达式失效的方式来结束循环,少部分情况需要一直执行,比如服务器的请求响应监听器

    public static void main(String[] args) {
    int a = 0;
    int sum = 0;
    while (a<100){
    a++;
    sum = a+sum;
    System.out.println(a);
    }
    System.out.println(sum);
    }
  2. do···while循环

    while和do···while的区别: while先判断再执行,do···while是先执行后判断

    do···while总是保证循环体会被至少执行一次!这是他们的主要区别

    public static void main(String[] args) {
    int a = 0;
    int sum = 0;
    do {
    sum = sum + a;
    a++;
    }while (a <= 100);
    System.out.println(sum);
    }
    public static void main(String[] args) {
    int a = 0;
    while (a<0){
    System.out.println(a);
    a++;
    }
    System.out.println("=========================");
    do {
    System.out.println(a);
    a++;
    }while (a<0);
    }
    public static void main(String[] args) {
    int a = 0;
    while (a<100){
    a +=2;
    System.out.println(a);
    }
    System.out.println("while循环结束");

    for (int i = 0;i<=100;i++){
    System.out.println(i);
    }
    }
  3. for循环

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

    //练习1:计算0到100之间的奇数和偶数的和?
    public static void main(String[] args) {
    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);
    }

    关于for循环有以下几点说明:

    • 最先执行的是初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句;

    • 然后检测布尔表达式的值。如果为true,循环体被执行。如果为false循环终止,开始执行循环体后面的语句。

    • 执行一次循环后更新循环控制变量(迭代因子控制循环变量的增减)

    • 再次检测布尔表达式,循环执行上面的过程

      三个参数都可以省略,那就是死循环

      //练习2:用while或者for循环输出1到1000之间能被5整除的数,并且每行输出三个
      public static void main(String[] args) {
         for (int i = 0; i < 1000; i++) {
             if(i%5==0){
                 System.out.print(i+"\t");
            }
             if (i%(5*3)==0){
                 System.out.print("\n");
            }
        }
         //println()输出后会换行
         //print()输出完不会换行,会在一行里无限输出
      }

五、增强for循环

  JDK5引入了一种主要用于数组或集合的增强型for循环

  Java增强型for循环语法格式如下:

  • for(声明语句:表达式){

    //代码句子

    }

  • 声明语句:声明新的局部变量,该变量的类型必须和数组元素的类型匹配。其作用域限定在循环语句块,其值与此时数组元素的值相等。

  • 表达式:表达式是要访问的数组名,或者是返回值为数组的方法

    public static void main(String[] args) {
       int[] number = {10,20,30,40,50};//定义了一个数组
       //遍历数组的元素
       for(int x:number){
           System.out.println(x);
      }
    }

六、break、continue以及goto关键字

  • break在任何循环语句的主体部分,均可用break控制循环的流程。break用于强行退出循环,不执行循环中剩余的语句。(break也可在Switch语句中使用)

    public static void main(String[] args) {
       int i = 0;
       while(i<100){
           i++;
           System.out.println(i);
           if (i == 30){
               break;
          }
      }
       System.out.println("跳出循环");
    }
  • continue语句用在循环语句体中,用于终止某次循环过程,即跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定

    public static void main(String[] args) {
       int i = 0;
       while (i<100){
           i++;
           if(i%10 == 0){
               continue;
          }
           System.out.println(i);
      }
    }
  • 带标号的continue

    public static void main(String[] args) {
       //打印101到150之间的所有质数
    //不建议使用
      other:for (int i = 101;i<150;i++){
           for (int j = 2;j<i/2;j++){
               if(i%j == 0){
                   continue other;
              }
          }
          System.out.println(i);
      }
    }
  • 打印三角形
public static void main(String[] args) {
   //打印三角形,首先是看成打印长方形,
   // 先打印左上角的空白倒直角三角形,
   // 再打印正直角三角形,最后打印剩下的三角形
   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();
  }
}

 

posted @ 2021-06-01 20:24  张信哲  阅读(359)  评论(0)    收藏  举报