首页 > 编程语言 > 详细

Java流程控制

时间:2021-07-11 14:24:35      阅读:13      评论:0      收藏:0      [点我收藏+]

Java流程控制

用户交互Scanner

  • 之前我们学习的基本语法中我们并没有实现程序和人的交互,但Java给我们提供了一个工具类,我们可以获取用户和输入。Java。util.Scanner是Java5的特性,我们可以通过Scanner类来获取用户输入。
  • 基本语法:
Scanner s = new Scanner(System.in);
  • 通过Scanner类的next()与nextLine()方法获取输入字符串,在读取钱我们一般需要使用hasNext()与hasNextLine()判断是否有输入数据。

使用Scanner.next方法

 public static void main(String[] args) {
        //创建一个扫描对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("使用next方法接收:");
        //判断用户有没有输入字符串
        if (scanner.hasNext()){
            String  str =scanner.next();
            System.out.println("输出的内容为:"+str);
        }
        //凡是属于IO流的类如果不关闭会一直占用资源,所以必须要关掉
        scanner.close();
    }
----------------------------------------------------------------------------------------
使用next方法接收:
hello word
输出的内容为:hello
Process finished with exit code 0

使用Scanner.nextLine方法

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);
    }
    //凡是属于IO流的类如果不关闭会一直占用资源,所以必须要关掉
    scanner.close();
}
-------------------------------------------------------------------------------------
使用nextLine方法接收:
hello Word
输出的内容为:hello Word
Process finished with exit code 0

二者比较

  • next

1.一定要读取有效字符后才可以结束输入

2.对输入有效字符之前遇到的空白,next()方法会自动将其去掉

3.只有输入有效字符才将其后面输入的空白作为分隔符或者结束符

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

  • nextLine

1.以Enter未结束符,也就是说nextLine()方法返回的是输入回车之前的所有字符

2.可以获取空白

 public static void main(String[] args) {
        //创建一个扫描对象,用于接收键盘数据
        Scanner scanner = new Scanner(System.in);
        //从键盘接收数据
        int i =0;
        float f =0.1f;
        System.out.println("请输入整数:");
        if (scanner.hasNextInt()){
            i = scanner.nextInt();
            System.out.println("输入的整数是:"+i);
        }else{
            System.out.println("你输入的不是整数数据");
        }

        System.out.println("请输入小数:");
        if (scanner.hasNextFloat()){
            f = scanner.nextFloat();
            System.out.println("输入的小数是:"+f);
        }else{
            System.out.println("你输入的不是小数数据");
        }
        scanner.close();
    }
public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    //和
    double sum = 0 ;
    //计算输入多少个数字
    int m= 0;
    while (scanner.hasNextDouble()){
        double x =scanner.nextDouble();
        m++;
        sum =sum+x;
    }
    System.out.println(m+"个数的和为:"+sum);
    System.out.println(m+"平均数为:"+sum/m);
    scanner.close();
}
------------------------------------------------------------------------------------
10
20
30
40
80
200
x
6个数的和为:380.0
6平均数为:63.333333333333336
Process finished with exit code 0

顺序结构

  • Java的基本结构就是顺序结构,除非特别指明,否则就按照顺序一句一句执行
  • 顺序结构是最简单的结构
  • 语句与语句之间,况愈况之间就是按照顺序进行的,它是由若干个依次执行的处理步骤组成的,他的任何一种算法都离不开一种基本算法结构

技术分享图片

选择结构

  • if单选结构
 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");

    }
  • if双选结构
public static void main(String[] args) {
    //考试分数大于60分及格,小于60分不及格
    Scanner scanner = new Scanner(System.in);
    System.out.println("请输入成绩:");
    Double score = scanner.nextDouble();
    if (score > 60) {
        System.out.println("及格");
    }else {
        System.out.println("不及格");
    }
    scanner.close();
  • if多选结构
 public static void main(String[] args) {
        //考试分数大于60分及格,小于60分不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入成绩:");
        Double score = scanner.nextDouble();
        if (score > 60) {
            System.out.println("及格");
        }else if (score>80&&score<90){
            System.out.println("良好");
        } else if (score<100&& score>90) {
            System.out.println("优秀");
        } else {
            System.out.println("不及格");
        }
        scanner.close();
  • 嵌套的if结构
  • switch多选结构
public static void main(String[] args) {
    char grade = ‘C‘;
    switch (grade){
        case ‘A‘: //case穿透现象
            System.out.println("优秀");
        break;
        case ‘B‘:
            System.out.println("良好");
        break;
        case ‘C‘:
            System.out.println("及格");
        break;
        case ‘D‘:
            System.out.println("再接再厉");
        break;
        default:
            System.out.println("为止等级");
    }
}

循环结构

while循环

  • while是最基本的循环

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

  • 我们大多数情况是会让循环停止下来的,我们需要一个表达式失效的当时来结束循环。

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

  • 循环条件一直为true就会造成无线循环【死循环】,我们正常编程中应该尽量避免死循环,会影响程序的性能或者造成卡死崩溃

    public static void main(String[] args) {
        //输出1-100;
        int i =0;
        while(i<100){
            i++;
            System.out.println(i);
        }
    }
    

do……while循环

  • 对于while语句而言,如果不满足条件,则不能进入循环。有时候我们需要即使不满足条件,也至少执行一次。
  • do……while训话和while循环相似,不同的是do……while循环至少循环一次
  • 二者区别都while先进行判断后执行,dowhile是先执行后判断

For循环

  • 虽然所有循环都可以用while循环或者是dowhile循环,但java提供另一种循环,for循环
  • for循环语句支持迭代的一种通用结构,是最有效、最灵活的循环结构
  • for循环的次数在执行前就是确定的
public static void main(String[] args) {
    //初始化值  条件判断 迭代
    for (int i=1;i<=100;i=i+2){
        System.out.println(i);
    }
   
}

题目1:计算0-100之间的奇数和和偶数和

public static void main(String[] args) {
  int oddsum =0;
  int evensum =0;
    for (int i = 0; i < 100; i++) {
        if(i%2==0){
            evensum+=i;
        }else {
            oddsum+=i;
        }
    }
    System.out.println("偶数为:"+evensum);
    System.out.println("奇数为:"+oddsum);
}
----------------------
偶数为:2450
奇数为:2500

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

public static void main(String[] args) {
    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");

        }
    }
}

打印九九乘法表

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

}
------------------------------------------------------------------------------
1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	

增强For循环

public static void main(String[] args) {
    //定义一个数组
    int [] number ={10,20,30,40,50};
    for(int x :number){
        System.out.println(x);
    }
}

break&continue

  • break在任何循环语句的主体部分,均可用break控制循环流程。break用于强行退出循环,不执行循环剩余语句。break也可在switch语句中使用。
  • continue也在任何循环语句的主体部分,用于种植某次循环过程,及跳过循环体中的尚未执行的语句,接着执行是否进行下一次循环判断。

练习

1.打印三角形 5行

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

Java流程控制

原文:https://www.cnblogs.com/suwenwu/p/14998263.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!