本文目录一览:
如何用JAVA 编写一个递归程序输出如下数字金字塔
这是我刚才编写的用于输出金字塔的一个类。完整的代码。//输出金字塔importjava.util.Scanner;publicclassa1{publicstaticvoidmain(String[]args){Scannera=newScanner(System.in);intN=5;//定义行数的变量booleanb=true;do{try{System.out.println("请输入整数类型的数字:");N=a.nextInt();//获取输入行数b=false;}catch(Exceptionea){a=newScanner(System.in);//N=a.nextInt();//获取输入行数}}while(b);inti,j,m;for(i=0;iN;i++)//输出金字塔{for(m=0;mN-1-i;m++){System.out.printf("");}for(j=0;j2*i+1;j++){System.out.printf("*");}System.out.printf("\n");}}}
怎么用java编写金字塔?
public class King
{
public static void main(String argc[]) {
int t;
java.util.Scanner san = new java.util.Scanner(System.in);
System.out.print("请输入行数: ");
t = san.nextInt();
for (int i = 1; i = t; i++) {
for (int f = 1; f = (t - i); f++)
System.out.print(" ");
for (int ff = 1; ff = (2 * i - 1); ff++)
System.out.print("*");
System.out.println();
}
}
}
求助java金字塔杨辉三角(不知道我的这个哪里错了)
import java.util.Scanner;
public class nihao
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while(true)
{
System.out.println("请输入杨辉三角的行数:");
int n = -1;
try
{
n = sc.nextInt();
}
catch(Exception e)
{
sc.close();
System.err.println("已退出");
break;
}
int a[][] = new int[n][];
for(int i = 0; i n; i++)
{
for(int m = 1; m n - i; m++)
{
System.out.print(" ");
}
a[i] = new int[i + 1];
for(int j = 0; j = i; j++)
{
if(j == 0 || j == i)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i - 1][j] + a[i - 1][j - 1];
}
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
}
JAVA以金字塔形式输出 1 2 3 4 5 6
int count = 1;
int x = 3;
for(int i=x;i0;i--){
for (int j = 1; j i; j++) {
System.out.print(" ");
}
for (int K = 0; K = x-i; K++) {
System.out.print(count);
System.out.print(" ");
count ++;
}
System.out.println("");
}