2018/12/05 PAT刷题 L1-015 跟奥巴马一起画方块 Java

题目是简单的, 就是有一个问题要注意, 在第8行的地方,  double h = (double)n / 2; , 不能改写成 double h = n / 2; , 如果写成第二个代码的话, 双精度浮点数变量的小数部分一定是.0, 因为整型n / 2 的结果一定是一个整型数. 代码如下:

 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3 
 4 public class Main {
 5 
 6     public static void main(String[] args) throws Exception {
 7         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 8         String[] str = br.readLine().split(" ");
 9         int n = Integer.parseInt(str[0]);
10         if (3 <= n && n <= 21) {
11             double h = (double)n / 2;
12             int x = (int) (n / 2);
13             double y = h - x;
14             // System.out.print(y);
15             if (y >= 0.5) {
16                 h++;
17             }
18             // System.out.println((int)h);
19             for (int i = 0; i < (int) h; i++) {
20                 for (int j = 0; j < n; j++) {
21                     System.out.print(str[1]);
22                 }
23                 System.out.println();
24             }
25         }
26 
27     }
28 
29 }
原文地址:https://www.cnblogs.com/huangZ-H/p/10068901.html