5 changed files with 102 additions and 0 deletions
@ -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(); |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
1. 为什么需要显式数据类型声明? |
|||
因为Java是编译型语言,需要提前知道数据类型来分配内存和检查类型错误。 |
|||
编译时必须确定每个变量占用多少内存(如int占4字节,double占8字节) |
|||
编译时检查类型是否匹配,防止运行时才崩溃(如不能把字符串当数字计算) |
|||
|
|||
2.为什么需要public这类访问修饰符? |
|||
因为Java需要明确控制谁能访问什么,保证程序安全性和结构清晰。 |
|||
|
|||
public:任何人都能访问(如main方法必须public,因为JVM要从外部调用) |
|||
private:只有自己能用(隐藏内部细节,防止乱改数据) |
|||
不写(默认):同一包内能用 |
|||
protected:子类能用 |
|||
@ -0,0 +1,2 @@ |
|||
python就像自波车 只有油门和刹车 简单易操作 但是遇到紧急情况容易把油门当刹车踩 并且更耗油 |
|||
java就像手波车 除了油门刹车 还有一个离合器 操作较繁琐 但是遇到危险可以直接踩下离合器切断汽车动力 比较安全 并且手动控制档位 更省油 |
|||
@ -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(); |
|||
} |
|||
} |
|||
|
After Width: | Height: | Size: 39 KiB |
Loading…
Reference in new issue