绝对值与编程画对称图形
目录
绝对值的概念与特性
pass
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。Java实现求绝对值用到的特性
参考:https://www.jianshu.com/p/6cc0f22cb34c
画对称图形模型
code:
public class Abs {
public static void main(String[] args) {
int row = 2;
for (int i = 0; i < row; i++) {
//定好原点
int xy0 = 'A' + i;
for (int j = 0; j < 2 * row + 1; j++) {
// 关键地方, 通过绝对值,能使数字围绕原点形成一个对称
int printChar = 'A' + Math.abs(row - j); // 1 0 1
// 2 1 0 1 2
//如果 [逻辑控制字母] 大于 [原点],则:
if (printChar > xy0) {
System.out.print(" ");
} else {
System.out.print(((char) printChar) + " ");
}
}
System.out.println();
}
}
}
输出:
A
B A B

更多精彩