今天去面试了写一个程序输出到三角,题目说只能用for循环,我用了while循环,面试关说要用for循环。
不会,请问怎么实现?
*
今天去面试了写一个程序输出到三角,题目说只能用for循环,我用了while循环,面试关说要用for循环。
不会,请问怎么实现?
*
我晕怎么这个问题出现了几次呀?
我上次也回答过:
[code="java"]static void print(int m) {
int max = 2*m-1;//正方形边长。
int len = max * max;//正方形中的格子数
int row = 1 ;//行数
int left = 0;//左边起始位置的前一个位置。
int right = max;//右边终止位置
int count = 0;//第row行的星号个数
int total = max* row;
for (int i = 1; i <= len; i++) {
if(left < i && i <= right ){
System.out.print("*");
} else {
System.out.print(" ");
}
if( i == total) {
System.out.println();
++row;
count = (max - ((m+1 -row)*2-1))/2;
total = max* row;
left = total - max +count;// max * (row-1)+ count;
right = total-count;
}
}
}
[/code]
这儿:[url]http://www.iteye.com/problems/80038#solutions[/url]