Java——控制结构与函数

一、选择结构:

  • if(布尔表达式) 单种case
  • if (布尔表达式) …else 两种case
  • if (布尔) …else if (布尔) …else 可以多种case

多重:switch(表达式)

  • 多个case分支
  • 满足一个分支后,需要break
  • 最后一个分支为default

二、循环结构

  • while
  • do…while
  • for
package Primary;

/**
 * Java控制结构
 * @version 15:21 2019-09-29
 * @author xxwang1018
 */

class IfElseTest { //选择结构之if-else
    public void i2() {
        int a = 5;
        if(a>1) {
            System.out.println("aaaaaaa");
        }

        if(a>10) {
            System.out.println("bbbbbbb");
        } else {
            System.out.println("ccccccc");
        }

        if(a>10) {
            System.out.println("dddddd");
        }
        else if(a>=5) {
            System.out.println("eeeeee");
        }
        else {
            System.out.println("ffffff");
        }
    }
}
class SwitchTest { //选择结构之switch
    public void s1() {
        int a1 = 1;
        int a2 = 2;
        switch(a1+a2) {
            case 1: System.out.println("aaaaaaa");
                break;
            case 2: System.out.println("bbbbbbb");
                break;
            case 3: System.out.println("ccccccc");
                //break;
            default:System.out.println("ddddddd");
        }

        String b = "abc";
        switch(b) {
            case "abc": System.out.println("eeeeeee");
                break;
            case "def": System.out.println("fffffff");
                break;
            case "hgi": System.out.println("ggggggg");
                break;
            default:System.out.println("hhhhhhh");
        }
    }
}
class WhileTest { //循环结构之while
    public void w1() {
        System.out.println("=============While Test==========");
        int x = 10;
        while (x < 20) {
            System.out.print("value of x : " + x);
            x++;
            System.out.print("\n");
        }

        System.out.println("=============Do  While Test==========");
        x = 10;
        do {
            x++;
            if(x%2 == 0) {
                continue;
            }
            System.out.println("value of x : " + x);
        } while (x < 20);
    }
}
class ForTest { //循环结构之for
    public void f2() {
        for(int i=0;i<5;i++) {
            for(int j=0;j<5;j++) {
                if(j<=i) {
                    System.out.print("*");
                } else {
                    break;
                }
            }
            System.out.println();
        }
    }
}

public class ControlStructure {
    public static void main(String[] args) {
        new IfElseTest().i2();
        new WhileTest().w1();
        new ForTest().f2();
        new SwitchTest().s1();
    }
}

中断控制流程语句

  • break:跳出当前所在循环,执行循环后面的语句
  • continue: 中断正常的控制流程,将控制转移到最内层循环的首部;若用于 for 语句,跳到循环变量的更新部分
  • 带标签的 break 语句:类似 goto 语句,用于跳出多重循环;标签必须放在希望跳出的最外层循环之前,并且紧跟一个冒号
  • 带标签的 continue 语句:跳到与标签匹配的循环首部
import java.util.*;
import java.math.*;
 
/**
 * This program discribes special "break" statements
 * @version 15:40 2019-03-27
 * @auther xxwang1018
 */
 
public class SpecialBreak{
    public static void main(String[] args) {
        int[] score = new int[10];
        int total=0;
        for (int i = 0; i < 10; ++i){
            score[i] = (int) (1 + Math.random() * 100); //随机产生学生分数
            total+=score[i]; //计算总分
        }
        double average=1.0*total/10; //计算平均分
        int counter=0;
        /**
         count 用来记录第一个低于平均分的学生的序号,且必须要初始化
         */
 
        read_data:
        while(average<total){
            for(int i=0; i<10; ++i){
                if(score[i]>average)
                    System.out.println("Congratulations! You are ok!"); //对超过平均分的学生鼓励
                else{
                    System.out.println("\nThe first student who doesn't get an average score appears.");
                    counter=i;
                    break read_data;
                }
                System.out.println(score[i]);
            }
        }
        System.out.println("\nThe average is "+average+"\nThis student's score is "+score[counter]);
    }
}

/*
测试结果:
Congratulations! You are ok!
92
Congratulations! You are ok!
85
 
The first student who doesn't get an average score appears.
 
The average is 61.6
This student's score is 39

三、函数

修饰词(public 或者 static) 返回值(int 或者void),函数名(形 参列表) {函数体}

  • 函数必须放在类的范围内。通常情况下,建议方法是public
  • 函数可以调用其他的函数,例如上例中,main函数调用了add函数
  • 递归函数调用,需要注意终止条件
  • 同一个类中,函数名称可以相同,即重载函数 (overload),但是函数参数的个数或者类型必须有所不同。不能以返回值来区分同名的函数
package Primary;

/**
 * Java函数
 * @version 15:42 2019-09-29
 * @author xxwang1018
 */

public class Function {
    public static void main(String[] args) {
        int a = 5;
        int b = factorialCalculation(a);
        System.out.println("The factorial of " + a + " is " + b);
    }

    public static int factorialCalculation(int m) {
        if (m > 1) {
            return m * factorialCalculation(m - 1);
        } else {
            return 1;
        }
    }
}

转载于:https://www.cnblogs.com/xxwang1018/p/11607032.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值