Java流程控制

输入输出

输出

System.out.print() :不换行输出
System.out.println():换行输出,
例如:

public class Main {
    public static void main(String[] args) {
        System.out.print("A,");
        System.out.print("B,");
        System.out.print("C.");
        System.out.println();
        System.out.println("END");
    }
}

输出结果:

A,B,C.
END

System.out.printf():格式化输出,通过使用占位符%?,指定输出格式
例如:

public class Main {
    public static void main(String[] args) {
        double d = 3.1415926;
        System.out.printf("%.2f\n", d); // 显示两位小数3.14
        System.out.printf("%.4f\n", d); // 显示4位小数3.1416
        double e = 12900000;
        System.out.print(e);
        System.out.println();
        System.out.println(e);
        System.out.printf("%e\n", e);
        int n = 12345000;
        System.out.printf("n=%d, hex=%08x", n, n); // %08x表示将整数 n 以十六进制形式输出,并确保输出的十六进制字符串长度为8位,不足8位时前面补0。
    }
}

输出结果:

3.14
3.1416
1.29E7
1.29E7
1.290000e+07
n=12345000, hex=00bc5ea8
Process finished with exit code 0

占位符说明:
在这里插入图片描述
⚠️由于%表示占位符,因此,连续两个%%表示一个%字符本身。

输入

scanner类:用于从各种输入源(例如:键盘、文件、字符串)读取并解析文本数据。

  1. 特点
    (1)简单易用:支持多种数据类型的读取(如整数、浮点数、字符串等)。
    (2)支持分隔符:默认使用空格或换行作为分隔符,也可以自定义。
    (3)灵活输入源:可以从控制台、文件、网络流、甚至字符串中读取数据。
  2. 常用方法
    (1)读取输入:
    在这里插入图片描述
    (2)检查输入:
    在这里插入图片描述
    (3)其他:
    在这里插入图片描述

控制台输入

Scanner scanner = new Scanner(System.in); 创建Scanner对象,传入System.in标准输入流,通过调用 scanner 的方法(如 nextInt()nextLine())读取并处理输入数据。

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入第一次考试成绩:");
        double score1 = scanner.nextDouble();
        System.out.println("请输入第二次考试成绩:");
        double score2 = scanner.nextDouble();
        double percentage = ((score2-score1)/score1)*100;
        System.out.printf("成绩提升百分比为:%.2f%%\n", percentage);
        scanner.close();
    }
}

运行结果:

请输入第一次考试成绩:
80
请输入第二次考试成绩:
86
成绩提升百分比为:7.50%

if条件判断

基础语法:

if (条件) {
	// 条件满足时执行
}

当if语句块只有一行语句时,可以省略花括号{}:

if (n>=60)
	System.out.println("成绩及格!");

if…else if…else:

if (n>=85) {
	System.out.println("优秀!");
}
else if (n>=60) {
	System.out.println("及格!");
}
else {
	System.out.println("不及格!");
}

⚠️由于浮点数在计算机中无法精确表示,所以在判断时要用临界值,而不用==判断相等:

public class Main {
    public static void main(String[] args) {
        double x = 1 - 9.0 / 10;
        if (Math.abs(x - 0.1) < 0.00001) {
            System.out.println("x is 0.1");
        } else {
            System.out.println("x is NOT 0.1");
        }
    }
}

引用类型判断相等:
==判断“引用是否相等”,即指向的对象是否一致
equals()判断变量内容是否相等

public class Main {
	public static void main(String[] args) {
		String s1 = "hello";
        String s2 = "HELLO".toLowerCase();  // toLowerCase()将字母改成小写
        System.out.println(s1);
        System.out.println(s2);
        if (s1 == s2) {
            System.out.println("s1 == s2");
        }
        else {
            System.out.println("s1 != s2");
        }
        if (s1.equals(s2)) {
            System.out.println("s1 equals s2");
        } else {
            System.out.println("s1 not equals s2");
        }
	}
}

运行结果:

hello
hello
s1 != s2
s1 equals s2

switch多重选择

switch语句根据switch (表达式)计算的结果,跳转到匹配的case结果,然后继续执行后续语句,直到遇到break结束执行。如果option的值没有匹配到任何case,执行default:

public class Main {
    public static void main(String[] args) {
        String fruit = "apple";
        switch (fruit) {
        case "apple":
            System.out.println("Selected apple");
            break;
        case "pear":
            System.out.println("Selected pear");
            break;
        case "mango":
            System.out.println("Selected mango");
            break;
        default:
            System.out.println("No fruit selected");
            break;
        }
    }
}

从Java 12开始,switch语句升级为更简洁的表达式语法:

public class Main {
    public static void main(String[] args) {
        String fruit = "apple";
        switch (fruit) {
        case "apple" -> System.out.println("Selected apple");
        case "pear" -> System.out.println("Selected pear");
        case "mango" -> {
            System.out.println("Selected mango");
            System.out.println("Good choice!");
        }
        default -> System.out.println("No fruit selected");
        }
    }
}

新语法直接返回值:

public class Main {
    public static void main(String[] args) {
        String fruit = "apple";
        int opt = switch (fruit) {
            case "apple" -> 1;
            case "pear", "mango" -> 2;
            default -> 0;
        }; // 注意赋值语句要以;结束
        System.out.println("opt = " + opt);
    }
}

用yield返回一个值作为switch语句的返回值(yield只能用在新的表达式语法中):

public class Main {
    public static void main(String[] args) {
        String fruit = "orange";
        int opt = switch (fruit) {
            case "apple" -> 1;
            case "pear", "mango" -> 2;
            default -> {
                int code = fruit.hashCode();
                yield code; // switch语句返回值
            }
        };
        System.out.println("opt = " + opt);
    }
}

while循环

while (条件表达式) {
    循环语句
}

do…while循环

do {
    执行循环语句
} while (条件表达式);

for循环

for (初始条件; 循环检测条件; 循环后更新计数器) {
    // 执行语句
}

break与continue

break会跳出当前循环,整个循环将不再执行
continue会提前结束本次循环,继续执行下一次循环

石头剪刀布小游戏

import java.util.Scanner;
import java.util.Random;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String playerMove = "";
        while (!playerMove.equals("q")) {
            System.out.println("请输入j、t、b分别代表剪刀、石头、布,输入q退出游戏:");
            playerMove = scanner.nextLine();
            Random random = new Random();
            if (playerMove.equals("q")) {
                System.out.println("游戏结束");
                break;
            }
            // 生成一个 0 到 2 的随机整数
//            int randomInt = random.nextInt(3);
            // 生成一个 0.0 到 1.0 之间的随机浮点数
            double randomDouble = random.nextDouble();

            // 根据随机数决定生成的数
            int randomInt;
            if (randomDouble < 0.3) {
                randomInt = 0; // 30% 的概率
            } else if (randomDouble < 0.8) {
                randomInt = 1; // 50% 的概率
            } else {
                randomInt = 2; // 20% 的概率
            }
            // 根据随机数决定 computerMove 的值
            String computerMove;
            switch (randomInt) {
                case 0:
                    computerMove = "剪刀";
                    break;
                case 1:
                    computerMove = "石头";
                    break;
                case 2:
                    computerMove = "布";
                    break;
                default:
                    computerMove = "错误"; // 这个分支理论上不会被执行
                    break;
            }
            System.out.println("电脑出:" + computerMove);
            switch (playerMove) {
                case "j":
                    switch (computerMove) {
                        case "剪刀":
                            System.out.println("平局");
                            break;
                        case "石头":
                            System.out.println("你输了");
                            break;
                        case "布":
                            System.out.println("你赢了");
                            break;
                    }
                    break;
                case "t":
                    switch (computerMove) {
                        case "剪刀":
                            System.out.println("你赢了");
                            break;
                        case "石头":
                            System.out.println("平局");
                            break;
                        case "布":
                            System.out.println("你输了");
                            break;
                    }
                    break;
                case "b":
                    switch (computerMove) {
                        case "剪刀":
                            System.out.println("你输了");
                            break;
                        case "石头":
                            System.out.println("你赢了");
                            break;
                        case "布":
                            System.out.println("平局");
                            break;
                    }
                    break;
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勇敢磊磊学IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值