JAVA打印一个倒立和正立的金字塔,就像影子那样的,原来会,突然想不起来了,谢谢先,详细代码啊
展开全部
方法一:一个星星一个星星的打印
方法二:
public class Diamond {
public static void main(String[] args) {
final int size = 4;
for (int i = 1; i <= size; i++) {
for (int j = i; j < size; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = size - 1; i >= 1; i--) {
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
for (int k = 2 * i - 1; k > 0; k--) {
System.out.print("*");
}
System.out.println();
}
}
}
方法三:
public static void main(String[] args){
Scanner n = new Scanner(System.in);
System.out.println("请输入整数:");
int size = n.nextInt();
char[] a = m(size);
float length = a.length;
int min = Math.round(length / 2) - 1;
int life = min;
int right = min;
for(int j = 0; j < size; j++) {
for(int i = 0; i < a.length; i++) {
if(i >= life && i <= right) {
a[i] = '*';
}
else {
a[i] = ' ';
}
System.out.print(a[i]);
}
System.out.println();
System.out.println();
life--;
right++;
}
life += 2;
right -= 2;
for(int j = 0; j < size - 1; j++) {
for(int i = 0; i < a.length; i++) {
if(i >= life && i <= right) {
a[i] = '*';
}
else {
a[i] = ' ';
}
System.out.print(a[i]);
}
System.out.println();
System.out.println();
life++;
right--;
}
}
public static char[] m(int n) {
int m = 2 * n - 1;
char[] a = new char[m];
return a;
}
}
方法四:
/**
* Prints a diamond of a particular size - see the readme for a full description
*/
public static void printDiamond(int size)
{
String result="";
for(int column=1; column<=size; column++)
{
int a=size-column;
for(int s=0; s<a; s++)
{
result+=" ";
}
int b=2*column-1;
for(int start=0; start<b;start++)
{
result+="*";
}
System.out.println(result);
result="";
}
for(int n=1;n<=size-1;n++)
{
// result+="a";
for(int c=0;c<n;c++)
{
result+=" ";
}
int numberOfStart=(2*size-1)-2*n;
for(int b=numberOfStart;b>0;b--)
{
result+="*";
}
System.out.println(result);
result="";
}
}
方法二:
public class Diamond {
public static void main(String[] args) {
final int size = 4;
for (int i = 1; i <= size; i++) {
for (int j = i; j < size; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
for (int i = size - 1; i >= 1; i--) {
for (int j = 1; j <= size - i; j++) {
System.out.print(" ");
}
for (int k = 2 * i - 1; k > 0; k--) {
System.out.print("*");
}
System.out.println();
}
}
}
方法三:
public static void main(String[] args){
Scanner n = new Scanner(System.in);
System.out.println("请输入整数:");
int size = n.nextInt();
char[] a = m(size);
float length = a.length;
int min = Math.round(length / 2) - 1;
int life = min;
int right = min;
for(int j = 0; j < size; j++) {
for(int i = 0; i < a.length; i++) {
if(i >= life && i <= right) {
a[i] = '*';
}
else {
a[i] = ' ';
}
System.out.print(a[i]);
}
System.out.println();
System.out.println();
life--;
right++;
}
life += 2;
right -= 2;
for(int j = 0; j < size - 1; j++) {
for(int i = 0; i < a.length; i++) {
if(i >= life && i <= right) {
a[i] = '*';
}
else {
a[i] = ' ';
}
System.out.print(a[i]);
}
System.out.println();
System.out.println();
life++;
right--;
}
}
public static char[] m(int n) {
int m = 2 * n - 1;
char[] a = new char[m];
return a;
}
}
方法四:
/**
* Prints a diamond of a particular size - see the readme for a full description
*/
public static void printDiamond(int size)
{
String result="";
for(int column=1; column<=size; column++)
{
int a=size-column;
for(int s=0; s<a; s++)
{
result+=" ";
}
int b=2*column-1;
for(int start=0; start<b;start++)
{
result+="*";
}
System.out.println(result);
result="";
}
for(int n=1;n<=size-1;n++)
{
// result+="a";
for(int c=0;c<n;c++)
{
result+=" ";
}
int numberOfStart=(2*size-1)-2*n;
for(int b=numberOfStart;b>0;b--)
{
result+="*";
}
System.out.println(result);
result="";
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询