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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人