/**
* Returns the absolute value of an {@code int} value.
* If the argument is not negative, the argument is returned.
* If the argument is negative, the negation of the argument is returned.
*
* <p>Note that if the argument is equal to the value of
* {@link Integer#MIN_VALUE}, the most negative representable
* {@code int} value, the result is that same value, which is
* negative.
*
* @param a the argument whose absolute value is to be determined
* @return the absolute value of the argument.
*/
public static int abs(int a) {
return (a < 0) ? -a : a;
}
int number = 6;
System.out.println("原值输出:");
while(number>=-6){
number --;
System.out.print(number+" ");
}
System.out.println("\n绝对值输出:");
number = 6;
while(number>=-6){
number --;
System.out.print(Math.abs(number)+" ");
}
原值输出: 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 绝对值输出: 5 4 3 2 1 0 1 2 3 4 5 6 7
A B A B C B A B C D C B A B C D E D C B A B C D E F E D C B A B C D E F G F E D C B A B C D E F G
//调用
print(5);
/**
* 先降序,再升序 实现
* @param row
*/
private static void print(int row){
for(int i=0;i<2*row+1;i++){
int printChar = 'A' + Math.abs(row-i);
System.out.print(((char)printChar)+" ");
}
}
F E D C B A B C D E F
for(int j=0;j<2*row+1;j++){
//逻辑输出字母。先降序、再升序逻辑输出的字母
int printChar = 'A' + Math.abs(row-j);
//如果 [逻辑控制字母] 大于 [规定输出字母],则:
if(printChar>firstChar){
//输出空格
System.out.print(" ");
}else{
//输出字母
System.out.print(((char)printChar)+" ");
}
}
//完整调用
printWithRow(7);
/**
* 先倒序 再正序 输出 英文大写字母
*
* @param row 行
*/
private static void printWithRow(int row){
for(int i=0;i<row;i++){
//规定输出字母。每行第一个显示出来的字母
int firstChar = 'A' + i;
for(int j=0;j<2*row+1;j++){
//逻辑输出字母。先降序、再升序逻辑输出的字母
int printChar = 'A' + Math.abs(row-j);
//如果 [逻辑控制字母] 大于 [规定输出字母],则:
if(printChar>firstChar){
//输出空格
System.out.print(" ");
}else{
//输出字母
System.out.print(((char)printChar)+" ");
}
}
//输出回车
System.out.println();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有