如何在Java程序中用*输出一个菱形的问题! 1.这样的如下图: * * * ** * * * * * * * *2.这样的如下图:* * * * * * * * * * * * * * *谢谢了..! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 1.这样的如下图: * * * ** * * * * * * * *public void printAngle(int n) //n=3{ for(int i=1;i<n+1;i++) { for(int j=i;j<n+1;j++) System.out.print(" "); for(int k=1;k<i+1;k++) System.out.println("*");//System.out.print(k); for(int m=i-1;m>0;m--) System.out.println("*");//System.out.print(m); System.out.println(); } for(int i=n-1;i>0;i--) { for(int m=1;m<n-i+2;m++) System.out.print(" "); for(int k=1;k<i+1;k++) System.out.println("*");//System.out.print(k); for(int j=i-1;j>0;j--) System.out.println("*");//System.out.print(j); System.out.println(); }}2.这样的如下图:* * * * * * * * * * * * * * *public void printRecAngel(int n) //n=5{ for(int i=1;i<n+1;i++) { for(int j=1;j<i;j++) System.out.print(" "); for(int j=i;j<n+1;j++) System.out.print("*");//System.out.print(j); System.out.println(); }} class Print{public static void main(String arg[]){printRecAngel();printRecAngel();}public void printRecAngel() { System.out.println(" *" ); System.out.println(" * * *"); System.out.println(" * * * * *"); System.out.println(" * * *"); System.out.println(" *"); }public void printRecAngel(){ System.out.println("* * * * *"); System.out.println(" * * * *"); System.out.println(" * * *"); System.out.println(" * *"); System.out.println(" *");}} 1.这样的如下图: * * * ** * * * * * * * *public class A { public static void main(String[] args) { for(int i=1;i<=3;i++) { for(int j=1;j<=6-2*i;j++) System.out.print(" "); for(int j=1;j<=2*i-1;j++) System.out.print("* "); System.out.println(); //打印上三角 } for(int i=1;i<=2;i++) { for(int j=1;j<=2*i;j++) System.out.print(" "); for(int j=1;j<=5-2*i;j++) System.out.print("* "); System.out.println(); } //打印下三角 }}* * * * * * * * * * * * * * *public class C{ public static void main(String[] args) { for(int i=1;i<=5;i++) //打印行数 { for(int j=1;j<=2*i-2;j++) System.out.print(" "); //打印每行空格 for(int j=1;j<=6-i;j++) System.out.print("* "); //打印每行星号 System.out.println(); } }} 第二个得:public class Csdn { private static int I=0;//控制“*'的个数 public static void main(String[] args) { display(4); } public static void display(int n){ //采用递归 if(n>0){ print(I,n); I++; display(n-1); } } private static void print(int kong,int xing){ for(;kong>0;kong--)//打印空格 System.out.print(" "); for(;xing >0;xing--)//打印“*” System.out.print("*"); System.out.println(); } } 1./** * 打印菱形 * @author AZERO */public class PrintDiamond { /** * 打印菱形 * @author 汤波 * @param n */ public static void print(int n){ if(n > 0 && n % 2 != 0){ /*打印上半部*/ for(int i = 0;i < n / 2 + 1; i++){ for(int j = 0; j < n; j++){ if(j >= (n - 1) / 2 - i && j <= (n - 1) / 2 + i){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } /*打印下半部*/ for(int i = 1;i <= (n - 1) / 2; i++){ for(int j = 0;j < n; j++){ if(j >= (n - 1) / 2 - n / 2 + i && j <= (n - 1) / 2 + n / 2 - i){ System.out.print("*"); }else{ System.out.print(" "); } } System.out.println(); } }else{ System.out.println("请输入奇数!"); } } /** * @param args */ public static void main(String[] args) { PrintDiamond.print(25); }}2.太简单不说了 JAVA 爱好者加群 38236097JAVA 爱好者加群 38236097 正则表达式小问题 一个基础问题。 java指定查询数据库表内容并用一个二维数组a来显示出现的问题 求助,打印三角形问题 Timer中得任务出了异常怎么捕获? 高分求教--为什么用getMouseListers()不能得到MouseLister的对象啊? 如何把byte数组转成InputStream? 各位大哥,急!!!!帮忙,在线等待 什么时候定义静态类 HELP!!! 关于java进程!解决就+100(急) I/O知识,大家进来看下吧! for循环跳出
1.这样的如下图:
*
* * *
* * * * *
* * *
*public void printAngle(int n) //n=3
{
for(int i=1;i<n+1;i++)
{
for(int j=i;j<n+1;j++)
System.out.print(" ");
for(int k=1;k<i+1;k++)
System.out.println("*");//System.out.print(k);
for(int m=i-1;m>0;m--)
System.out.println("*");//System.out.print(m);
System.out.println();
}
for(int i=n-1;i>0;i--)
{
for(int m=1;m<n-i+2;m++)
System.out.print(" ");
for(int k=1;k<i+1;k++)
System.out.println("*");//System.out.print(k);
for(int j=i-1;j>0;j--)
System.out.println("*");//System.out.print(j);
System.out.println();
}
}2.这样的如下图:
* * * * *
* * * *
* * *
* *
*
public void printRecAngel(int n) //n=5
{
for(int i=1;i<n+1;i++)
{
for(int j=1;j<i;j++)
System.out.print(" ");
for(int j=i;j<n+1;j++)
System.out.print("*");//System.out.print(j);
System.out.println();
}
}
public static void main(String arg[]){
printRecAngel();
printRecAngel();
}
public void printRecAngel()
{
System.out.println(" *" );
System.out.println(" * * *");
System.out.println(" * * * * *");
System.out.println(" * * *");
System.out.println(" *");
}
public void printRecAngel(){
System.out.println("* * * * *");
System.out.println(" * * * *");
System.out.println(" * * *");
System.out.println(" * *");
System.out.println(" *");}
}
*
* * *
* * * * *
* * *
*
public class A
{
public static void main(String[] args)
{
for(int i=1;i<=3;i++)
{
for(int j=1;j<=6-2*i;j++)
System.out.print(" "); for(int j=1;j<=2*i-1;j++)
System.out.print("* ");
System.out.println(); //打印上三角
}
for(int i=1;i<=2;i++)
{
for(int j=1;j<=2*i;j++)
System.out.print(" "); for(int j=1;j<=5-2*i;j++)
System.out.print("* "); System.out.println();
} //打印下三角
}
}* * * * *
* * * *
* * *
* *
*
public class C{
public static void main(String[] args)
{
for(int i=1;i<=5;i++) //打印行数
{
for(int j=1;j<=2*i-2;j++)
System.out.print(" "); //打印每行空格
for(int j=1;j<=6-i;j++)
System.out.print("* "); //打印每行星号 System.out.println();
}
}
}
public class Csdn {
private static int I=0;//控制“*'的个数 public static void main(String[] args) {
display(4);
}
public static void display(int n){
//采用递归
if(n>0){
print(I,n);
I++;
display(n-1);
}
}
private static void print(int kong,int xing){
for(;kong>0;kong--)//打印空格
System.out.print(" ");
for(;xing >0;xing--)//打印“*”
System.out.print("*");
System.out.println();
}
}
/**
* 打印菱形
* @author AZERO
*/
public class PrintDiamond { /**
* 打印菱形
* @author 汤波
* @param n
*/
public static void print(int n){
if(n > 0 && n % 2 != 0){
/*打印上半部*/
for(int i = 0;i < n / 2 + 1; i++){
for(int j = 0; j < n; j++){
if(j >= (n - 1) / 2 - i && j <= (n - 1) / 2 + i){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
/*打印下半部*/
for(int i = 1;i <= (n - 1) / 2; i++){
for(int j = 0;j < n; j++){
if(j >= (n - 1) / 2 - n / 2 + i && j <= (n - 1) / 2 + n / 2 - i){
System.out.print("*");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}else{
System.out.println("请输入奇数!");
}
}
/**
* @param args
*/
public static void main(String[] args) {
PrintDiamond.print(25);
}}2.太简单不说了
JAVA 爱好者加群 38236097