廖雪峰JAVA学习日志day2

    技术2025-02-16  19

    廖雪峰JAVA学习日志day2

    Java version-Java14 IDE-Eclipse

    输入和输出

    System.out.println()表示输出并换行 如果不想换行可以用System.out.print()

    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(),通过使用占位符%?,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 } } 3.14 3.1416

    占位符-说明 %d -格式化输出整数 %x -格式化输出十六进制整数 %f -格式化输出浮点数 %e -格式化输出科学计数法表示的浮点数 %s -格式化字符串 注意,由于%表示占位符,因此,连续两个%%表示一个%字符本身。

    创建Scanner对象并传入System.in

    import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 创建Scanner对象 System.out.print("Input your name: "); // 打印提示 String name = scanner.nextLine(); // 读取一行输入并获取字符串 System.out.print("Input your age: "); // 打印提示 int age = scanner.nextInt(); // 读取一行输入并获取整数 System.out.printf("Hi, %s, you are %d\n", name, age); // 格式化输出 } }

    数组批量赋值

    /*用到了Arrays.fill(arr,100);方法*/ import java.lang.reflect.Array; import java.util.Arrays; public class Main { public static void main(String[] args) { int[] arr = new int[100]; Arrays.fill(arr,100);//数组的批量赋值。 for(int i:arr) System.out.print(i+" "); } }

    if判断

    浮点数在计算机中常常无法精确表示,并且计算可能出现误差,因此,判断浮点数相等用==判断不靠谱 正确的方法是利用差值小于某个临界值来判断:

    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"); } } } x is 0.1

    要判断引用类型的变量内容是否相等,必须使用equals()方法

    public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "HELLO".toLowerCase(); System.out.println(s1); System.out.println(s2); if (s1.equals(s2)) { System.out.println("s1 equals s2"); } else { System.out.println("s1 not equals s2"); } } }

    switch多重选择

    switch语句根据switch (表达式)计算的结果,跳转到匹配的case结果,然后继续执行后续语句,直到遇到break结束执行

    使用switch时,注意case语句并没有花括号{},而且,case语句具有“穿透性”,漏写break将导致意想不到的结果:

    public class Main { public static void main(String[] args) { int option = 2; switch (option) { case 1: System.out.println("Selected 1"); case 2: System.out.println("Selected 2"); case 3: System.out.println("Selected 3"); default: System.out.println("Not selected"); } } } Selected 2 Selected 3 Not selected

    使用switch时,如果遗漏了break,就会造成严重的逻辑错误,而且不易在源代码中发现错误。从Java 12开始,switch语句升级为更简洁的表达式语法,使用类似模式匹配(Pattern Matching)的方法,保证只有一种路径会被执行,并且不需要break语句。

    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"); } } }

    练习

    请帮小明同学设计一个程序,输入上次考试成绩(int)和本次考试成绩(int),然后输出成绩提高的百分比,保留两位小数位(例如,21.75%)。

    package com.itranswarp.learnjava; import java.util.Scanner; /** * 输入上次考试成绩(int)和本次考试成绩(int),然后输出成绩提高的百分比,保留两位小数位(例如,21.75%) */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // 创建Scanner对象 System.out.print("Input your last score: "); // 打印提示 int last = scanner.nextInt(); System.out.print("Input your now score: "); // 打印提示 int now = scanner.nextInt(); double pre = ((double)(now - last)/last)*100; System.out.printf("%.2f%%", pre); // 格式化输出 } } Input your last score: 80 Input your now score: 90 12.50%
    Processed: 0.009, SQL: 9