本文目錄一覽:
如何用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(“”);
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/288504.html