学习 java Day08

Java流程控制01:用户交互Scanner


Scanner对象

介绍:之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的额输入。java.util.Scanner是Java5的新特征,我们可以通过Scanner类来获取用户的输入。

基本语法:

//常用的语法
import java.util.Scanner;
Scanner  s = new Scanner(System.in);
int i = scanner.nextInt();

特例:

//输入字符串的格式
import java.util.Scanner;
Scanner scanner=new Scanner();
char grade=scanner.next().charAt(0);

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

next():

  • 1.一定要读取到有效字符后才可以结束输入。
  • 2.对输入有效字符之前遇到的空白,next()方法会自动将其去掉。
  • 3.只有输入有效字符后才将其后面输入的空白作为分隔符或者结束符。
  • next()不能得到带有空格的字符串。

nextLine():

  • 1.以Enter位结束符,也就是说 nextLine()方法返回的是输入回撤之前的所有字符。
  • 2.可以获得空白。
//scanner 面向对象,next方式接收
package com.hashiqi.scanner;
import java.util.Scanner;

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

        //判断用户有没有输入字符串
        if(scanner.hasNext()==true){
            String str = scanner.next();
            System.out.println("输出的内容为:"+str);;
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,要养成良好的习惯用完就关掉
        scanner.close();
    }
}
//scanner 面向对象,使用nextline方式接收
package com.hashiqi.scanner;
import java.util.Scanner;

public class Demo02 {
    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();
    }
}
//scanner next方式接收,无if条件判断语句
package com.hashiqi.scanner;
import java.util.Scanner;

public class Demo03 {
    public static void main(String[] args) {
        //从键盘接收数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入数据:");

            String str = scanner.nextLine();
            System.out.println("输出内容为:\n" + str);

        scanner.close();
    }
}
//scanner 输入多个数求平均值
package com.hashiqi.scanner;
import java.util.Scanner;

public class Demo05 {
    public static void main(String[] args) {
        //输入多个数字,并求其总和与平均数,每输入一个数字用回车确认,通过输入非数字来结束输入兵输出执行结果:

        Scanner scanner = new Scanner(System.in);
        //和
        double sum = 0;
        //计算输入了多少个苏子
        int number = 0;

        System.out.println("请输入数据:");
        //通过循环判断是否还有输入,并在里面对每一次进行求和和统计
        while(scanner.hasNextDouble()){
            double x = scanner.nextDouble();
            number++;
            sum = sum +x;
            System.out.println("你输入了第"+number+"个数据,当前结果sum="+sum);
        }
        System.out.println(number+"个数的和" + sum);
        System.out.println(number + "个数的平均值是" + (sum/number));
        
        scanner.close();
    }
}

顺序结构

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

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

  • 语句与语句之间,框与框之间是按从上到下的顺序进行的,它是由若干个依次执行的处理步骤组成的,它是任何一个算法都离不开的一种基本算法结构。

package com.hashiqi.struct;

public class ShunXuDemo {
    public static void main(String[] args) {
        System.out.println("hello1");
        System.out.println("hello2");
        System.out.println("hello3");
        System.out.println("hello4");
        System.out.println("hello5");
    }
}

选择结构

分类

  • if单选择结构
  • if双选择结构
  • if多选择结构
  • 嵌套的if结构
  • switch多选择结构

if单选择结构

  • 判断一个东西是否可行,然后才去执行,这样一个过程在程序中用if语句来表示

语法:

if(布尔表达式){

//如果不二表达式为true将执行的语句

}

if多选择结构

  • 真实情况下可能面临多个选择,存在区间多级判.比如90-100就是A,80-90就是B…等等,在生活中我们很多时候的选择不仅仅只有两个,这时候需要一个多选择结构来处理这类问题
package com.hashiqi.struct;
import java.util.Scanner;

public class IfDemo03 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        /*
        if 语句至多有1个else 语句,else 语句在所有的else if 语句之后。
        if 语句可以有若干个lese if 语句,它们必须在else 语句之前。
        一旦期中一个else if 语句检测为ture,其他的else if 以及else 语句都将跳过执行。
         */
        System.out.println("请输入成绩:");
        double score = scanner.nextDouble();

        if(score==100) {
            System.out.println("满分");
        }else if (score<100&&score>=90){
            System.out.println("A");
        }else if (score<90&&score>=80){
            System.out.println("B");
        }else if (score<80&&score>=70){
            System.out.println("C");
        }else if (score<70&&score>=60){
            System.out.println("D");
        }else if (score<60){
            System.out.println("不及格");
        }else{
            System.out.println("成绩不合法");
        }
        scanner.close();
    }
}

嵌套的if结构

  • 使用嵌套的if…else语句是合法的。也就是说可以在另一个If或者else if语句中使用if或者else if 语句.可以像if语句一样嵌套 else if …else

语法:

if(布尔表达式 1){
    //如果不二表达式 1的值为ture执行代码
    if(布尔表达式 2){
       //如果不二表达式 2的值为ture 执行代码
    }
}
package com.hashiqi.struct;
import java.util.Scanner;

public class IfDemo04 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入成绩:");
        double score = scanner.nextDouble();
        if(score>=0&&score<=100) {
            if (score >= 60) {
                if (score >= 70) {
                    if (score >= 85) {
                        if (score == 100) {
                            System.out.println("满分");
                        } else {
                            System.out.println("A");
                        }
                    } else {
                        System.out.println("B");
                    }
                } else {
                    System.out.println("C");
                }
            } else {
                System.out.println("不及格");
            }
        }else{
            System.out.println("成绩不合法");
        }
        scanner.close();
    }
}

Switch 多选择结构

  • 多选择结构还有一个实现方式就是switch case 语句。
  • switch case 语句判断一个变量与一系列值中某个值是否相等,每个值称为一个分支。
//语法
switch(expression){
    case value:
        //语句
        break; //可选
    case value:
        //语句
        break; //可选
    //你可以有任意数量的case语句
    default:   //可选
        //语句
}

switch 语句中的变量类型可以是:

  • byte.short.int或者char.
  • 从Java SE 7开始
  • switch 支持字符串 String类型
  • 同时case 标签必须为字符串常量或字面量
package com.hashiqi.struct;
import java.util.Scanner;
public class SwitchDemo01 {
    public static void main(String[] args) {
        //case 穿透 //switch  匹配一个具体的值
        Scanner scanner = new Scanner(System.in);


        char grade=scanner.next().charAt(0);
        switch(grade){
            case'A':
                System.out.println("优秀");
                break;
            case'B':
                System.out.println("良好");
                break;
            case'C':
                System.out.println("及格");
                break;
            case'D':
                System.out.println("再接再厉");
                break;
            case'E':
                System.out.println("挂科");
                break;
            default:
                System.out.println("未知等级");

        }

    }
}

表达式可以是字符串

反编译的字符串通过hashcode转化为对应的数值,例如:

//java文件
package com.hashiqi.struct;
public class SwitchDemo02 {
    public static void main(String[] args) {

        String name = "哈士奇";
        //JDK7的新特性,表达式结果可以是字符串!!!
        //字符的本质还是数字
        //反编译  java---class (字节码文件)----反编译(IDEA)

        switch(name){
            case"哈士奇":
                System.out.println("二哈");
                break;
            case"龙城":
                System.out.println("龙城");
                break;
            case"晨光":
                System.out.println("晨光");
                break;

        }

    }
}

反编译后

//反编译后的class文件
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.hashiqi.struct;

public class SwitchDemo02 {
    public SwitchDemo02() {
    }

    public static void main(String[] args) {
        String name = "哈士奇";
        byte var3 = -1;
        switch(name.hashCode()) {
        case 833505:
            if (name.equals("晨光")) {
                var3 = 2;
            }
            break;
        case 1289045:
            if (name.equals("龙城")) {
                var3 = 1;
            }
            break;
        case 21586052:
            if (name.equals("哈士奇")) {
                var3 = 0;
            }
        }

        switch(var3) {
        case 0:
            System.out.println("二哈");
            break;
        case 1:
            System.out.println("龙城");
            break;
        case 2:
            System.out.println("晨光");
        }

    }
}

循环结构

  • while 循环
  • do…while循环
  • for循环

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

While循环

while是最基本的循环,结构为:

while(布尔表达式){
//循环内容
}

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

大多数情况下回让循环停止,需要一个让表达式失效的方式来结束循环。

少部分情况下需要循环一直执行,比如服务器的请求响应监听等。

循环调剂一直为true就会造成无线循环【死循环】,我们正常的业务编程中应尽量避免死循环。会影响程序性能或者造成程序卡死崩溃!

思考:计算1+2+3+…+100?

//实现1+2+3+...+100
package com.hashiqi.struct;

public class WhileDemo01 {
    public static void main(String[] args) {
        int i = 1;
        int sum = 0;
        while(i<=100){
            sum = sum+i ;
            i++;
        }
        i--;
        System.out.println("sum:"+sum);
        System.out.println("i:"+i);
    }
}
Do…while循环

对于while语句而言,如果不满足条件,则不能进入循环。但有时候我们需要即使不满足条件,也至少执行一次。

do…while循环和while循环相似,不同的是,do…while循环至少会执行一次。

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

While和do-While的区别:

while先判断后执行。do-while是先执行后判断

Do…while总是保证循环体会被至少执行一次,着是他们的主要差别。

package com.hashiqi.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:"+sum);
        System.out.println("i:"+i);
    }
}
For循环

虽然所有循环结构都可以用while或者do…while表示,但Java提供了另一种语句—for循环,使一些循环结构变得更加简单。

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

for循环执行的次数是在执行前就确定的。语法格式如下:

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

练习1:计算0到100之间的奇数和偶数的和

练习2:用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个

练习3:打印九九乘法表

package com.hashiqi.struct;

public class ForDemo01 {
    public static void main(String[] args) {
        //练习1:计算0到100之间的奇数和偶数的和
        int i1 ;
        int i2 ;
        int s1 = 0;
        int s2 = 0;

        for(i1=0;i1<101;i1=i1+2)
        {
            s1 = s1+ i1;
        }
        for(i2=1;i2<101;i2=i2+2)
        {
            s2 = s2+i2;
        }
        System.out.println("s1="+s1);
        System.out.println("s2="+s2);
    }

}
/*
关于for循环有以下几点说明:
最先执行初始化步骤。可以声明一种类型,但可初始化一个或多个循环控制变量,也可以是空语句。
然后,检测布尔表达式的值。如果为ture,循环体被执行。如果为false,循环种植,开始执行循环体后面的语句。
执行一次循环后,更新循环控制变量(迭代因子控制循环变量的增减)。
再次检测布尔表达式。循环执行上面的过程。
 */
package com.hashiqi.struct;

public class ForDemo02 {
    public static void main(String[] args) {
        //练习1:计算0到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("oddsum="+oddsum);
        System.out.println("evensum="+evensum);
    }
}
package com.hashiqi.struct;

public class ForDemo03 {
    public static void main(String[] args) {
        //用while或for循环输出1-1000之间能被5整除的数,并且每行输出3个
        int i ;
        for(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");
        }
    }
}
package com.hashiqi.struct;

public class ForDemo04 {
    public static void main(String[] args) {
        int i=0;

        while(i<=1000){
            if(i%5==0) 
                System.out.print(i + "\t");
                if (i % 15 == 0)
                    System.out.println();
            i++;
        }
    }
}
package com.hashiqi.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        //练习3:打印九九乘法表
        int i=0;
        int j=0;
        int y=0;
        for(i=1;i<10;i++)
            for(j=1;j<=i;j++)
            {  y=i*j;
                System.out.print(j+"*"+i+"="+y+"\t");
                if(i==j)
                    System.out.println();
            }
    }
}
package com.hashiqi.struct;

public class ForDemo05 {
    public static void main(String[] args) {
        //练习3:打印九九乘法表
        int i=0;
        int j=0;
        int y=0;
        for(i=1;i<10;i++) {
            for (j = 1; j <= i; j++) {
                y = i * j;
                System.out.print(j + "*" + i + "=" + y + "\t");
            }
            System.out.println();
        }
    }
}
增强for循环
  • 这里做个了解,之后数组重点使用

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

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

  • for(声明语句:表达式)
    {
        //代码句子
    }
    

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

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

    package com.hashiqi.struct;
    
    public class ForDemo06 {
        public static void main(String[] args) {
            int[] number = {10,20,30,40,50}; //定义一个数组
            for(int i =0;i<5;i++)
                System.out.println(number[i]);
            System.out.println("===================");
            //遍历数组元素
            for(int x:number){
                System.out.println(x);
            }
        }
    }
    
Break continue

Break:用于退出本次循环

Continue:跳过本次循环后面的操作,继续下一个循环

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

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

关于goto关键字

goto关键字很早就在程序设计语言中出现。尽管goto仍是Java的一个保留字,但并未在语言中得到正式使用;Java没有goto.然而,在break和continue这两个关键字的身上,仍然可以看出一些goto的影子—带表亲啊的break和continue。

“标签”是指后面跟一个冒号的标识符。例如:label:

对Java来说唯一用到标签的地方是在循环语句之前。而在循环之前设置标签的唯一理由是:我们希望在其中嵌套另一个循环,由于break和从图虐关键字通常只终端当前循环,在若随同标签使用,它们就会终端到存在标签的地方。

break的用法

package com.hashiqi.struct;

public class BreakDemo {
    public static void main(String[] args) {
        //break的用法
        int i=1;
        while(i<20){
            i++;
            System.out.println(i);
            if(i==10) {
                break;
            }
        }
    }
}

continue的用法

package com.hashiqi.struct;

public class ContinueDemo {
    public static void main(String[] args) {
        //continue的用法
        int i=0;
        while(i<100){
            i++;
            if(i%10==0){
                System.out.println("=====");
                continue;
            }
            System.out.println(i);
        }
    }
}
package com.hashiqi.struct;

public class ContinueDemo02 {
    public static void main(String[] args) {
        //continue的用法
        int i;
        int sum=0;
        for(i=0;i<100;i++){
            if(i%10==0) {
                System.out.println("=======");
                continue;
            }
            System.out.println(i);
        }

    }
}
java中类似goto跳转的用法
package com.hashiqi.struct;

public class LabelDemo {
    public static void main(String[] args) {
        //打印101-150之间所有的质数
        //质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数
        int i;
        int j;
        outer:for(i=101;i<150;i++){
            for(j=2;j<i/2;j++) {
                if (i % j == 0) {
                      continue outer;
                }
            }
            System.out.print(i+"\t");
        }
    }
}

打印三角形

package com.hashiqi.struct;

public class TestDemo01 {
    public static void main(String[] args) {
        //打印三角形  5*5
        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();
        }
    }
}
/*
            for (int j = 1; j <=i ; j++) {
                    System.out.print("*");
                    }
                    for (int j = 1; j <i ; j++) {
        System.out.print("*");
        }
        System.out.println();*/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值