9月22日学习

This commit is contained in:
2025-09-22 21:36:32 +08:00
parent 9a58125862
commit c0ff930024
9 changed files with 421 additions and 43 deletions

View File

@@ -1,6 +1,8 @@
package smy.javastudy;
import smy.javastudy.aiwrite.NumberAndMathDemo;
import smy.javastudy.classes.*;
public class Main {
final double PI = 3.14159;
@@ -10,23 +12,64 @@ public class Main {
//项目唯一主函数
public static void main(String[] args) {
//TIP 当文本光标位于高亮显示的文本处时按 <shortcut actionId="ShowIntentionActions"/>
// 查看 IntelliJ IDEA 建议如何修正。
System.out.printf("Hello and welcome!\n");
Main main = new Main();
main.testHuHangTao();
//测试本地方法
if(false){
System.out.printf("Hello and welcome!\n");
Main main = new Main();
main.testHuHangTao();
}
JavaBasicDataType dataType = new JavaBasicDataType();
dataType.ShowDataTypesInfo();
GrammarPractice grammarPractice = new GrammarPractice();
grammarPractice.Exchange("刘道熙");
grammarPractice.Exchange("唐伟");
grammarPractice.Exchange("树萌芽");
//测试基本数据类型
if(false){
JavaBasicDataType dataType = new JavaBasicDataType();
dataType.ShowDataTypesInfo();
}
//测试基本语法
if(false){
GrammarPractice grammarPractice = new GrammarPractice();
grammarPractice.Exchange("刘道熙");
grammarPractice.Exchange("唐伟");
grammarPractice.Exchange("树萌芽");
}
//ai写的Number类和Math类的用法
if(false){
NumberAndMathDemo demo = new NumberAndMathDemo();
demo.numberClassUsage();
demo.mathClassUsage();
}
//测试Number类和Math类用法
if (false){
MathClass mathClass = new MathClass();
mathClass.MathClass();
NumberClass numberClass = new NumberClass();
numberClass.NumberClass();
}
//测试Charactor类用法
if(false){
CharacterClass characterClass = new CharacterClass();
characterClass.CharacterClass();
}
//测试String类用法
if(false){
StringClass stringClass = new StringClass();
stringClass.StringClass();
}
//测试StringBuffer
if (true){
StringBufferClass stringBufferClass = new StringBufferClass();
stringBufferClass.StringBufferClass();
}
NumberAndMathDemo demo = new NumberAndMathDemo();
demo.numberClassUsage();
demo.mathClassUsage();
}
public void testHuHangTao(){

View File

@@ -1,27 +0,0 @@
package smy.javastudy;
import java.lang.Number;
public class TestNumberClass {
//基本类型转换
public void typeConversion(){
Number num1 = 1234.56;
System.out.println(num1);
System.out.println(num1.getClass());
System.out.println(num1.intValue());
System.out.println(num1.longValue());
System.out.println(num1.doubleValue());
System.out.println(num1.floatValue());
System.out.println(num1.byteValue());
}
//数值比较
public void compareNum(){
Integer x = 10;
Double y = 10.0;
System.out.println(x.doubleValue()==y.doubleValue());
}
//处理大数
//public
}

View File

@@ -0,0 +1,24 @@
package smy.javastudy.aiwrite;
public class MathUtils {
// 使用泛型方法支持所有Number子类Integer, Long, Float, Double等
public static <T extends Number, U extends Number> double log(T base, U value) {
// 将泛型参数转换为double进行计算
double baseDouble = base.doubleValue();
double valueDouble = value.doubleValue();
// 参数校验
if (baseDouble <= 0 || baseDouble == 1) {
throw new IllegalArgumentException("底数必须是正数且不等于1");
}
if (valueDouble <= 0) {
throw new IllegalArgumentException("对数值必须是正数");
}
// 计算并返回结果
return Math.log(valueDouble) / Math.log(baseDouble);
}
}

View File

@@ -1,4 +1,4 @@
package smy.javastudy;
package smy.javastudy.aiwrite;
import java.math.BigDecimal;
import java.math.BigInteger;
@@ -84,6 +84,6 @@ public class NumberAndMathDemo {
// 获取一个范围内的随机整数例如 [1, 10]
int randomInt = (int) (Math.random() * 10) + 1;
System.out.println("一个 1 到 10 之间的随机整数: " + randomInt);
System.out.println("\n=========================Math类的用法=========================");
System.out.println("\n=========================Math类的用法=========================\n");
}
}

View File

@@ -0,0 +1,48 @@
package smy.javastudy.classes;
public class CharacterClass {
public void CharacterClass() {
System.out.println("\n=========================Character类的用法=========================");
char ch1 = 'a';
char ch2 = '1';
char ch3 = ' ';
char ch4 = '\n';
char ch5 = '中';
// 判断字符类型
System.out.println(ch1 + " 是字母吗? " + Character.isLetter(ch1));
System.out.println(ch2 + " 是数字吗? " + Character.isDigit(ch2));
System.out.println(ch3 + " 是空白吗? " + Character.isWhitespace(ch3));
System.out.println(ch4 + " 是空白吗? " + Character.isWhitespace(ch4));
System.out.println(ch5 + " 是汉字吗? " + Character.isIdeographic(ch5));
// 字符转换
System.out.println(ch1 + " 转为大写: " + Character.toUpperCase(ch1));
System.out.println(Character.toUpperCase('A') + " 转为小写: " + Character.toLowerCase('A'));
System.out.println("9 转为数字: " + Character.getNumericValue('9'));
System.out.println("A 转为数字: " + Character.getNumericValue('A')); // A对应10
// 获取字符信息
System.out.println("Unicode 码点 of '" + ch5 + "': " + (int) ch5);
System.out.println("字符 '" + ch1 + "' 的类别: " + Character.getType(ch1)); // 0-控制字符1-大写字母2-小写字母9-数字等
// 判断字符是否为特定类型
System.out.println('A' + " 是大写字母吗? " + Character.isUpperCase('A'));
System.out.println('a' + " 是小写字母吗? " + Character.isLowerCase('a'));
// 其他有用的方法
System.out.println("空字符 '\\0' 的 Unicode 码点: " + (int) '\0');
System.out.println("回车符 '\\r' 的 Unicode 码点: " + (int) '\r');
System.out.println("制表符 '\\t' 的 Unicode 码点: " + (int) '\t');
//测试转义字符
System.out.println("/t 效果为: Hello\tWorld!");//制表符
System.out.println("/n 效果为: Hello\nWorld!");//换行符
System.out.println("/r 效果为: Hello\rWorld!");//回车符
System.out.println("/' 效果为: It\'s a nice day!");//单引号
System.out.println("/" + " 效果为: He said, \"" + "Hello!\"");//双引号
System.out.println("/ 效果为: This is a backslash: \\");//反斜杠
System.out.println("访问\"菜鸟教程!\"");
System.out.println("=========================Character类的用法=========================\n");
}
}

View File

@@ -0,0 +1,59 @@
package smy.javastudy.classes;
import smy.javastudy.aiwrite.MathUtils;
//数学类的简单举例
public class MathClass {
public void MathClass() {
System.out.println("\n=========================Math类的用法=========================");
double x = 45.6;
double y = -78.9;
double angle = 45.0;
// 基本算术方法
System.out.println(y + " 的绝对值是: " + Math.abs(y));
System.out.println(x + " 的向上取整是: " + Math.ceil(x)); // 向上取整
System.out.println(x + " 的向下取整是: " + Math.floor(x)); // 向下取整
System.out.println(x + " 的四舍五入是: " + Math.round(x)); // 四舍五入到最近的整数
System.out.println(x + "" + y + " 的最大值是: " + Math.max(x, y));
System.out.println(x + "" + y + " 的最小值是: " + Math.min(x, y));
// 指数和对数函数
System.out.println("e^2 是: " + Math.exp(2));
System.out.println("10 的自然对数是: " + Math.log(10));//ln10
System.out.println("100 的以10为底的对数是: " + Math.log10(100));//lg100
System.out.println("1000 的以2为底的对数是: " + (Math.log(1000)/Math.log(2)));//以2为底的对数可以用换底公式计算
System.out.println("10000 的以5为底的对数是: " + (MathUtils.log(10000,5)));//
System.out.println("2^3 是: " + Math.pow(2, 3));
System.out.println("16 的平方根是: " + Math.sqrt(16));
// 三角函数
double radians = Math.toRadians(angle);
System.out.println(angle + " 度的正弦值是: " + Math.sin(radians));
System.out.println(angle + " 度的余弦值是: " + Math.cos(radians));
System.out.println(angle + " 度的正切值是: " + Math.tan(radians));
System.out.println("" + radians + " 弧度转换为度: " + Math.toDegrees(radians));
System.out.println("1的反正切值: " + Math.atan(1));//反正切值
// 随机数生成
// Math.random() 返回一个大于等于 0.0 且小于 1.0 的 double 值 [0,1)
System.out.println("一个随机数: " + Math.random());
// 获取一个范围内的随机整数,例如 [1, 10]
int randomInt = (int) (Math.random() * 10) + 1;
System.out.println("一个 1 到 10 之间的随机整数: " + randomInt);
// 其他有用的方法
System.out.println("符号函数:"+Math.signum(-23.45));//返回数字的符号,负数返回-1.0正数返回1.0零返回0.0 → -1.0
System.out.println("平方和开根:"+Math.hypot(3,4));//计算sqrt(3²+5²) → 5.0
System.out.println("返回余数"+Math.IEEEremainder(10,3));//返回10除以3的余数结果可能为负数 → 1.0
System.out.println("指数运算:"+Math.getExponent(256.0));//返回256的指数部分 → 8
System.out.println("2的3次方:"+Math.scalb(2,3));//返回2乘以2的3次方 → 16.0
System.out.println("转换成字符串: " + Math.random());//返回"java.lang.Math"
System.out.println("字串符解析成整数数值:"+Integer.parseInt("123123123"));//返回123123123
System.out.println("字串符解析成浮点数值:"+Double.parseDouble("123.123123"));//返回123.123123;
System.out.println("=========================Math类的用法=========================\n");
}
}

View File

@@ -0,0 +1,65 @@
package smy.javastudy.classes;
import java.lang.Number;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.NumberFormat;
public class NumberClass {
//基本类型转换
public void NumberClass(){
System.out.println("\n=========================Number类的用法=========================");
Number num1 = 1234.56;
System.out.println(num1);
System.out.println(num1.getClass());
System.out.println(num1.intValue());
System.out.println(num1.longValue());
System.out.println(num1.doubleValue());
System.out.println(num1.floatValue());
System.out.println(num1.byteValue());
//数值比较
Integer x = 10;
Double y = 10.0;
System.out.println(x.doubleValue()==y.doubleValue());
//大数用法
BigInteger bigInt1 = new BigInteger("123122222222222222222222231231231231231");
BigInteger bigInt2 = new BigInteger("9876543210987654321098765432109876543210");
BigInteger resInt1 = bigInt1.add(bigInt2); //相加 注意BigInteger 和 BigDecimal 的加减乘除不能用 + - * / 这些运算符
BigInteger resInt2 = bigInt1.multiply(bigInt2);//相乘
BigInteger resInt3 = bigInt1.subtract(bigInt2);//相减
BigInteger resInt4 = bigInt1.divide(bigInt2);//相除
System.out.println("大整数相加"+resInt1);
System.out.println("大整数相乘"+resInt2);
System.out.println("大整数想减"+resInt3);
System.out.println("大整数相除"+resInt4);
BigDecimal bigDec1 = new BigDecimal("1293123.123123123123123123123");
BigDecimal bigDec2 = new BigDecimal("98765.4321098765432109876543210");
BigDecimal resDec1 = bigDec1.add(bigDec2);
BigDecimal resDec2 = bigDec1.multiply(bigDec2);
BigDecimal resDec3 = bigDec1.subtract(bigDec2);
BigDecimal resDec4 = bigDec1.divide(bigDec2, 10, BigDecimal.ROUND_HALF_UP); // 指定小数位数和舍入模式,避免异常
System.out.println("大浮点数相加"+resDec1);
System.out.println("大浮点数相乘"+resDec2);
System.out.println("大浮点数相减"+resDec3);
System.out.println("大浮点数相除"+resDec4);
// compareTo() 方法 (来自 Comparable 接口, 由 Number 的子类实现)
Integer int1 = 100;
Integer int2 = 200;
System.out.println("比较 100 和 200: " + int1.compareTo(int2)); // 如果小于则为-1如果等于则为0如果大于则为1
// equals() 方法
Double double1 = 123.45;
Double double2 = 123.45;
System.out.println("用 equals() 比较 123.45 和 123.45: " + double1.equals(double2));
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3); // 设置小数点后最多3位
System.out.println("格式化后:"+nf.format(131321311.1231312));
System.out.println("=========================Number类的用法=========================");
}
}

View File

@@ -0,0 +1,49 @@
package smy.javastudy.classes;
public class StringBufferClass {
public void StringBufferClass() {
System.out.println("\n=====================StringBufferClass==========================");
// 初始对象,后续操作不会修改此对象
StringBuffer original = new StringBuffer("Hello World");
System.out.println("初始字符串: " + original);
// 1. 追加操作 - 基于原对象创建副本后操作
StringBuffer appended = new StringBuffer(original); // 复制原对象
appended.append(" appended text.");
System.out.println("追加字符串: " + appended);
// 2. 插入操作 - 基于原对象创建副本后操作
StringBuffer inserted = new StringBuffer(original); // 复制原对象
inserted.insert(0, "Start-");
System.out.println("插入字符串: " + inserted);
// 3. 替换操作 - 基于原对象创建副本后操作
StringBuffer replaced = new StringBuffer(original); // 复制原对象
replaced.replace(1, 3, "replaced");
System.out.println("替换字符串: " + replaced);
// 4. 反转操作 - 基于原对象创建副本后操作
StringBuffer reversed = new StringBuffer(original); // 复制原对象
reversed.reverse();
System.out.println("反转字符串: " + reversed);
// 5. 删除操作 - 基于原对象创建副本后操作
StringBuffer deleted = new StringBuffer(original); // 复制原对象
deleted.delete(0, deleted.length() - 1);
System.out.println("删除字符串: " + deleted);
// 6. 获取长度
int length = original.length();
System.out.println("原始字符串长度: " + length);
//获取容量
int capacity = original.capacity();
System.out.println("原始字符串容量: " + capacity);
System.out.println("=====================StringBufferClass==========================\n");
}
}

View File

@@ -0,0 +1,117 @@
package smy.javastudy.classes;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class StringClass {
//注意:String 类是不可改变的,所以你一旦创建了 String 对象,那它的值就无法改变了(详看笔记部分解析)。
//如果需要对字符串做很多修改,那么应该选择使用 StringBuffer & StringBuilder 类。
public void StringClass() {
System.out.println("\n=========================String类的用法=========================");
// 1. 创建与不可变性immutable
String str = "Hello, World!"; // 字面量(驻留在字符串池)
// 显式创建一个在堆上的新字符串对象(与字面量不同的引用),用于演示引用相等 vs 值相等
String newObj = new StringBuilder(str).toString(); // 新对象(堆),用于演示引用不相等
System.out.println("str == newObj ? " + (str == newObj) + " // intentional: compare references"); // false引用不同
System.out.println("str.equals(newObj) ? " + str.equals(newObj)); // true内容相同
// 2. 基本常用方法
System.out.println("字符串: " + str);
System.out.println("长度: " + str.length());
System.out.println("索引7的字符: " + str.charAt(7));// 索引从0开始 输出w 空格也算
System.out.println("子串(7,12): " + str.substring(7, 12)); // 包含7不包含12
System.out.println("连接: " + str.concat(" Let's learn Java."));
System.out.println("是否包含 'World' : " + str.contains("World"));
System.out.println("第一次出现 'o' 的索引: " + str.indexOf('o')); // 找不到返回 -1
System.out.println("最后出现 'o' 的索引: " + str.lastIndexOf('o'));
System.out.println("替换 (World->Java): " + str.replace("World", "Java"));
System.out.println("大写: " + str.toUpperCase(Locale.ENGLISH));
System.out.println("小写: " + str.toLowerCase(Locale.ENGLISH));
// 3. 去空白与比较
String padded = " Leading and trailing spaces ";
System.out.println("原始: '" + padded + "'");
System.out.println("trim() -> '" + padded.trim() + "'"); // Java 8 起可用
// Java 11+: strip(), isBlank(), repeat() 等更强大,若使用低版本请注释
System.out.println("strip() -> '" + padded.strip() + "'");
String s1 = "Hello";
String s2 = "hello";
System.out.println("equals 忽略大小写: " + s1.equalsIgnoreCase(s2));
System.out.println("compareTo (字典序): " + s1.compareTo(s2)); // 区分大小写
// 4. split / join / format
String str2 = "Java Programming";
System.out.println("分割字符串(空格):");
String[] parts = str2.split(" ");
System.out.println(Arrays.toString(parts));
System.out.println("join 示例: " + String.join(" | ", parts));
int number = 42;
double pi = 3.14159;
String formatted = String.format("Number: %d, Pi: %.2f", number, pi);
System.out.println("格式化: " + formatted);
// 5. 正则与 replaceAll / matches
String mixed = "abc123xyz456";
System.out.println("matches digits only? " + "12345".matches("\\d+")); // true
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(mixed);
System.out.print("正则查找数字: ");
while (m.find()) {
System.out.print(m.group() + " ");
}
System.out.println();
System.out.println("regex replace (digits->#): " + mixed.replaceAll("\\d", "#"));
// 6. split 的 limit 使用与注意事项
String csv = "a,b,c,d";
System.out.println("split limit=2: " + Arrays.toString(csv.split(",", 2))); // [a, b,c,d]
System.out.println("split limit=0 (默认): " + Arrays.toString(csv.split(",")));
// 7. 性能提示:大量拼接用 StringBuilder
StringBuilder sb = new StringBuilder(64);
for (int i = 0; i < 5; i++) {
sb.append(i).append('-');
}
String built = sb.toString();
System.out.println("使用 StringBuilder 拼接: " + built);
// 8. 字符编码与字节转换(注意编码)
String chinese = "你好,世界";
byte[] utf8 = chinese.getBytes(StandardCharsets.UTF_8);
String fromUtf8 = new String(utf8, StandardCharsets.UTF_8);
System.out.println("原始: " + chinese + " | bytes length(UTF-8): " + utf8.length + " | 还原: " + fromUtf8);
// 9. 与基本类型互转
String intStr = "123";
int parsed = Integer.parseInt(intStr);
System.out.println("解析数字: " + parsed + " (类型 int)");
// 演示把基本类型变成字符串的常见方式Integer.toString 或 String.valueOf
System.out.println("Integer.toString(parsed) -> " + Integer.toString(parsed));
// 10. 小心常见错误与边界
// substring 越界会抛出 IndexOutOfBoundsException
// indexOf 返回 -1 要先判断再用
String maybe = "abc";
int idx = maybe.indexOf('x');
System.out.println("indexOf 'x' -> " + idx + " (如果 -1 就表示没找到)");
// 11. 文本块text block示例Java 13+),若你的 JDK 版本低,请注释掉下面部分
// String multiLine = """
// This is a multi-line
// string example.
// It preserves line breaks and spaces.
// """;
// System.out.println("多行文本块示例: " + multiLine);
System.out.println("=========================String类的用法=========================");
}
}