第三章 控制语句

第三章 控制语句

控制语句分类

  • 顺序结构
  • 选择结构
if(){ 
}

if(){    }
else {  }

if(){   }
else if() {  }
....
else {   }

JDK1.5之前必须是Int整数 
JDK1.7之后可以是String
switch(表达式){
case value1: 
语句;
break;
case value2:
语句2;
break;
default: 语句;
}
  • 循环结构
while() {} 

do {   }
while();

for(初始表达式; 布尔表达式; 迭代因子){  循环体; }
  • break;
  • continue;
  • return;(用于跳出当前方法/c/c++中的子函数)
  • 带标签的 break;continue; 类比于 goto(lable:) 自定义名字
public class Test18 {
    public static void main(String args[]) {
        outer: for (int i = 101; i < 150; i++) {
            for (int j = 2; j < i / 2; j++) {
                if (i % j == 0){
                    continue outer;
                }
            }
            System.out.print(i + "  ");
        }
    }
}

java.lang 包 Math类几个函数

Math.PI=3.141592.......
Math.pow(number,次方数);
Math.randow();随机生成double [0,1)数
(int)(number*Math.random())生成整形的 数

3.4 方法

[修饰符1  修饰符2  …]   返回值类型    方法名(形式参数列表){
    Java语句;… … …
 }

非静态 需要在 main()中 new 一个在调用
静态中可以直接调用

3.5 方法的重载(overload)

方法的重载:是指在一个类中可以定义多个同名的方法

可以重载:1)形参个数不同 2)形参个数相同但是类型不同
不能重载:1)形参个数相同类型相同前提下–> 返回值不同–>2)形参名称不同

示例
public class Test21 {
    public static void main(String[] args) {
        System.out.println(add(3, 5));// 8
        System.out.println(add(3, 5, 10));// 18
        System.out.println(add(3.0, 5));// 8.0
        System.out.println(add(3, 5.0));// 8.0
        // 我们已经见过的方法的重载
        System.out.println();// 0个参数
        System.out.println(1);// 参数是1个int
        System.out.println(3.0);// 参数是1个double
    }
    /** 求和的方法 */
    public static int add(int n1, int n2) {
        int sum = n1 + n2;
        return sum;
    }
    // 方法名相同,参数个数不同,构成重载
    public static int add(int n1, int n2, int n3) {
        int sum = n1 + n2 + n3;
        return sum;
    }
    // 方法名相同,参数类型不同,构成重载
    public static double add(double n1, int n2) {
        double sum = n1 + n2;
        return sum;
    }
    // 方法名相同,参数顺序不同,构成重载
    public static double add(int n1, double n2) {
        double sum = n1 + n2;
        return sum;
    }
    //编译错误:只有返回值不同,不构成方法的重载
    public static double add(int n1, int n2) {
        double sum = n1 + n2;
        return sum;
    }
    //编译错误:只有参数名称不同,不构成方法的重载
    public static int add(int n2, int n1) {
        double sum = n1 + n2;         
        return sum;
    }  
}

3.6 递归结构

同C/C++一样
能用递归解决的问题都能通过循环解决
在要求高性能,尽量避免使用递归,特别占用时间以及内存

课后部分习题

2.下列选项中关于变量x的定义,( )可使以下switch语句编译通过。(选择二项)

switch(x) {
    case 100 :
        System.out.println("One hundred");
        break;
    case 200 :              
        System.out.println("Two hundred");                 
        break;
    case 300 :
        System.out.println( "Three hundred");
        break;
    default :
        System.out.println( "default");    
}

A.double x = 100;

B.char x = 100;

C.String x = “100”;//我的java不行,,,不是说jdk1.7以上可以嘛…

D.int x = 100;
3.给定如下Java代码,编译运行的结果是( )。(选择一项)

public class Test { 
    public static void main(String[] args) {
        int sum=0;
        for(int i=1;i<10;i++){
            do{
                i++;
                if(i%2!=0)
                    sum+=i;
            }while(i<6);
        }
        System.out.println(sum);
    }
}

A.8//慢慢推别急.有点意思

B.15

C.24

D.什么也不输出

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值