JAVA学习第三天

JAVASE

Scanner对象

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

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

t通过scanner类的next()与nextLine()方法获取输入的字符串,一般使用hasNext()与hasNextLine()判断是否还有输入的数据

next()方法是以空格为结束符

nextLine()是以enter为结束符

package base01;
​
import java.util.Scanner;
​
public class demo03 {
    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();
    }
}
package base01;
​
import java.util.Scanner;
​
public class demo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入:");
        if(scanner.hasNextLine()){
            String str = scanner.nextLine();
            System.out.println(str);
        }
        scanner.close();
    }
}

顺序结构

一句一句执行 依次执行 顺序结构是基本结构

选择结构

if单选择结构:if(布尔表达式){}

package base02;
​
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();
​
        // equals判断字符串是否相等
        if(s.equals("hello")){
            System.out.println(s);
        }
        System.out.println("end");
        scanner.close();
    }
}

if双选择结构:if(布尔表达式){true执行}else{false执行}

package base02;
​
        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();
    }
}

if多选择结构:if(表达式1){表达式1为true执行}else if(表达式2){表达式2为true执行}else if(表达式3){表达式3为真执行}else{表达式都不为true执行}

package base02;
​
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为true则后面的就不会执行
         */
        if(score == 100){
            System.out.println("good");
        }else if (score<100 && score >= 80){
            System.out.println("不错");
        }else if (score<80 && score >=60){
            System.out.println("一般");
        }else {
            System.out.println("死去吧");
        }
​
        scanner.close();
    }
}

嵌套的if结构:可也嵌套使用if else语句

语法:if(布尔表达式1){表达式1为true执行if(布尔表达式2){表达式2为true执行}}

switch多选择结构:switch case 语句判断一个变量与一系列值中某个值是否相等。

语法:switch(expression){

case value:

语句

break;

....

default:

语句

}

package base02;
​
public class demo04 {
    public static void main(String[] args) {
        char grade = 'a';
        // case穿透  switch匹配一个具体的值
        switch (grade){
            case 'a':
                System.out.println("优秀");
                break;
            case 'b':
                System.out.println("良好");
                break;
            case 'c':
                System.out.println("差");
                break;
            default:
                System.out.println("不知道");
        }
    }
}
​

循环结构

while循环结构:while(布尔表达式){循环内容}

要求:只有表达式为true循环就会一直执行执行下去,我们需要让表达式失效的方式来结束循环

package base02;
​
public class demo05 {
    public static void main(String[] args) {
        // 输出 1--- 100的和
        int i = 0;
        int sum = 0;
         while (i<=100){
             sum = sum + i;
             i++;
         }
        System.out.println(sum);
    }
}
​

do.....while循环:while不满足不能进入循环do while 循环至少执行一次 while先判断后执行dowhile先执行后判断

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

package base02;
​
public class demo06 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        do {
            sum = sum + i;
            i++;
        }while (i<=100);
        System.out.println(sum);
    }
}

for 循环:支持迭代的一种通用结构,循环次数在执行前就确定

语法: for(初始化; 布尔表达式; 更新){代码语句}

package base02;
​
public class demo07 {
    public static void main(String[] args) {
​
        /*
        for(;;){}  死循环
         */
        for(int i=1;i<=100;i++){
            System.out.println(i);
        }
    }
}
​
package base02;
​
public class demo08 {
    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.println();
            }
        }
    }
}
​
package base02;
​
public class demo09 {
    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循环 :主要用于数据或集合

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

声明语句:声明新的局部变量,该变量的类型必须和数组的元素类型匹配。

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

break

// break用于强制退出循环,不执行循环中剩余的语句

package base02;
​
public class demo10 {
    public static void main(String[] args) {
        // break用于强制退出循环,不执行循环中剩余的语句
        int i = 0;
        while (i < 100){
            i++;
            System.out.println(i);
            if (i == 30){
                break;  // 跳出循环不会终止程序
            }
        }
    }
}
​

continue

continue用于终止某次循环过程,跳过循环体中尚未执行的语句,接着进行下一次是否执行循环的判定

package base02;
​
public class demo10 {
    public static void main(String[] args) {
        // break用于强制退出循环,不执行循环中剩余的语句
        int i = 0;
        while (i < 100){
            i++;
            System.out.println(i);
            if (i%10==0){
                //break;  // 跳出循环不会终止程序
                System.out.println();
                continue; // 
            }
            System.out.println(i);
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值