This commit is contained in:
2025-09-21 21:55:05 +08:00
parent 4264c76aa8
commit 9a58125862
22 changed files with 376 additions and 99 deletions

View File

@@ -0,0 +1,17 @@
package smy.javastudy;
public class GrammarPractice {
public void Exchange(String input){
switch (input){
case "刘道熙":
System.out.println("是畜生");
break;
case "唐伟":
System.out.println("是大帅比");
break;
default:
System.out.println("不知道");
break;
}
}
}

View File

@@ -0,0 +1,74 @@
package smy.javastudy;
public class JavaBasicDataType {
public int integerType = 100; //整数类型
public float floatType = 3.14f; //单精度浮点类型
public double doubleType = 3.141592653589793; //双精度浮点类型
public char charType = 'A'; //字符类型
public boolean booleanType = true; //布尔类型
public String stringType = "Hello, Java!"; //字符串类型
public long longType = 123456789L; //长整型
public short shortType = 32000; //短整型
public byte byteType = 100; //字节型
public void displayDataTypes() {
System.out.println("==================================================");
System.out.println("整数类型 (int): " + integerType);
System.out.println("单精度浮点类型 (float): " + floatType);
System.out.println("双精度浮点类型 (double): " + doubleType);
System.out.println("字符类型 (char): " + charType);
System.out.println("布尔类型 (boolean): " + booleanType);
System.out.println("字符串类型 (String): " + stringType);
System.out.println("长整型 (long): " + longType);
System.out.println("短整型 (short): " + shortType);
System.out.println("字节型 (byte): " + byteType);
System.out.println("==================================================");
}
public void ShowDataTypesInfo() {
System.out.println("==================================================");
System.out.println("基本类型byte 二进制位数:"+Byte.SIZE);
System.out.println("包装类java.lang.Byte");
System.out.println("最小值Byte.MIN_VALUE="+Byte.MIN_VALUE);
System.out.println("最大值Byte.MAX_VALUE="+Byte.MAX_VALUE);
System.out.println();
System.out.println("基本类型short 二进制位数:"+Short.SIZE);
System.out.println("包装类java.lang.Short");
System.out.println("最小值Short.MIN_VALUE="+Short.MIN_VALUE);
System.out.println("最大值Short.Max_Value="+Short.MAX_VALUE);
System.out.println();
System.out.println("基本类型int 二进制位数:"+Integer.SIZE);
System.out.println("包装类java.lang.Integer");
System.out.println("最小值Integer.MIN_VALUE="+Integer.MIN_VALUE);
System.out.println("最大值Integer.Max_Value="+Integer.MAX_VALUE);
System.out.println();
System.out.println("基本类型long 二进制位数:"+Long.SIZE);
System.out.println("包装类java.lang.Long");
System.out.println("最小值Long.MIN_VALUE="+Long.MIN_VALUE);
System.out.println("最大值Long.Max_Value="+Long.MAX_VALUE);
System.out.println();
System.out.println("基本类型float 二进制位数:"+Float.SIZE);
System.out.println("包装类java.lang.Float");
System.out.println("最小值Float.MIN_VALUE="+Float.MIN_VALUE);
System.out.println("最大值Float.Max_Value="+Float.MAX_VALUE);
System.out.println();
System.out.println("基本类型double 二进制位数:"+Double.SIZE);
System.out.println("包装类java.lang.Double");
System.out.println("最小值Double.MIN_VALUE="+Double.MIN_VALUE);
System.out.println("最大值Double.Max_Value="+Double.MAX_VALUE);
System.out.println();
System.out.println("基本类型char 二进制位数:"+Character.SIZE);
System.out.println("包装类java.lang.Character");
System.out.println("最小值Character.MIN_VALUE="+(int)Character.MIN_VALUE);
System.out.println("最大值Character.Max_Value="+(int)Character.MAX_VALUE);
System.out.println("==================================================");
}
}

View File

@@ -0,0 +1,43 @@
package smy.javastudy;
public class Main {
final double PI = 3.14159;
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
//项目唯一主函数
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();
JavaBasicDataType dataType = new JavaBasicDataType();
dataType.ShowDataTypesInfo();
GrammarPractice grammarPractice = new GrammarPractice();
grammarPractice.Exchange("刘道熙");
grammarPractice.Exchange("唐伟");
grammarPractice.Exchange("树萌芽");
NumberAndMathDemo demo = new NumberAndMathDemo();
demo.numberClassUsage();
demo.mathClassUsage();
}
public void testHuHangTao(){
SWPUStudent HuHangTao = new SWPUStudent("胡航滔", 20, "2024520541", "网络工程", 3.8, "hht@stu.swpu.edu.cn", "12345678901", "四川省广安市", "");
HuHangTao.ShowInfo();
HuHangTao.setGpa(4.5);
HuHangTao.setEmail(HuHangTao.getStudentID() + "@stu.swpu.edu.cn");
HuHangTao.setPhoneNumber("18782345678");
HuHangTao.ShowInfo();
}
}

View File

@@ -0,0 +1,89 @@
package smy.javastudy;
import java.math.BigDecimal;
import java.math.BigInteger;
public class NumberAndMathDemo {
public void numberClassUsage() {
System.out.println("=========================Number类的用法=========================");
// Number 是一个抽象类,其子类包括 Integer、Double、Float 等。
// 我们可以用它来持有任何数字类型。
Number num1 = 123;
Number num2 = 456.78;
System.out.println("num1 是: " + num1 + ",类型是 " + num1.getClass().getName());
System.out.println("num2 是: " + num2 + ",类型是 " + num2.getClass().getName());
// xxxValue() 方法用于转换为基本数据类型
System.out.println("num1 作为 int: " + num1.intValue());
System.out.println("num1 作为 double: " + num1.doubleValue());
System.out.println("num2 作为 int: " + num2.intValue()); // 注意:这会截断小数部分
System.out.println("num2 作为 long: " + num2.longValue());
System.out.println("num2 作为 float: " + num2.floatValue());
System.out.println("num2 作为 byte: " + num2.byteValue());
System.out.println("num2 作为 short: " + num2.shortValue());
// 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));
// 使用 BigInteger 和 BigDecimal 处理非常大的数字
BigInteger bigInt = new BigInteger("12345678901234567890");
BigDecimal bigDecimal = new BigDecimal("1234567890.0987654321");
System.out.println("BigInteger: " + bigInt);
System.out.println("BigDecimal: " + bigDecimal);
// 大数的算术运算
BigInteger bigIntSum = bigInt.add(new BigInteger("1"));
System.out.println("BigInteger 的和: " + bigIntSum);
BigDecimal bigDecimalProduct = bigDecimal.multiply(new BigDecimal("2.5"));
System.out.println("BigDecimal 的积: " + bigDecimalProduct);
System.out.println("=========================Number类的用法=========================");
}
public void mathClassUsage() {
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));
System.out.println("100 的以10为底的对数是: " + Math.log10(100));
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));
// 随机数生成
// Math.random() 返回一个大于等于 0.0 且小于 1.0 的 double 值
System.out.println("一个随机数: " + Math.random());
// 获取一个范围内的随机整数,例如 [1, 10]
int randomInt = (int) (Math.random() * 10) + 1;
System.out.println("一个 1 到 10 之间的随机整数: " + randomInt);
System.out.println("\n=========================Math类的用法=========================");
}
}

View File

@@ -0,0 +1,137 @@
package smy.javastudy;
public class SWPUStudent {
private String name; //学生姓名
private int age; //学生年龄
private String studentID; //学号
private String major; //专业
private double gpa; //绩点
private String email; //电子邮件
private String phoneNumber; //电话号码
private String address; //住址
private String gender; //性别
//学生类构造函数
public SWPUStudent(String name, int age, String studentID, String major, double gpa, String email, String phoneNumber, String address, String gender) {
this.name = name;
this.age = age;
this.studentID = studentID;
this.major = major;
this.gpa = gpa;
this.email = email;
this.phoneNumber = phoneNumber;
this.address = address;
this.gender = gender;
}
//展示学生基本信息
public void ShowInfo() {
System.out.println("==================================");
System.out.println("西南石油大学学生信息:");
System.out.println("姓名: " + name);
System.out.println("年龄: " + age);
System.out.println("学号: " + studentID);
System.out.println("专业: " + major);
System.out.println("绩点: " + gpa);
System.out.println("电子邮件: " + email);
System.out.println("电话号码: " + phoneNumber);
System.out.println("住址: " + address);
System.out.println("性别: " + gender);
System.out.println("==================================");
}
//======================对学生属性进行封装==================================
//姓名
public String getName() {
return name;
}
public String setName(String name) {
return this.name = name;
}
//年龄
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//学生ID
public String getStudentID() {
return studentID;
}
public void setStudentID(String studentID) {
this.studentID = studentID;
}
//专业
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
//学分绩点
public double getGpa() {
return gpa;
}
public void setGpa(double gpa) {
this.gpa = gpa;
}
//邮箱
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
//电话号码
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
//家庭住址
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
//年龄
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
//======================对学生属性进行封装==================================
}

View File

@@ -0,0 +1,27 @@
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
}