Java语言程序设计(第3版) 课后练习
第1单元
1.1
public class PersonalInfo {
public static void main(String[] args) {
String name = "张三";
int age = 30;
System.out.println("姓名: " + name + ", 年龄: " + age);
}
}
1.2
public class Sum {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i <= 10; i++) {
sum += i;
}
System.out.println("1到10的和为: " + sum);
}
}
1.3
public class Circle {
public static void main(String[] args) {
double radius = 5.5;
double pi = 3.14159;
double area = pi * radius * radius;
double circumference = 2 * pi * radius;
System.out.println("半径为: " + radius + " 的圆面积为: " + area);
System.out.println("半径为: " + radius + " 的圆周长为: " + circumference);
}
}
1.4
public class Pattern {
public static void main(String[] args) {
for (int i = 1; i <= 4; i++) {
for (int j = 0; j < i; j++) {
System.out.print("* ");
}
System.out.println();
}
}
}