java 流程控制--狂神说

流程控制

Scanner

位于java.util.Scanner库 来获取用户输入

Scanner scanner = new Scanner(System.in);

借助Scanner的next() 和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("使用next方式接受");
        //判断用户有没有输入字符
        if (scanner.hasNext()) {
            String str = scanner.next();
            System.out.println("输入的内容为"+str);
        }
        //凡是属于IO的类如果不关闭会一直占用资源,要养成好习惯
        scanner.close();

    }
}

next():

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

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

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

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

nextLine():

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

2.可以获得空白

package scanner;

import java.util.Scanner;

public class Demo04 {
    public static void main(String[] args) {
        //我们可以输入多个数字,并求其总和和平均数,每输入一个数字用回车确认,通过输入一个非数字来结束输入并执行结果
       Scanner scanner= new Scanner(System.in);
       //和
        double sum =0;
        //计算输入多少个数字
        int m =0;
        System.out.println("请输入:");
        //通过循环判断是否还有属输入,并再在里面对每一次进行求和和统计
        while(scanner.hasNextDouble()){
            double x= scanner.nextDouble();
            m=++m;
            sum=sum+x;
            System.out.println("你输入了第"+m+ "个数据,总和为"+sum);
        }
        System.out.println(m+"个数的和为"+sum);
        System.out.println(m+"个数的平均值为"+(sum/m));





       scanner.close();
    }
}
package first.basic;
import java.util.Scanner;

public class HelloWorld {

    static Scanner input = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {//设置一个死循环
            try {
                acceptPrint();//当输入正确数据后,执行break语句,跳出循环
                break;
            } catch (Exception e) {
                input = new Scanner(System.in);
            }
        }
    }

    public static void acceptPrint() {
        System.out.println("请输入一个整数:");
        int temp = input.nextInt();
        System.out.println(temp);
    }
}

请输入一个整数:
s
请输入一个整数:
2
2
//多次输入直至正确
package first.basic;
//连续多次输入 一起输出
import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        Scanner scanner =new Scanner(System.in);
        int out= scanner.nextInt();
        int out2= scanner.nextInt();
        System.out.println(out);
        System.out.println(out2);

    }
}

2
5
2
5
//多次输入 一个输入 一个输出
 package first.scanner;

import java.util.Scanner;

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

        //从键盘接收数据
        int i = 0;
        float f = 0.0f;
        System.out.printf("请输入整数:");

        if (scanner.hasNextInt()) {
           i  = scanner.nextInt();
            System.out.println("整数数据:" + i);
        } else {
            System.out.println("输入的不是整数数据!");
        }
        System.out.printf("请输入小数:");
        if (scanner.hasNextFloat()) {
            f = scanner.nextFloat();
            System.out.println("小数数据:" + f);
        } else {
            System.out.println("输入的不是小数数据!");
        }
        scanner.close();
    }
}
请输入整数:1
整数数据:1
请输入小数:1.0
小数数据:1.0

结构

顺序结构

自上而下 依次进行

if选择结构

1.if 单选择结构:

if(布尔表达式){//为true 则进行}

package first.Structure;

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")){
            System.out.println(s);

        }


        System.out.println("END");
        scanner.close();
    }
}
请输入内容:
sd
END
请输入内容:
hello
hello
END

2.if 双选择结构

if(布尔表达式){

​ //为true 则进行

}else{

//为false 则进行

}

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

3.if多选择结构构

if(布尔表达式1){

​ //布尔表达式1为true 则进行

}else if(布尔表达式2){

//布尔表达式2为true 则进行

}else if(布尔表达式3){

//布尔表达式3为true 则进行

}else ){

//以上皆false则进行

package first.Structure;

import java.util.Arrays;
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();
        int[] score = new int[10];
        for (int i = 0; i < 10; i++) {
            System.out.println("请输入成绩:");
            score[i] = scanner.nextInt();
            if (score[i] == 100) {
                System.out.println("恭喜满分");
            } else if (score[i] >= 90 && score[i] < 100) {
                System.out.println("成绩A级");
            } else if (score [i]>= 80 && score[i] < 90) {
                System.out.println("成绩B级");
            } else if (score[i] >= 60 && score[i] < 80) {
                System.out.println("成绩C级");
            } else if (score[i] >= 0 && score[i] < 60) {
                System.out.println("成绩不合格");
            } else {
                System.out.println("输入不正确");
            }
    }
        scanner.close();
    }
}
//连续输入10次判断
请输入成绩:
100
恭喜满分
请输入成绩:
85
成绩B级
请输入成绩:
74
成绩C级
请输入成绩:
32
成绩不合格
请输入成绩:
10
成绩不合格
请输入成绩:
。。。

4.嵌套if结构

if(布尔表达式1){

​ //布尔表达式1为true 则进行

​ if(布尔表达式2){

​ //布尔表达式1为true 则进行

}

}

package first.basic;


import java.util.Arrays;
import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] num=new int[10];
        for (int i = 0; i < 10; i++) {
            num[i] = scanner.nextInt();
        }
        System.out.println(Arrays.toString(num));
        if(num[0]==1){
            if(num[1]==2){
                System.out.println(num[0]+num[1]);
            }else{
                System.out.println("第二位不是我想要的");
            }
        }else{
            System.out.println("第一位不是我想要的");
        }
    }
}
//输入10位int  然后判断 第一位 不对则输出:第一位不是我想要的 对则判断第二位 不对则输出:第二位不是我想要的 对则输出 3(num[0]+num[1])

switch选择结构

switch(expression){
    case value:
        //语句
        break;//可选
    case  value:
         //语句
        break;//可选
        //可有任意多个case语句
    default://可选
        //语句
}
package first.Structure;


public class Demo04 {
    public static void main(String[] args) {
        char grade = 'C';
        switch (grade){
            case 'A':
                System.out.println("优秀");
            case 'B':
                System.out.println("良好");
            case 'C':
                System.out.println("及格");
            case 'D':
                System.out.println("再接再厉");
                break;//没有break 匹配成功后每一项都会运行 故应该每次都加上break
            case 'E':
                System.out.println("挂科");
            default:
                System.out.println("未知");
        }
    }
}
及格
再接再厉

反编译:

在这里插入图片描述

在这里插入图片描述

找到对应处的class文件 复制
在这里插入图片描述

打开Java文件的文件夹 粘贴进去 即可

循环结构

while循环

while(布尔表达式){

​ //循环内容

}

布尔表达式为true 运行

大多数我们需要设置停止条件

package first.Structure;

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

do…while循环

do while至少执行一次

do{

​ //代码语句

}while(布尔表达式);

while 和 do while 区别:

1.while先判断后执行 do while先执行后判断

2.do while只是执行一次

package first.Structure;

public class Demo09 {
    public static void main(String[] args) {
        int a=0;
        int i=0;
        while(a<0){
            System.out.println(a);
            a++;
        }
        System.out.println("==========================");
        do{
            System.out.println(i);
            i++;
        }while(i<0);
    }
}


==========================
0

For 循环

for(初始化;布尔表达式;更新){

//代码语句

}

注意点:

1.最先执行初始化的步骤,可以声明一种类型,但可初始化一个或多个循环变量,也可以为空

2.然后,检测布尔表达式的值,为true,循环体被执行;为false,循环终止,开始执行循环体后语句

3.执行一次循环体后,更新循环变量

4.再次检测布尔表达式,循环上面过程

两个因素同时循环

for (int i = 0, j=array.length-1; i < array.length; i++,j--) {

        result[j]=array[i];
    }

死循环

for(;😉{}

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

package first.lesson;

public class Lesson01 {
    public static void main(String[] args) {
        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);
        System.out.println("偶数的和" + evenSum);

    }
}
奇数的和2500
偶数的和2550

2.while或for 循环输出1-100之间能被五整除的数 并且每行输出三个

public class Lesson02 {
    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.print("\n");
                System.out.println();//println输入会换行 print不会换行
            }
            
        }
    }
}
0
5 10 15
 。。。
package first.lesson;

public class lesson03 {
    public static void main(String[] args) {
        int i =0;
        while(i<=1000){
            i++;
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%15==0){
                System.out.println();
            }

        }
    }
}
5 10 15
 。。。

3.99乘法表

package first.lesson;
//99乘法表
public class Lesson04 {
    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();

        }
    }
}

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循环

for(声明语句;表达式){

//语句

}

package first.Structure;

public class Demo11 {
    public static void main(String[] args) {
        int[] numbers={10,20,30,40,50};//定义一个数组
        for (int i = 0; i < 5; i++) {
            System.out.println(numbers[i]);

        }
        System.out.println("=============================");
        //遍历数组的元素  增强for
        for (int x : numbers){
            System.out.println(x);
        }
    }
}
10
20
30
40
50

break continue

break终止循环 强行退出

package first.Structure;

public class Demo12 {
    public static void main(String[] args) {
        int i=0;
        while(i<100){
            i++;
            System.out.print(i+"\t");
            if(i==30){
                break;
            }
        }
    }
}
1	2	3	4	5	6	7	8	9	10	11	12	13	14	15	16	17	18	19	20	21	22	23	24	25	26	27	28	29	30

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

public class Demo13 {
    public static void main(String[] args) {
        int i=0;
        while(i<10){
            i++;
            if(i%2==0){
                continue;
            }
            System.out.println(i);
        }
        //break 在任何循环语句的主体部分 均可用break控制循环的流程
        //  break强制退出循环 不执行循环语句中的剩余语句
        //continue 语句用在循环体中,用于终止某次循环,即跳过语句中未执行的部分,直接进行下一次是否循环的判断
    }
}
	1
    3
    5
    7
    9

关于goto(了解)

Java没有使用goto 但可以再break和continue加入标签

联系

打印三角形

public class Lesson05 {
    public static void main(String[] args) {
        //打印三角形 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 =2;j<=i;j++){
                System.out.print("*");
            }
            System.out.println();

        }
    }
}
    *
   ***
  *****
 *******
*********
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
前 言 1 1 概 述 2 1.1 选题背景 2 1.2 组织结构 2 2 所用相关技术和方法 3 2.1 工作流 3 2.1.1 什么叫工作流 3 2.1.2 工作流发展 3 2.1.3 工作流的优点 3 2.2 MVC工作模式 4 2.2.1 MVC设计思想 4 2.2.2 MVC的具体实现 5 2.2.3 MVC的不足 6 2.3 JSP技术介绍 6 2.3.1 JSP的运行原理 7 2.3.2 JSP的生命周期 8 2.3.3 Servlet和JavaBean技术介绍 8 2.3.4 Java 虚拟机 9 2.3.5 JSP访问SQL Server 2000数据库 9 2.4 数据库后台环境配置 10 2.5 系统开发工具简介 10 2.5.1 Dreamweaver 10 2.5.2 MyEclipse 10 2.5.3 Tomcat 11 2.5.4 SQL Server2000 11 2.5.5 chs_sql2ksp3 12 3 系统需求分析 13 3.1 系统功能分析 13 3.2 系统性能分析 13 3.3 系统方案的确定和评价 13 4 系统总体设计 15 4.1 系统层次模块图 15 4.1.1 营业厅模块 15 4.1.2 收费管理模块 16 4.2 系统数据流程图 16 4.3 数据表设计 18 5 详细设计及编码 21 5.1 编写JAVABEAN 21 5.2 营业厅实现函数 21 5.3 收费厅主要的实现函数 22 5.4 JAVABEAN主要实现模块 22 5.4.1 中文字符格式的转换模块(Stringto.java) 22 5.4.2 自动生成验证码(Ran.java) 22 5.4.3 数据库的连接(ConnectionFactory.java) 23 5.4.4 数据库连接的关闭(DatabaseUtils.java--只提供接口 23 5.4.5 密码修改模块(Common_fuction.java) 24 5.4.6 时间格式转换(timeBean.java) 24 5.4.7 数据统计(counthander.java) 25 5.4.8 营业厅的接口(luruaction.java) 27 5.4.9 营业厅的主要函数实现(luruhander.java) 28 5.4.10 收费厅的主要函数接口 32 5.5 管理员登陆模块 33 5.5.1 管理员登录 33 5.6 营业厅管理模块 36 5.6.1 Left.jsp页面 36 5.6.2 Work.jsp 40 5.6.3 customerlistinfo.jsp 41 5.6.4 allinfo.jsp 41 5.7 收费厅管理模块 42 5.7.1 Left.jsp 42 5.7.2 Work.jsp 43 5.7.3 Customerlistinfo.jsp 43 5.7.4 gongdan.jsp 43 6 系统测试与维护 45 6.1 测试目的 45 6.2 测试环境 45 6.3 系统测试 45 6.4 系统维护 45 7 开发难点与技术 46 7.1 主要程序实现的代码描述 46 7.1.1 验证码的自动生成 46 7.1.2 生成WORD工单 46 7.1.3 以一定的时间刷新页面 47 7.1.4 JSP中文问题的解决 47 7.2 在程序编码过程遇到的主要问题: 48 7.3 代码编写风格 49 7.4 我的不足: 49 结束语 50 致 谢 50

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值