RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
金字塔用java代码 用java打出金字塔

怎么用java编写金字塔?

public class King

创新互联公司是专业的环江网站建设公司,环江接单;提供成都网站制作、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行环江网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

{

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 T {

public static void main(String[] args) {

System.out.println("请输入一个大写字母:");

Scanner s = new Scanner(System.in);

String input = s.next();

if (input.length() != 1 || input.charAt(0)  65 || input.charAt(0)  90) {

System.out.println("输入的不是一个大写字母。");

}

int c = input.charAt(0) - 65;

for (int i = 0; i = c; i++) {

for (int j = 25; j  i; j--) {

System.out.print(" ");

}

for (int j = 0; j = i; j++) {

System.out.print((char) (j + 65));

}

for (int j = i - 1; j= 0; j--) {

System.out.print((char) (j + 65));

}

System.out.print("\n");

}

}

}

求镂空的倒金字塔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;

// 点阵法:

// 首先,我们假设金字塔是等边三角形,等边三角形与矩形的关系是:

// 1, 底边是矩形的宽度

// 2, 高是举行的长度

// 3, 顶点是矩形的底边中点

// *******

// |* *|

// | * * |

// ---*---

// 这样,如果我们知道矩形的长和宽,我们就能指导等边三角形在矩形每一行中的点位于什么位置了:

// 若长(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);

}

}

如何用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代码 用java打出金字塔
标题网址:http://scyingshan.cn/article/doieojp.html