Java流程控制

一、Scanner对象

Scanner类来获取用户的输入
基本语法:

Scanner s = new Scanner(System.in);

通过Scanner类的next()与nextLine()方法获取输入的字符串,在读取我们一般需要使用hasNext()与hasNextLine()判断是否还有输入的数据。
next():不能得到带有空格的字符串。
nextLine():可以获得带空格的字符串。

package Scanner;

import java.util.Scanner;

public class Demo1 {
    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 Scanner;

import java.util.Scanner;

public class Demo2 {
    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();
    }
}
package Scanner;

import java.util.Scanner;

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

        //从键盘接收数据
        int i =0;
        float f =0.0f;

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

import java.util.Scanner;

public class Demo5 {
    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 = m + 1; //统计个数
            sum = sum + x;
        }

        System.out.println("和为:"+sum);
        System.out.println("平均值为:"+(sum/m));

        scanner.close();
    }
}

二、顺序结构

Java的基本结构就是顺序结构。

三、选择结构

if 单选择结构:
if(布尔表达式){
//如果布尔表达式为true将执行的语句
}

package structure;

import java.util.Scanner;

public class Demo1 {
    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 structure;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args){
        //考试分数大于60分就是及格,小于60分就是不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入分数:");
        int score = scanner.nextInt();
        if(score>=60){
            System.out.println("及格了,你的分数是:"+score);
        }else{
            System.out.println("没及格,你的分数是:"+score);
        }
        scanner.close();

    }
}

if 多选择结构:
if(布尔表达式 1){
//如果布尔表达式 1为true将执行的语句
}else if(布尔表达式 2){
//如果布尔表达式2为true将执行的语句
}else if(布尔表达式 3){
//如果布尔表达式3为true将执行的语句
}else{
/如果以上表达式均为false将执行的语句
}

package structure;

import java.util.Scanner;

public class Demo2 {
    public static void main(String[] args){
        //考试分数大于60分就是及格,小于60分就是不及格
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入分数:");
        int score = scanner.nextInt();
        if(score == 100){
            System.out.println("恭喜满分");
        }else if(score<100 && score>=80){
            System.out.println("优秀,你的分数是:"+score);
        }else if(score <80 && score >=60){
            System.out.println("及格了,你的分数是:"+score);
        }else if(score<60 && score>=0){
            System.out.println("不及格,你的分数是:"+score);
        }else {
            System.out.println("输入的成绩有误!");
        }
        scanner.close();
    }
}

嵌套的if结构:
if(表达式1){
if(表达式2){
}
}

switch 多选择结构:
Switch(expression){
case value:
//语句
break;
case value:
//语句
break;
default:
//语句
}

package structure;

public class SWITCH {
    public static void main(String[] agrs){
        char grade = 'C';
        //加上break,终止;不加break,case穿透现象,后面的都会被输出
        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("未知等级");
        }
    }
}

switch匹配字符串:

package structure;

public class switchDemo {
    public static void main(String[] args){
        String name ="贝贝";
         switch (name){
             case "贝贝":
                 System.out.println("贝贝");
                 break;
             case "贝店":
                 System.out.println("贝店");
                 break;
             default:
                 System.out.println("其他");
         }
    }
}

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

package structure;

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

1-100之和:

package structure;

public class whileDemo2 {
    public static void main(String[] args){
        //计算1+2+3+...+100
        int i = 0;
        int sum = 0;
        while (i<100){
            i++;
            sum = sum + i;
        }
        System.out.println(sum);
    }
}

do … while 循环:
do{
//执行语句
}while(布尔表达式)

package structure;

public class whileDemo2 {
    public static void main(String[] args){
        //计算1+2+3+...+100
        int i = 0;
        int sum = 0;
        do{
            i++;
            sum = sum+i;
        }while (i<= 99);
        System.out.println(sum);
    }
}

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

package structure;

public class forDemo1 {
    public static void main(String[] args){
        int sum = 0;
        for(int i=1;i <=100;i++){
            sum = sum+i;
        }
        System.out.println(sum);
    }
}

在这里插入图片描述
第一句语句可以为空语句:for(; i<100;i++)
练习1:

package structure;

public class forDemo2 {
    public static void main(String[] args){
        //计算0-100奇数的和和偶数的和
        int oddSum = 0;
        int evenSum = 0;

        for (int i = 0; i <= 100; i++) {
            if (i%2!=0){
                oddSum =oddSum+i;
            }else{
                evenSum = evenSum+i;
            }
        }
        System.out.println("0-100奇数和为:"+oddSum);
        System.out.println("0-100偶数和为:"+evenSum);
    }
}

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

package structure;

public class forDemo3 {
    public static void main(String[] args){
        //用while或for循环循环输出1-1000之间能被5整除的数,并每行输出3个
        for (int i = 1; i <= 1000; i++) {
            if(i%5 == 0){
                System.out.print(i+"\t");
            }
            if(i%(5*3) == 0){
                System.out.println();//换行
            }
        }
        System.out.println();
        System.out.println("______________");
        //print 输出不换行
        //println 输出要换行
        int i = 1;
        while(i<=1000){
            if (i%5==0){
                System.out.print(i+"\t");
            }
            if (i%(5*3)==0){
                System.out.println();
            }
            i++;
        }
    }
}

练习3:打印九九乘法表

package structure;

public class forDemo {
    public static void main(String[] args){
        //打印九九乘法表
        for (int i = 1; i < 10; i++) {
            for (int j = 1;  j<= i; j++) {
                    System.out.print(j+"*"+i+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}

增强for循环:主要用来遍历数组和集合
for(声明语句:表达式){
//代码语句
}

package structure;

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

break 强制退出循环

package structure;

public class breakDemo {
    public static void main(String[] args){
        //break 强制退出循环
        int i =0;
        while(i<100){
            i++;
            System.out.println(i);
            if(i==10){
                break;
            }
        }
    }
}

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

package structure;

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.print(i+"\t");
        }
    }
}

goto:在Java中未使用

package structure;

public class labelDemo {
    public static void main(String[] args){
        //打印101-150之间的质数(只能被自身和1整除)
        int count = 0;
        outer:for (int i = 101; i <= 150; i++) {
            for (int j = 2;j<i/2; j++ ){
                if (i%j == 0){
                    continue outer; //从内部循环,跳到外部循环
                }
            }
         System.out.println(i);
        }
    }
}

打印三角形:

package structure;

public class testDemo {
    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 = 1;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、付费专栏及课程。

余额充值