https://www.cnblogs.com/AugustLight/p/-/Jvav-note
起手式
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.close();
}
}
读入
令 sc 为一个 Scanner。(Scanner sc = new Scanner(System.in);)
| 需求 |
代码 |
| 读入一个整数 |
sc.nextInt() |
| 读入一行字符 |
sc.nextLine() |
| 读入一个小数 |
sc.nextDouble() |
输出
| 需求 |
代码 |
| 输出一个字符串 |
System.out.print |
| 输出一个字符串并换行 |
System.out.println |
数学
| 需求 |
代码 |
| 最大值 |
Math.max |
| 最小值 |
Math.min |
| \(\gcd\) |
没有,自己写 |
| 四舍五入到最近整数 |
Math.round |
| 平方根 |
Math.sqrt |
| 绝对值 |
Math.abs |
注:Math.round 返回一个 long,如果需要 int 则要强转。
字符串 & 字符
令 s 为一个 String。
| 需求 |
代码 |
s 长度 |
s.length() |
求 s 索引为 i 的字符 |
s.charAt(i) |
| 转成字符数组 |
s.toCharArray() |
求 s 转成大写后的字符串 |
s.toUpperCase() |
求 s 转成小写后的字符串 |
s.toLowerCase() |
| format |
String.format |
t 在 s 中第一次出现的 index(没有时返回 \(-1\)) |
s.indexOf(t) |
注:
- 关于
String.format:例如,求浮点数 ans 保留两位小数的字符串:String.format("%.2f", ans)。
| 需求 |
代码 |
| 判断字符是否是数字 |
Character.isDigit |
| ASCII 转字符 |
强转就行 |
| 字符 转 ASCII |
强转就行 |
随机
| 需求 |
代码 |
| 生成一个 \([0,1)\) 的随机数 |
Math.random() |