1.1(显示三条消息)
public class Test1_01 {
public static void main(String[] args) {
System.out.println("Welcome to Java");
System.out.println("Welcome to Computer Science");
System.out.println("Programming is fun");
}
}
1.2(显示五条消息)
public class Test1_02 {
public static void main(String[] args) {
int num = 5;
for(int i = 0; i < num; i++) {
System.out.println("Welcome to Java");
}
}
}
1.3(显示图案)
public class Test1_03 {
public static void main(String[] args) {
System.out.println(" J A V V A");
System.out.println(" J A A V V A A");
System.out.println("J J A A V V A A");
System.out.println(" J J A A V A A");
}
}
1.4(打印表格)
public class Test1_04 {
public static void main(String[] args) {
System.out.println("a\ta^2\ta^3");
int num = 4;
for(int i = 1; i <= num; i++) {
System.out.println(i + "\t" + (i * i) + "\t" + (i * i * i));
}
}
}
1.5(计算表达式)
public class Test1_05 {
public static void main(String[] args) {
double num = (8.5 * 4.5 - 2.5 * 3) / (45.5 - 3.5);
System.out.println(num);
}
}
1.6(数列求和)
public class Test1_06 {
public static void main(String[] args) {
int num_start = 1;
int num_end = 9;
int sum = 0;
for(int i = num_start; i <= num_end; i++) {
sum += i;
}
System.out.println(sum);
}
}
1.7(近似求p)
public class Test1_07 {
public static void main(String[] args) {
int num_end1 = 11;
int num_end2 = 13;
System.out.println(computer(num_end1));
System.out.println(computer(num_end2));
}
public static double computer(int num_end) {
double sum = 0;
for(int i = 1, n = 0; i <= num_end; i += 2, n++) {
sum += 1.0/(i * Math.pow(-1, n));
}
double number = 4 * sum;
return number;
}
}
1.8(圆的面积和周长)
public class Test1_08 {
private static final double PI = 3.1415926;
public static void main(String[] args) {
double radius = 5.5;
System.out.println("area is " + getArea(radius));
System.out.println("perimeter is " + getPerimeter(radius));
}
public static double getArea(double radius) {
return radius * radius * PI;
}
public static double getPerimeter(double radius) {
return 2 * radius * PI;
}
}
1.9(矩形的面积和周长 )
public class Test1_09 {
public static void main(String[] args) {
double height = 7.9;
double width = 4.5;
System.out.println("area is " + getArea(height, width));
System.out.println("perimeter is " + getPerimeter(height, width));
}
public static double getArea(double height, double width) {
return height * width;
}
public static double getPerimeter(double height, double width) {
return 2 * (height + width);
}
}
1.10(以英里计的平均速度)
public class Test1_10 {
public static void main(String[] args) {
double kil = 14;
double mil = 1.6 * kil;
int time = 45 * 60 + 30;
double hour = time / 3600.0;
System.out.println(mil/hour + "英里/小时");
}
}
1.11(人口估算)
public class Test1_11 {
public static void main(String[] args) {
int num = 312032486;
for(int i = 1; i <= 5; i++) {
num = fun(i, num);
System.out.println("第" + i + "年的人口数是: " + num);
}
}
public static int fun(int i, int num) {
int time = 3600 * 24 * 365 * i;
double birth = time / 7.0;
double die = time / 13.0;
double in = time / 45.0;
int change = (int) (birth + in - die);
num += change;
return num;
}
}
1.12(以公里计的平均速度)
public class Test1_12 {
public static void main(String[] args) {
double mil = 24;
double kil = mil / 1.6;
int time = 1 * 3600 + 40 * 60 + 35;
double hour = time / 3600.0;
System.out.println(kil/hour + "公里/小时");
}
}
1.13(代数,求解2*2线性方程)
public class Test1_13 {
public static void main(String[] args) {
double a = 3.4, b = 50.2, e = 44.5, c = 2.1, d = 0.55, f = 5.9;
double x = (e * d - b * f) / (a * d - b * c);
double y = (a * f - e * c) / (a * d - b * c);
System.out.println("x = " + x + "\ty=" + y);
}
}
以上代码均为个人编写,不当之处,悉请指正。