Java基础自学记录四

程序流程控制

此为笔者个人笔记,如有错误还请指出

if-else

  • 单分支

  • 双分支

  • 多分支

  • 嵌套分支(注意不要超过三层,可读性不好)

import java.util.*;
public class IfTest{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int age = input.nextInt();
        if(age>=18){
        	System.out.println("Congratulations!");
        }else{
            System.out.println("Oh no!");
        }
        int year = input.nextInt();
        if((year%4==0&&year%100!=0)||(year%400==0)){
            System.out.println("yes");
        }
        double a = input.nextDouble();
        double b = input.nextDouble();
        if(a<10&&b>30){
            System.out.println("a+b=" + (a+b));
        }else{
            System.out.println("a*b=" + (a*b));
        }
    }
}
import java.util.*;
public class IfelseTest{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        boolean b = input.nextBoolean();
        if(b = false){//测试一下会不会输出a  不会执行这一条,只会输出c
            System.out.println("a");
        }else if(b){
            System.out.println("b");
        }else if(!b){
            System.out.println("c");
        }else{
            System.out.println("d");
        }
    }
}
import java.util.*;
public class IfelseTest{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        double score = input.nextDouble();
        char gender = input.next().charAt(0);//提高容错性
        if(score>8.0){
            System.out.println("Congratulations!");
            if(gender=='男'){
                System.out.println("male");
            }else if(gender=='女'){
                System.out.println("famale");
            }
        }else{
            System.out.println("Sorry");
        }
    }
}

switch分支结构

switch(表达式){
    case 常量1:
        语句块;
        break;
    case 常量2:
        语句块;
        break;
    case ...:
        ...;
        break;
    default:
        缺省语句块;
        break;
}
//表达式的值等于哪个常量执行哪个语句
import java.util.*;
public class SwitchTest{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int a = input.nextInt();
        switch(a){
            case 1:
                System.out.println("1");
                break;
            case 2:
                {
                    System.out.println("2");
                    break;
                }
            default:
                System.out.println("3");
                break;
        }
    }
}

一个用switch-case语句的穿透特性的题

//输入月份对季节进行判断
import java.util.*;
public class MSeason{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int month = input.nextInt();
        if(month>=1&&month<=12) {
            switch(month) {
                case 3:
                case 4:
                case 5:
                    System.out.println("Spring");
                    break;
                case 6:
                case 7:
                case 8:
                    System.out.println("Summer");
                    break;
                case 9:
                case 10:
                case 11:
                    System.out.println("Autumn");
                    break;
                case 12:
                case 1:
                case 2:
                    System.out.println("Winter");
                    break;
            }
        }else {
            System.out.println("Wrong number!");
        }
    }
}

for循环

for(;;){
    ...
}
//表示无限循环
public class Homework{
    public static void main(String[] args){
        for(int i = 1; i <= 100; i++){
            if(i%9==0) System.out.print(i+"\t");
        }
        System.out.println();
    }
}

while循环

注意循环条件是返回布尔值的一个值,不像C++那样

do…while循环

do{
    ...;
}while(...);

注意:1、while 后有一个分号 2、先执行,再判断,一定会执行一次

int i = 1, cnt = 0;
do{
    if(i%5==0&&i%3!=0){
        cnt++;
    }
    i++;
}while(i <= 100);
System.out.println(cnt);

多重循环控制

建议最多不超过三层

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

练习:打印空心金字塔

import java.util.*;
public class Stars{
    public static void main(String[] args){
        Scanner input = new Scanner();
        int totalLevel = input.nextInt();
        int b = (totalLevel) * 2 - 1;
        for(int i = 1; i <= totalLevel; i++){
            int kg = (b - (2 * i) + 1) / 2;
            //输出左边空格
            for(int j = 1; j <= kg; j++){
                System.out.print(" ");
            }
            //输出*和中间空格
            for(int j = 1; j <= ((2 * i) - 1); j++){
                if(i != totalLevel){
                    //开始这里的||写成了**
                    //呃呃,可以直接写到一起
                    //if(j == 1 || j == ((2 * i) - 1) || i != totalLevel)
                    if(j == 1 || j == ((2 * i) - 1)){
                        System.out.print("*");
                    }
                    else{
                        System.out.print(" ");
                    }
                }
                else {
                    System.out.print("*");
                }
            }
            //输出右边空格
            for(int j = 1; j <= kg; j++){
                System.out.print(" ");
            }
        }
    }
}

break语句

break语句在多层嵌套的语句块中可以通过标签(lable)来指明要终止哪一层语句块

//标签不一定要写成lable, 只要满足标识符规则就行
public class BreakTest{
    public static void main(String[] args){
        lable1:
        for(int i = 0; i < 4; i++){
            lable2:
            for(int j = 0; j < 4; j++){
                if(i == 3){
                    break lable1;
                }
                System.out.println("i and j = " + i + ", " + j);
            }
        }
    }
}

实际开发时不建议使用标签,没有标签时默认退出最近的循环体

课堂练习(登录验证)

要点:String比较是否相等用.equals()方法,注意这里尽量将已知非空字符串放在.equals()前面,如"ymby".equals(name),避免由于空指针使得程序崩溃

import java.util.*;
public class BreakTest{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        String name = "", password = "";
        for(int i = 0; i < 3; i++){
            name = input.next();
            password = input.next();
            System.out.println(name+" "+password);
            //注意字符串内容相等用equals
            if("ymby".equals(name) && "955".equals(password)){
                break;
            }
        }
    }
}

continue语句

结束这层循环,但继续进行上层循环

可以用标签指定跳过哪一层循环

跳转控制语句 return

表示退出方法

homework

public class homework1{
    public static void main(String[] args){
        double sum = 100000;
        int cnt = 0;
        while(true){
            if(sum > 50000){
                sum -= sum*0.05;
                //可以简洁写为 sum*=0.95
                cnt++;
            }else if(sum >= 1000){
                sum -= 1000;
                cnt++;
            }else{//钱不够
                break;
            }
        }
        System.out.println(cnt);
    }
}
import java.util.*;
public class homework1{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int year = input.nextInt();
        if((year%4==0&&year%100!=0)||year%400==0){
            System.out.println("yes");
        }
    }
}
//水仙花数
import java.util.*;
public class homework1{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int num = input.nextInt();
        int copy = num % 100;
        int h = num / 100;
        int o = copy % 10;
        int t = copy / 10;
        if(Math.pow(o, 3) + Math.pow(h, 3) + Math.pow(t, 3) == num){
            System.out.println("yes");
        }
    }
}
//1-1/2+1/3-1/4+1/5+...+1/100
import java.util.*;
public class homework1{
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        int wt = input.nextInt();
        int sum = 0;
        for(int i = 1; i < wt; i++){
            int psum = 0;
            for(int j = 1; j <= i; j++){
                psum+=j;
            }
            sum+=psum;
        }
        System.out.println(sum);
    }
}
public class homework1{
    public static void main(String[] args){
        for(int i = 0; i < 26; i++){
            System.out.println((char)('a' + i));
        }
        for(int i = 0; i < 26; i++){
            System.out.println((char)('Z' - i));
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值