2月21号总结

今天复习了一下java的表达式,输入与输出

整数的加减乘除四则运算:

public class Main {
    public static void main(String[] args) {
        int a = 6 + 3 * 4 / 2 - 2;

        System.out.println(a);

        int b = a * 10 + 5 / 2;

        System.out.println(b);

        System.out.println(23 * 56 - 78 / 3);
    }
}

浮点数(小数)的运算:

public class Main {
    public static void main(String[] args) {
        double x = 1.5, y = 3.2;

        System.out.println(x * y);
        System.out.println(x + y);
        System.out.println(x - y);
        System.out.println(x / y);
    }
}

输入

import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String str = sc.next();  // 读入下一个字符串
        int x = sc.nextInt();  // 读入下一个整数
        float y = sc.nextFloat();  // 读入下一个单精度浮点数
        double z = sc.nextDouble();  // 读入下一个双精度浮点数
        String line = sc.nextLine();  // 读入下一行
    }
}

输出

public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(123);  // 输出整数 + 换行
        System.out.println("Hello World");  // 输出字符串 + 换行
        System.out.print(123);  // 输出整数
        System.out.print("yxc\n");  // 输出字符串
        System.out.printf("%04d %.2f\n", 4, 123.456D);  // 格式化输出,float与double都用%f输出
    }
}

System.out.printf()中不同类型变量的输出格式:

(1) int:%d
(2) float: %f, 默认保留6位小数
(3) double: %f, 默认保留6位小数
(4) char: %c, 回车也是一个字符,用'\n'表示
(5) String: %s

posted @ 2023-02-21 20:23  lcz111  阅读(16)  评论(0)    收藏  举报