Browse Source

上传文件至 'W2周梓浩202506050319'

main
zhouzihao 1 month ago
parent
commit
f416bf0f60
  1. 33
      W2周梓浩202506050319/Student.java.txt
  2. 12
      W2周梓浩202506050319/回答问题.txt
  3. 2
      W2周梓浩202506050319/心得.txt
  4. 55
      W2周梓浩202506050319/轉換后.txt
  5. BIN
      W2周梓浩202506050319/运行截图.png

33
W2周梓浩202506050319/Student.java.txt

@ -0,0 +1,33 @@
class Student{
String studentId;
String name;
double score;
//define a study approach
void study() {
System.out.println(studentId+" "+name+"正在学习,上一次的成绩是"+score);
}
}
public class Main {
public static void main (String[] args) {
//setting up the first object
Student s1=new Student();
s1.studentId="001";
s1.name="George";
s1.score=90;
//setting up the second object
Student s2=new Student();
s2.studentId="002";
s2.name="Roya";
s2.score=95;
//using the approach study()
s1.study();
s2.study();
}
}

12
W2周梓浩202506050319/回答问题.txt

@ -0,0 +1,12 @@
1. 为什么需要显式数据类型声明?
因为Java是编译型语言,需要提前知道数据类型来分配内存和检查类型错误。
编译时必须确定每个变量占用多少内存(如int占4字节,double占8字节)
编译时检查类型是否匹配,防止运行时才崩溃(如不能把字符串当数字计算)
2.为什么需要public这类访问修饰符?
因为Java需要明确控制谁能访问什么,保证程序安全性和结构清晰。
public:任何人都能访问(如main方法必须public,因为JVM要从外部调用)
private:只有自己能用(隐藏内部细节,防止乱改数据)
不写(默认):同一包内能用
protected:子类能用

2
W2周梓浩202506050319/心得.txt

@ -0,0 +1,2 @@
python就像自波车 只有油门和刹车 简单易操作 但是遇到紧急情况容易把油门当刹车踩 并且更耗油
java就像手波车 除了油门刹车 还有一个离合器 操作较繁琐 但是遇到危险可以直接踩下离合器切断汽车动力 比较安全 并且手动控制档位 更省油

55
W2周梓浩202506050319/轉換后.txt

@ -0,0 +1,55 @@
import java.util.Scanner;
public class TemperatureConverter {
// 将摄氏度转换为华氏度
public static double celsiusToFahrenheit(double c) {
return c * 9.0 / 5.0 + 32.0;
}
// 将华氏度转换为摄氏度
public static double fahrenheitToCelsius(double f) {
return (f - 32.0) * 5.0 / 9.0;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入要转换的温度与单位(例如 36.6 C 或 97 F):");
String input = scanner.nextLine().trim();
if (input.isEmpty()) {
System.out.println("输入为空,程序退出。");
scanner.close();
return;
}
String[] parts = input.split(" ");
double value;
String unit;
try {
value = Double.parseDouble(parts[0]);
if (parts.length > 1) {
unit = parts[1].toUpperCase();
} else {
unit = "C";
}
} catch (Exception e) {
System.out.println("输入解析失败,请按示例输入数值与单位,例如:36.6 C");
scanner.close();
return;
}
if (unit.startsWith("C")) {
double f = celsiusToFahrenheit(value);
System.out.printf("%.2f °C = %.2f °F%n", value, f);
} else if (unit.startsWith("F")) {
double c = fahrenheitToCelsius(value);
System.out.printf("%.2f °F = %.2f °C%n", value, c);
} else {
System.out.println("未知单位,请使用 C 或 F。");
}
scanner.close();
}
}

BIN
W2周梓浩202506050319/运行截图.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Loading…
Cancel
Save