黑马程序员技术交流社区
标题:
打印图形代码的问题
[打印本页]
作者:
王睿
时间:
2012-3-9 00:32
标题:
打印图形代码的问题
public class demo {
public static void main(String[] args) {
int num = 23;
int height = num/4+1;
int width = num;
int[][] arr = new int[height][width];
int x = height-1;
int y = 0;
boolean count = true;
for(int i=1; i<=width; i++){
arr[x][y] = i;
y++;
if(count){
x--;
}else{
x++;
}
if(x<0){
x=x+2;
count = false;
}
if(x>height-1){
x=x-2;
count = true;
}
}
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
if(arr[i][j]==0){
System.out.print(" ");
}else{
System.out.print(arr[i][j]);
}
}
System.out.println();
}
}
}
复制代码
打印出的结果有错位,怎么调整呢?
作者:
吴超
时间:
2012-3-9 00:56
输出语句问题
public class Demo {
public static void main(String[] args) {
int num = 23;
int height = num/4+1;
int width = num;
int[][] arr = new int[height][width];
int x = height-1;
int y = 0;
boolean count = true;
for(int i=1; i<=width; i++){
arr[x][y] = i;
y++;
if(count){
x--;
}else{
x++;
}
if(x<0){
x=x+2;
count = false;
}
if(x>height-1){
x=x-2;
count = true;
}
}
for(int i=0; i<arr.length; i++){
for(int j=0; j<arr[i].length; j++){
if(arr[i][j]==0){
System.out.print(" ");//输出语句上的问题
}else{
System.out.print(arr[i][j]);
}
}
System.out.println();
}
}
}
复制代码
1222222222224.jpg
(7.64 KB, 下载次数: 48)
下载附件
2012-3-9 00:55 上传
作者:
王睿
时间:
2012-3-9 13:31
谢谢哦!
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2