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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值