第五章第五题(千克与磅之间的互换)(Conversion from kilogram to pound and pound to kilogram)
- 5.5(千克与磅之间的互换)编写一个程序,并排显示下列两个表格。
千克 | 磅 | 磅 | 千克 |
---|---|---|---|
1 | 2.2 | 20 | 9.09 |
3 | 6.6 | 25 | 11.36 |
… | … | … | … |
197 | 433.4 | 510 | 231.82 |
199 | 437.8 | 515 | 234.09 |
- 5.5(Conversion from kilogram to pound and pound to kilogram) Write a program that displays the following two tables side by side.
Kilograms | Pounds | Pounds | Kilograms |
---|---|---|---|
1 | 2.2 | 20 | 9.09 |
3 | 6.6 | 25 | 11.36 |
… | … | … | … |
197 | 433.4 | 510 | 231.82 |
199 | 437.8 | 515 | 234.09 |
- 参考代码:
package chapter05;
public class Code_05 {
public static void main(String[] args) {
System.out.printf("%s%15s%15s%21s\n", "Kilograms", "Pounds", "Pounds", "Kilograms");
int i, j;
for(i = 1, j = 20;i <= 199 && j <= 515;i += 2, j += 5)
{
System.out.printf("%-3d%21.1f%15d%21.2f\n", i, i * 2.2, j, j * 0.4545);
}
}
}
- 结果显示:
Kilograms Pounds Pounds Kilograms
1 2.2 20 9.09
3 6.6 25 11.36
5 11.0 30 13.64
7 15.4 35 15.91
9 19.8 40 18.18
11 24.2 45 20.45
13 28.6 50 22.73
15 33.0 55 25.00
17 37.4 60 27.27
19 41.8 65 29.54
21 46.2 70 31.82
23 50.6 75 34.09
25 55.0 80 36.36
27 59.4 85 38.63
29 63.8 90 40.91
31 68.2 95 43.18
33 72.6 100 45.45
35 77.0 105 47.72
37 81.4 110 50.00
39 85.8 115 52.27
41 90.2 120 54.54
43 94.6 125 56.81
45 99.0 130 59.09
47 103.4 135 61.36
49 107.8 140 63.63
51 112.2 145 65.90
53 116.6 150 68.18
55 121.0 155 70.45
57 125.4 160 72.72
59 129.8 165 74.99
61 134.2 170 77.27
63 138.6 175 79.54
65 143.0 180 81.81
67 147.4 185 84.08
69 151.8 190 86.36
71 156.2 195 88.63
73 160.6 200 90.90
75 165.0 205 93.17
77 169.4 210 95.45
79 173.8 215 97.72
81 178.2 220 99.99
83 182.6 225 102.26
85 187.0 230 104.54
87 191.4 235 106.81
89 195.8 240 109.08
91 200.2 245 111.35
93 204.6 250 113.63
95 209.0 255 115.90
97 213.4 260 118.17
99 217.8 265 120.44
101 222.2 270 122.72
103 226.6 275 124.99
105 231.0 280 127.26
107 235.4 285 129.53
109 239.8 290 131.81
111 244.2 295 134.08
113 248.6 300 136.35
115 253.0 305 138.62
117 257.4 310 140.90
119 261.8 315 143.17
121 266.2 320 145.44
123 270.6 325 147.71
125 275.0 330 149.99
127 279.4 335 152.26
129 283.8 340 154.53
131 288.2 345 156.80
133 292.6 350 159.08
135 297.0 355 161.35
137 301.4 360 163.62
139 305.8 365 165.89
141 310.2 370 168.17
143 314.6 375 170.44
145 319.0 380 172.71
147 323.4 385 174.98
149 327.8 390 177.26
151 332.2 395 179.53
153 336.6 400 181.80
155 341.0 405 184.07
157 345.4 410 186.35
159 349.8 415 188.62
161 354.2 420 190.89
163 358.6 425 193.16
165 363.0 430 195.44
167 367.4 435 197.71
169 371.8 440 199.98
171 376.2 445 202.25
173 380.6 450 204.53
175 385.0 455 206.80
177 389.4 460 209.07
179 393.8 465 211.34
181 398.2 470 213.62
183 402.6 475 215.89
185 407.0 480 218.16
187 411.4 485 220.43
189 415.8 490 222.71
191 420.2 495 224.98
193 424.6 500 227.25
195 429.0 505 229.52
197 433.4 510 231.80
199 437.8 515 234.07
Process finished with exit code 0