学习Java Day10

2022/2/5 22:27:26

本文主要是介绍学习Java Day10,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

Day 10 算法结构

顺序结构

从上到下依次执行

选择结构

if单选择结构

用于判断是否可行

if(布尔表达式){
    //如果布尔表达式为true将执行的语句
}

e.g.

package structure;

import java.util.Scanner;

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

        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入内容:");
        String s = scanner.nextLine();
        //输完scanner.nextLine();Alt+Enter

        //equals:判断字符串是否相等  此处不能用==
        if (s.equals("Hello")){
            System.out.println(s);
        }

        System.out.println("End");

        scanner.close();
    }
}

if双选择结构

if(boolean){
    //if boolean = true
}else{
    //if booolean = false
}
package structure;

import java.util.Scanner;

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

        //score>60 pass;<60 fail
        Scanner scanner = new Scanner(System.in);

        System.out.println("Please enter the score:");
        int score = scanner.nextInt();

        if (score>60){
            System.out.println("PASS");
        }else{
            System.out.println("FAIL");
        }


        scanner.close();
    }

}

if多选择结构

if(boolean 1){
    //if boolean 1 = true
}else if(boolean 2){
    //if boolean 2 = true
}else if(boolean 3){
    //if boolean 3 = true
}else{
    //if all of these ?= true
}
package structure;

import java.util.Scanner;

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

        Scanner scanner = new Scanner(System.in);

        System.out.println("PLease enter the score:");

        int socre = scanner.nextInt();

        if (socre==100){
            System.out.println("Congratulations!");
        }else if (socre<100 && socre>=90){
            System.out.println("A");
        }else if (socre<90 && socre>=75){
            System.out.println("B");
        }else if (socre<75 && socre>=60){
            System.out.println("C");
        }else if (socre<60 && socre>=0){
            System.out.println("D");
        }else{
            //别漏了这个!
            System.out.println("Null!");
        }


        scanner.close();
    }
}

  • if语句至多有1个else语句,else语句在所有的else if语句之后
  • if语句可以用若干个else if语句,他们必须在else语句之前
  • 一旦一个else if语句检测为true,其他的else if以及else语句都将跳过执行

嵌套的if结构

if(boolean 1){
    //if bollean 1 = true
    if(boolean 2){
        //if boolean 2 = true
    }
}

switch 多选择结构

switch(expression){
    case value:
        //statements
        break;//可选
    case value:
        //statements
        break;//可选
        //你可以有任意数量的case语句
    default://可选
        //statements
}
  • 变量类型可以是byte,short,int,char
  • 支持String
  • case标签必须为字符串常量或字面量
package structure;

public class switchDemo04 {
    public static void main(String[] args) {
        //case穿透  //switch匹配一个具体的值
        char grade = 'B';

        switch (grade){
            case'A':
                System.out.println("EXCELLENT!");
                break;//不加break就会全部输出
            case 'B':
                System.out.println("GOOD!");
                break;
            case 'C':
                System.out.println("PASS");
                break;
            case 'D':
                System.out.println("FAIL");
                break;
            default:
                System.out.println("UNKNOWN");
        }
    }
}

反编译——字符的本质是数字 看源码

循环结构

while循环

while(布尔表达式){
    //循环内容
}
  • 如果不满足条件,则不能进入循环
  • 只要布尔表达式正确,循环就会一直执行下去
  • 大部分需要一个让表达式失效的方法来结束循环
  • 少部分需要循环一直执行,比如服务器的请求响应监听等
  • 循环条件一直为true就会无限循环
package structure;

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

        int i = 0;

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

package structure;

public class whileDemo06 {
    public static void main(String[] args) {
        //calculate 1+2+3+...+100=?
        int i = 0;
        int sum = 0;

        while (i<=100){
            sum = sum + i;
            i++;
        }

        System.out.println(sum);
    }
}

do…while循环

  • 若不满足条件,也至少会执行一次
do{
    //code statements
}while(布尔表达式);

while和do…while的区别

while先判断后执行,dowhile先执行后判断

For循环!

for(初始化;布尔表达式;更新){
    //statements
}
package structure;

public class ForDemo07 {
    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);
        }
        //快捷键:100.for
        System.out.println("For循环结束");
     }
}

for(; ; ){
    //死循环
}
package structure;

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

        //calculate the sum of odd and even between 0 and 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("The sum of odd:"+oddSum);
        System.out.println("The sum of even:"+evenSum);
        
    }
}

package structure;

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

        /*print the number that can be divided by 5 between 1 and 1000
          print 3 of them every line
         */

        for (int i = 0; i <= 1000; i++) {

            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
 //System.out.print("\n")
            }

        }
    }
}

  • println 输出会换行
  • print 输出不会换行


这篇关于学习Java Day10的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程