java中用for循环怎样打印三角行啊,主要是不理解什么情况外层循环递增什么时候递减,如等腰三角形
java中用for循环怎样打印三角行啊,主要是不理解什么情况外层循环递增什么时候递减,如等腰三角形外层循环递增,而倒直角三角形外层循环递减,原理是什么啊,求解啊?...
java中用for循环怎样打印三角行啊,主要是不理解什么情况外层循环递增什么时候递减,如等腰三角形外层循环递增,而倒直角三角形外层循环递减,原理是什么啊,求解啊?
展开
2个回答
展开全部
public class Test1
{
public static void main(String[] params)
{
String str = sanjiao(11);
System.out.println(str);
String str2 = sijiao(11);
System.out.println(str2);
}
public static String sijiao(int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= num; i++)
{
if (i == 1 || i == num)
{
String str = numStr("*", num);
sb.append(str + "\n");
}
else if (i % 2 == 0)
{
}
else if (i % 2 == 1)
{
String str = middlePad("*", ' ', num - 2);
sb.append(str + "\n");
}
}
return sb.toString();
}
public static String sanjiao(int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= num; i++)
{
if (i == num)
{
String str = numStr("*", num);
sb.append(str + "\n");
}
else if (i == 1)
{
int j = (num - i)/2;
String str = leftPad("*", ' ', j);
str = rightPad(str, ' ', j);
sb.append(str + "\n");
}
else if (i % 2 == 0)
{
}
else if (i % 2 == 1)
{
int j = (num - i)/2;
String str = middlePad("*", ' ', i - 2);
str = leftPad(str, ' ', j);
str = rightPad(str, ' ', j);
sb.append(str + "\n");
}
}
return sb.toString();
}
public static String numStr(String str, int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++)
{
sb.append(str);
}
return sb.toString();
}
public static String leftPad(String str, char c, int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++)
{
sb.append(c);
}
sb.append(str);
return sb.toString();
}
public static String rightPad(String str, char c, int num)
{
StringBuffer sb = new StringBuffer();
sb.append(str);
for (int i = 0; i < num; i++)
{
sb.append(c);
}
return sb.toString();
}
public static String middlePad(String str, char c, int num)
{
StringBuffer sb = new StringBuffer();
sb.append(str);
for (int i = 0; i < num; i++)
{
sb.append(c);
}
sb.append(str);
return sb.toString();
}
}
{
public static void main(String[] params)
{
String str = sanjiao(11);
System.out.println(str);
String str2 = sijiao(11);
System.out.println(str2);
}
public static String sijiao(int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= num; i++)
{
if (i == 1 || i == num)
{
String str = numStr("*", num);
sb.append(str + "\n");
}
else if (i % 2 == 0)
{
}
else if (i % 2 == 1)
{
String str = middlePad("*", ' ', num - 2);
sb.append(str + "\n");
}
}
return sb.toString();
}
public static String sanjiao(int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= num; i++)
{
if (i == num)
{
String str = numStr("*", num);
sb.append(str + "\n");
}
else if (i == 1)
{
int j = (num - i)/2;
String str = leftPad("*", ' ', j);
str = rightPad(str, ' ', j);
sb.append(str + "\n");
}
else if (i % 2 == 0)
{
}
else if (i % 2 == 1)
{
int j = (num - i)/2;
String str = middlePad("*", ' ', i - 2);
str = leftPad(str, ' ', j);
str = rightPad(str, ' ', j);
sb.append(str + "\n");
}
}
return sb.toString();
}
public static String numStr(String str, int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++)
{
sb.append(str);
}
return sb.toString();
}
public static String leftPad(String str, char c, int num)
{
StringBuffer sb = new StringBuffer();
for (int i = 0; i < num; i++)
{
sb.append(c);
}
sb.append(str);
return sb.toString();
}
public static String rightPad(String str, char c, int num)
{
StringBuffer sb = new StringBuffer();
sb.append(str);
for (int i = 0; i < num; i++)
{
sb.append(c);
}
return sb.toString();
}
public static String middlePad(String str, char c, int num)
{
StringBuffer sb = new StringBuffer();
sb.append(str);
for (int i = 0; i < num; i++)
{
sb.append(c);
}
sb.append(str);
return sb.toString();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询