Java流程控制(基础阶段)

Java流程控制(基础阶段)

1.用户交互Scanner

包 java.util.Scanner

​ 通过Scanner类来获取用户的输入

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

通过Scanner类的nest()与nextLine()方法获取输入的字符串,在读取前我们一般需要使用

hasNext()与hasNextLine()判断是否还有输入的数据。

package Scanner;
import java.util.Scanner;

public class Demo01 {
    public static void main(String[] args) {
        //创建一个扫描器对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入数据:");

        //判断用户有没有输入字符串
        if(scanner.hasNext()){

            //使用next方式接收
            String str = scanner.next();//程序会等待用户输入完毕
            System.out.println("输出的内容为:"+str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成好习惯用完就关掉
        scanner.close();
    }
}
//请输入数据:   hello world
//输出的内容为:	hello

next():
  1. 一定要读取到有效字符后才可以结束输入;//前面可以有空格

  2. next()不能得到带有空格的字符串

package Scanner;

import java.util.Scanner;

public class Demo02 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        //用nextLine来接收用户输入的
        System.out.println("请输入数据:");
        //判断是否还有输入
        if(scanner.hasNextLine()) {

            String str = scanner.nextLine();

            System.out.println("你输入的是:" + str);
        }

        scanner.close();
    }
}
//请输入数据:hello world
//你输入的是:hello world
nextLine():
  1. 以Enter为结束符,也就是说,nextLine()方法返回的是输入回车之前的所有字符。
  2. 可以获得空白

拓展练习

package com.jiuwei.yi.database.study;
import org.apache.commons.lang3.StringUtils;
import java.util.Scanner;
public class Test {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);//新建一个对象,用来接收用户输入的数据
        System.out.println("您好,请输入密码:");
        String str;
        while (scanner.hasNextLine()) {
            str = scanner.nextLine();//数据存储在str
            // if (str.equals("hello"))
            //String定义的字符串用equals比较,不能用 ==
            if (StringUtils.equals(str, "hello")) {
                System.out.println("密码正确!");
                break;
            } 
            else {
                System.out.println("密码错误!请重新输入:");
            	 }
    }
        scanner.close();
    }
}

练习

package Scanner;
import java.util.Scanner;
public class Demo08 {
    public static void main(String[] args) {
        //求输入的数据的和,平均数,
        System.out.println("请输入数据,按回车键下一个数据,按#号键结束");
        //创建一个类,接收键盘输入的数据
        Scanner scanner = new Scanner(System.in);
        //定义一个和
        double sum = 0.0;
        //输入数据的个数
        int m = 0;
        while(scanner.hasNextDouble()){     //判断有没有数据输入
            double x = scanner.nextDouble(); //定义一个变量来接收数据
            sum = sum + x;
            m = m + 1;
            System.out.println("您输入的第"+m+"个数据,当前的和为:"+sum);
        }
        System.out.println("您输入的"+m+"个数据的和为:"+sum);
        System.out.println("您输入的"+m+"个数据的平均数为:"+(sum/m));
        scanner.close();
    }
}

3.选择结构

if单选结构
if(布尔表达式){
    //如果布尔表达式的值为true
}
if双选结构
if(布尔表达式){
    //如果布尔表达式的值为true
}else{
    //如果布尔表达式的值为false
}
if多选结构
if(布尔表达式){
    //如果布尔表达式1的值为true
}else if{
    //如果布尔表达式2的值为true
}else if{
    //如果布尔表达式3的值为true
}else{
    //如果布尔表达式的值都为false
}

练习

package struct;
import java.util.Scanner;
public class IfDemo01 {
    public static void main(String[] args) {
        System.out.println("请输入成绩:");
        Scanner scanner = new Scanner(System.in);
        double chengJi = scanner.nextDouble();
        if(chengJi == 100 ){
            System.out.println("恭喜满分");
        }else if(chengJi >= 90 && chengJi < 100){
            System.out.println("优秀");
        }else if(chengJi >= 80 && chengJi < 90){
            System.out.println("良好");
        }else if(chengJi >= 70&& chengJi < 80){
            System.out.println("良");
        }else if(chengJi >= 60 && chengJi < 70){
            System.out.println("继续努力");
        }else if(chengJi >= 45 && chengJi < 60){
            System.out.println("不及格");
        }else if(chengJi >= 0 && chengJi < 45){
            System.out.println("回去种地吧");
        }else{
            System.out.println("输入错误!");
        }
        scanner.close();
    }
}
嵌套的if结构
switch多选择结构
switch(expression){
    case value:
        //语句
        breakcase value:
        //语句
        break;
            defaule:
        //语句
}

练习

package struct;
import java.util.Scanner;
public class SwitchDemo01 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数字:");
        int value = scanner.nextInt();
        switch(value){
            case 1:
                System.out.println("A");
                break;
            case 2:
                System.out.println("B");
                break;
            case 3:
                System.out.println("C");
                break;
            case 4:
                System.out.println("D");
                break;
            default:
                System.out.println("啥也不是");
        }
        scanner.close();
    }
}

4.循环结构

while
while(布尔表达式){
    
}
package struct;
public class WhileDemo01 {
    public static void main(String[] args) {
        int i = 0;
        int sum = 0;
        while(i <= 100){
            sum = sum +i;
            i++;
        }
        System.out.println(sum);
    }
}
do…while
do{
    
}while(布尔表达式);
package struct;
public class DoWhileDemo01 {
    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 struct;
public class ForDemo99 {
    public static void main(String[] args) {
        for (int a = 1; a <= 9; a++) {
            for (int b = 1; b <= a; b++) {
                System.out.print(b+"*"+a+"="+(a*b)+"\t");
            }
            System.out.println();
        }
    }
}
/*
1*1=1	
1*2=2	2*2=4	
1*3=3	2*3=6	3*3=9	
1*4=4	2*4=8	3*4=12	4*4=16	
1*5=5	2*5=10	3*5=15	4*5=20	5*5=25	
1*6=6	2*6=12	3*6=18	4*6=24	5*6=30	6*6=36	
1*7=7	2*7=14	3*7=21	4*7=28	5*7=35	6*7=42	7*7=49	
1*8=8	2*8=16	3*8=24	4*8=32	5*8=40	6*8=48	7*8=56	8*8=64	
1*9=9	2*9=18	3*9=27	4*9=36	5*9=45	6*9=54	7*9=63	8*9=72	9*9=81	
*/
public class ForDemo04 {
    public static void main(String[] args) {
        int[] number = {10,20,30,40,50,60};
        for(int i=0;i<6;i++)
        {
            System.out.println(number[i]);
        }
        System.out.println("--------------------------");
        for(int x:number){
            System.out.println(x);
        }
    }
}

5.break&continue

break

break用于强行退出循环,不执行剩下的语句

continue

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

6.练习

打印三角形
package struct;
public class TextDemo01 {
    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();
        }
    }
} 
     *     
    ***    
   *****   
  *******  
 *********             
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值