Java语言程序设计(基础篇)(原书第10版) 第一章编程练习题
目录
心血来潮补一下之前没做完的课后题,争取全部做完,欢迎大家评论指正。
需要书籍或者相关资料可以私聊!!!
1-1 显示三条消息
public class Program1_1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Welcome to Java!");
System.out.println("Welcome to Computer Science!");
System.out.println("Programming is fun!");
}
}
1-2 显示五条消息
public class Program1_2 {
public static void main(String[] args){
for(int i = 1; i <= 5; i++)
System.out.println("Welcome to Java");
}
}
1-3 显示图案
public class Program1_3 {
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 AAAAA V V AAAAA");
System.out.println(" J J A A V A A");
}
}
1-4 打印表格
public class Program1_4 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("a a^2 a^3");
System.out.println("1 1 1");
System.out.println("2 4 8");
System.out.println("3 9 27");
System.out.println("4 16 64");
}
}
1-5 计算表达式
public class Program1_5 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("(9.5*4.5-2.5*3)/(45.5-3.5) = "
+ (9.5*4.5-2.5*3)/(45.5-3.5));
}
}
1-6 数列求和
public class Program1_6 {
public static void main(String[] args) {
int sum = 0;
for (int i = 1; i < 10; i++)
sum += i;
System.out.println(sum);
}
}
1-7 近似求PI
public class Program1_7 {
public static void main(String[] args) {
double m = 0, t = 0, f = 1;
for (int i = 1; i <= 13; i += 2) {
t += 1.0 * f / i;
if(i == 11) m = t;
f *= -1;
}
System.out.println(m*4);
System.out.println(t*4);
}
}
1-8 圆的面积和周长
public class Program1_8 {
public static void main(String[] args) {
// TODO Auto-generated method stub
float pi = (float) 3.14159265;
float c = (float) (2*5.5*pi);
float s = (float) (5.5*5.5*pi);
System.out.println("半径为5.5的圆的周长为" + c);
System.out.println("半径为5.5的圆的面积为" + s);
}
}
1-9 矩形的面积和周长
public class Program1_9 {
public static void main(String[] args) {
double a = 4.5, b = 7.9;
System.out.printf("宽为%.1f, 高为%.1f的矩形的周长为%.2f\n", a, b, (a+b)*2);
System.out.printf("宽为%.1f, 高为%.1f的矩形的面积为%.2f\n", a, b, a*b);
}
}
1-10 以英里计的平均速度
public class Program1_10 {
public static void main(String[] args) {
double s = 14 / 1.6;
int second = 45 * 60 + 30;
double h = second / 3600.0;
System.out.println(s / h);
}
}
1-11 人口估算
public class Program1_11 {
public static void main(String[] args) {
// TODO Auto-generated method stub
double add = 365*24*60*60/7.0 - 365*24*60*60/13.0 + 365*24*60*60/45.0;
System.out.printf("first : %.0f\n", (312032486 + add));
System.out.printf("second : %.0f\n", (312032486 + add*2));
System.out.printf("third : %.0f\n", (312032486 + add*3));
System.out.printf("forth : %.0f\n", (312032486 + add*4));
System.out.printf("fifth : %.0f\n", (312032486 + add*5));
}
}
1-12 以公里计的平均速度
public class Program1_12 {
public static void main(String[] args) {
// TODO Auto-generated method stub
float v;
v = (float) ((24*1.6)/(1+40/60.0+35/3600.0));
System.out.println("速度为 :" + v);
}
}
1-13 代数:求解2*2线性方程
public class Program1_13 {
public static void main(String[] args) {
double a = 3.4, b = 50.2, c = 2.1, d = 0.55, e = 44.5, f = 5.9;
double x1 = e*d - b*f, y1 = a*f - e*c, m = a*d - b*c;
System.out.println("x = " + x1/m);
System.out.println("y = " + y1/m);
}
}