java基础02

流程控制

* 通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用hasNext()与hashNextLine()判断
* 是否还有输入数据
public class Demo01 {
    public static void main(String[] args) {
        //new出scanner 对象,用来接受键盘数据
        Scanner scanner = new Scanner(System.in);

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

        //判断用户有没有输入字符串
        if (scanner.hasNext()==true){
            //使用next方式接收
            String str = scanner.next(); //程序会等待用户输出完毕
            System.out.println("输出的内容为:"+str);

        }
        //如果使用io流(输入输出流)的话关闭scanner
        scanner.close();
    }
}

使用next方式接收:
hello world
输出的内容为:hello

进程已结束,退出代码0
import java.util.Scanner;  //导入 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()){
            String str = scanner.nextLine();  //以回车为结束符号,客户输出空格

            System.out.println("输出内容为:"+str);
        }
    scanner.close();
    }
}

使用nextline方式接收:
hello world
输出内容为:hello world

进程已结束,退出代码0

image-20221125上午10123874

选择结构

if 单选择结构 Demo01

if(布尔表达式){

//如果布尔表达式为true将执行语句

}
import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入内容");
        String s = scanner.nextLine();   //接收用户输入
        if(s.equals("hello world")){     //判断字符串是否等于hello woeld
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();

    }
}

请输入内容
hello world
hello world
end

进程已结束,退出代码0

if双选择结构 Demo02

if(布尔表达式){

//如果布尔表达式的值为true

}else{

//如果布尔表达式的值为fales

}
import java.util.Scanner;

public class Demo02 {
    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("不及格");
        }
        scanner.close();
    }
}


请输入成绩:
60
成绩合格

进程已结束,退出代码0

if多选择结构

import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你的成绩:");
        int s = scanner.nextInt();
        if (s==100){
            System.out.println("成绩满分");
        } else if (s < 100 && s >= 90) {
            System.out.println("成绩A");
        } else if (s<90 && s>=80 ) {
            System.out.println("成绩B");
        } else if (s < 80 && s>=70) {
            System.out.println("成绩C");
        } else if (s <70 && s>= 60) {
            System.out.println("成绩D");

        }else {
            System.out.println("成绩不合格");
        }


        scanner.close();
    }
}

请输入你的成绩:
60
成绩D

进程已结束,退出代码0

switch 多选择结构 匹配一个具体的值

1)注意 如果一个case执行过后,没有break的话可能会case穿透 执行下面所有的case

2)判断变量中的某一个值与一系列是否相等

switch (expression)
  case value ;

public class SwitchDemo01 {
    public static void main(String[] args) {           //case穿透例子 建议每个case后边加上break
        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("未知登记");
        }



    }
}



及格
再接再厉
挂科
未知登记

进程已结束,退出代码0

while循环

while(布尔表达式){

//循环内容

注意:只要布尔表达式为true ,循环就会一直执行下去

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


5050:101
5050

do while循环

和while的区别。 while先判断后执行,dowhile是先执行后判断

do while 总是会保证循环体至少执行一次

do{
 				//代码语句
}while(布尔表达式);

while 和do while对比

public class DoWhileDemo01 {
    public static void main(String[] args) {
        int a = 0 ;
        while (a<0){                                 //上面部分的while 并未执行
            a++;
            System.out.println(a);
        }
        System.out.println("--------------------------------");
        do {
            a++;                                     //下面的a++ 执行了一次
            System.out.println(a);
        }while(a<0);
    }
}

--------------------------------
1

for循环. 最有效的一种循环方法

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++){                // int i=1 初始化值
            System.out.println(i);                 // i<=100 条件
                                                     // i++ 迭代

        }
        System.out.println("for循环结束!");
        //for (int i = 0; i < 100; i++) {             //快捷键 100.for 也可以生成for循环

        }
    }

}

计算0-100之间基数偶数的和

for循环实现

public class ForDemo02 {
    public static void main(String[] args) {
        //计算0-100之间偶数的和     //for 循环
        int oddsum = 0 ;
        //定义偶数和的初试值
        int evensum =0 ;
        //定义奇数和的初试值
        for (int i = 0; i < 100; i++) {
            //for循环 循环1到100
            if (i%2!=0){
                //如果可以被2乘除证明为偶数
                oddsum = oddsum + i;
                //求和

            }else {
                //否则为奇数并求奇数的和
                evensum =evensum +i ;
            }

        }
        System.out.println("奇数的和:"+ evensum);
        System.out.println("偶数的和:"+ oddsum);
    }
}

while循环实现

    public static void main(String[] args) {
        int oddsum = 0;      
        int enensum = 0;
        int i = 0;
        //定义初试值
        while (i<100){
            //判断当前i是否小于100
            i++;
            //循环增加
            if (i%2!=0){
                oddsum+=i;
            }else {
                enensum+=i;
            }
            //条件判断求和
        }
        System.out.println("奇数的和为:"+oddsum);
        System.out.println("偶数的和为:"+enensum);
    }
}

使用for循环1000只能能被5整除的数字 并且每行输出3个

for实现

public class ForDemo03 {
    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){
                //换行 这里可以输出空 或者 print("\n")
                System.out.print("\n");
            }
        }
    }
}

while实现

       int i = 0;
        while(i<+1000){
            i++;
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%15==0){
                System.out.print("\n");
            }
        }
    }
}

打印九九乘法表

public class ForDemo04 {
    public static void main(String[] args) {
        for (int j = 1; j <= 9; j++){
            for (int i = 1; i <= j; i++) {
                System.out.print(j+"*"+i+"="+(j*i)+"\t");
        }
            System.out.println();
        }
    }
}

增强for循环

public class ForDemo05 {
    public static void main(String[] args) {
        int[] number = {10,20,30,40,50,60};  //定义一个数组
        for (int i =0 ; i<5 ; i++){
            System.out.println(number[i]);
        }
        System.out.println("----------------------------");
        for (int x:number){     //将nubber[]中的每一个值都赋值给x
            System.out.println(x);  //打印x
        }


    }
}

break

在任何循环语句的主体部分,均可用break控制循环的流程。

break用户强行退出循环,不执行循环中剩余的语句。(break语句也可以在swithc语句中使用)

public class BreakDemo {
    public static void main(String[] args) {
        int i =0 ;
        int a = 0;
        while (i<100){
            i++;


            if (i==30){
              //当i等于30的时候跳出循环并不再执行剩余的while循环  
              break;
            }

        }
        System.out.println("i总共循环了"+i+"次");
    }

}

i总共循环了30次

进程已结束,退出代码0

continue

Continue 语句在循环语句体总,用于终止某次循环过程,即跳过循环体中尚未执行的语句,借着进行下一次是否执行循环的判定。

public class ContinueDemo01 {
    public static void main(String[] args) {
        int i = 0 ;
        while (i<100){
            i++;
            if (i%10 == 0){
                //如果i能被10整除
                System.out.println();
                //则换行,跳出本次循环
                continue;
            }
            System.out.print(i+"\t");
            //打印i
        }
    }
}

1	2	3	4	5	6	7	8	9	
11	12	13	14	15	16	17	18	19	
21	22	23	24	25	26	27	28	29	
31	32	33	34	35	36	37	38	39	
41	42	43	44	45	46	47	48	49	
51	52	53	54	55	56	57	58	59	
61	62	63	64	65	66	67	68	69	
71	72	73	74	75	76	77	78	79	
81	82	83	84	85	86	87	88	89	
91	92	93	94	95	96	97	98	99	

进程已结束,退出代码0

打印三角形

posted @ 2022-12-04 23:18  你能成大事  阅读(48)  评论(0)    收藏  举报