
正文
java金字塔代码结构 java编程数字金字塔
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
如何用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");}}}
相关问答
Q1: 求镂空的倒金字塔java代码....金字塔可用*代替! 谢了
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import javax.swing.JFrame;
import javax.swing.JPanel;
// 点阵法:
// 首先java金字塔代码结构,java金字塔代码结构我们假设金字塔是等边三角形,等边三角形与矩形java金字塔代码结构的关系是:
// 1, 底边是矩形的宽度
// 2, 高是举行的长度
// 3, 顶点是矩形的底边中点
// *******
// |* *|
// | * * |
// ---*---
// 这样,如果java金字塔代码结构我们知道矩形的长和宽,我们就能指导等边三角形在矩形每一行中的点位于什么位置java金字塔代码结构了:
// 若长(height)为4,宽(width)为7.
// 计算等边三角形在某一行x的两点位置为:
// 若x == 1, 则整行都是三角形的边
// 若x == 4, 则只有中点是三角形的边。中点 = width / 2 + (width % 2 0 ? 1 : 0)
// 若 x 1 x 4,则左点 = width / (2 * height) * x; 右点 = width - width / (2 * height) * x + 1;
// 知道了三角形的点,我们就能画出金字塔了。
// Java2D
// 首先,找到三角形的三个点,在两两相连即可。
// 示例如下:
public class Question1 {
// 矩形
class Rec{
int height;
int width;
public Rec(int height, int width){
this.height = height;
this.width = width;
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
}
// 以字符串打印形式绘制[点阵法]
public Question1(int height, int width, char shape) {
height = height = 0 ? 3 : height; // 对参数进行验证整理
width = width = 0 ? 3 : width; // 对参数进行验证整理
// 每行有多少点?
int rowPoints = width;
// 不要忘记每行最后还有换行符
rowPoints ++;
// 总共有多少点?
int totalPoints = rowPoints * height;
char[] allChar = new char[totalPoints]; // 所有行的字符
// 特殊处理第一行
for(int i = 0; i rowPoints; i++){
if(i rowPoints - 1) // rowpoints位是换行符,所以这里需要特殊处理。
allChar[i] = shape;
else
allChar[i] = '\n';
}
// 处理从第二行到倒数第二行
for(int i = 2; i height; i++){
//左点 = width / (2 * height) * x; 右点 = width - width / (2 * height) * x + 1;
// 但是这里得牢记,运算符的运算顺序,所以必须这样写:
int leftpoint = (width * i) / (2 * height);
int rightpoint= width - (width * i) / (2 * height) + 1;
for(int j = 0; j rowPoints; j++){
int index = (i - 1) * rowPoints + j; // 这里对数组index的计算很重要,千万别算错了。
if( j rowPoints - 1){
if(j + 1 == leftpoint || j + 1 == rightpoint) // 列序号从0开始,但点位是从1开始的,所以给列序号+1
allChar[index] = shape;
else
allChar[index] = ' ';
}else{
allChar[index] = '\n';
}
}
}
//特殊处理最后一行
int point = width / 2 + (width % 2 0 ? 1 : 0);
int startIndex = (height - 1) * rowPoints;
for(int i = 0; i rowPoints; i++){
if( i rowPoints - 1){
if( i + 1 == point)
allChar[startIndex + i] = shape;
else
allChar[startIndex + i] = ' ';
}else
allChar[allChar.length - 1] = '\n';
}
String result = new String(allChar);
System.out.print(result);
}
class PanelContainer extends JPanel{
private Point point1;
private Point point2;
private Point point3;
public PanelContainer(Point point1, Point point2, Point point3){
this.point1 = point1;
this.point2 = point2;
this.point3 = point3;
setBackground(Color.white);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if(point1 != null point2 != null point3 != null){
g.setColor(Color.red);
Graphics2D g2d = (Graphics2D) g;
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point2.getX(), (int)point2.getY());
g2d.drawLine((int)point2.getX(), (int)point2.getY(), (int)point3.getX(), (int)point3.getY());
g2d.drawLine((int)point1.getX(), (int)point1.getY(), (int)point3.getX(), (int)point3.getY());
}
}
}
//Java2D
public Question1(int height, int width) {
JFrame frame = new JFrame("Java2D 三角形");
frame.setBounds(50, 50, 400, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Point point1 = new Point(50, 50);// 让图形举例边界50像素
Point point2 = new Point(width + 50, 50);
Point point3 = new Point(width/2 + 50, height + 50);
PanelContainer container = new PanelContainer(point1, point2, point3);
frame.getContentPane().add(container);
frame.setVisible(true);
}
public static void main(String args[]){
new Question1(200, 300, '*');
new Question1(200, 300);
}
}
Q2: java金字塔代码有些不明白请大神进来一下下
i-j是没有条件的,只是用i-j的值来作为判断的标准。
以第1行为例,i等于5,j等于1,time等于0,此时time不等于i-j,所以这个时候要打印空格,并且time自增1,进入下一轮循环。直到打印了4个空格之后,time等于4,此时
time!=i-j条件为假,跳出循环,执行下一步。
Q3: 在Java中实现数字金字塔
你好,代码如下,与你图中的一模一样:
public class Pyramid {
/*
* param h 金字塔高
* 返回 金字塔矩阵
*/
public static String[][] getPyramid(int h){
//根据图像可以观察只
//每一个数据都是有*和数字组成 而且规律是:
//如果数子长度为1,那么前面有两个*
//如果数子长度为2,那么前面有一个*
//根据高度来计算数组的宽度
int w = 1 + (h-1)*2 ;
//
String[][] array = new String[h][w] ;
for(int i = 0;ih;i++){
for(int j=0;jw;j++){
if(jh-i-1){
array[i][j] = "***";
}else if(jh){
if((h-j)9){
array[i][j] = "*" + (h-j) ;
}else{
array[i][j] = "**" + (h-j) ;
}
}else if(jh+i){
if((h-j)9){
array[i][j] = "*" + (j-h+2) ;
}else{
array[i][j] = "**" + (j-h+2) ;
}
}else{
array[i][j] = "";
}
}
}
return array ;
}
public static void print(String[][] array){
for(int i=0;iarray.length;i++){
for(int j=0;jarray[i].length;j++){
System.out.print(array[i][j]) ;
}
System.out.println();
}
}
public static void main(String[] args) {
print(getPyramid(12)) ;
}
}
Q4: 怎么用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();
}
}
}
Q5: 用java把1-100这100个数字按顺序排成金字塔形状,尽量用最简单的代码实现
你好,除了直接用多个System.out.println(xxx);直接打印出三角形外,我的代码最简单,而且给足了注释,可以直接运行
public class t {
public static void main(String[] args) {
int no=1;//要打印的数
for(int i=1;no=100;i++){//第1行1个数,第n行n个数,结束条件是打印的数=100
for(int j=1;j=i;j++){
System.out.print(no+++"\t");
}
System.out.println();//转行
}
}
}
结果:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91
92 93 94 95 96 97 98 99 100 101 102 103 104 105
关于java金字塔代码结构和java编程数字金字塔的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







