下面的程序是菱形打印的【终结者】,改变下设置,就基本可以满足所有菱形碧核打印的题目。程序主体 for 语句只裤滚有9行,简单而强大 */#include <stdio.h>#include <stdlib.h>#include <string.h>// 这个小函数用来输出 repeatTimes 个字符
#includevoid main(){printf(" *\n")printf(" ***\n")printf(" *****\n")printf("胡慧余 ***\n")printf(" *\n")} 这是最简单的。下面一个稍微复杂一点。#includevoid main(){int i=0,j=0,k=0for(i=0i{for(j=6-ij>=0j--){putchar(' ')...
手机党输入#include"stdio.h"main(){printf(" *\n")printf(" * *\n")printf(" * *\n")printf(" * *\n")printf(" *\n")}
java打印菱形方法:
package circulationDemo
import java.util.Scanner
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in)
System.out.println("请输入要打印的行数:")
int num = input.nextInt()
t1(num)// 正直角
split()
t2(num)// 倒直角
split()
t3(num)// 等腰三角
split()
t4(num)// 等腰倒三角++
split()
t5(num)// 等腰倒三角--
split()
t6(num)// 实心菱形
split()
t7(num)// 空心菱形(1)
split()
t8(num)// 空心菱形(2)
split()
t9(num)// 空心菱形(3)
}
private static void t9(int num) {
for (int k = 1 k <= num k++) {
for (int i = 1 i <= (num - k) i++) {
System.out.print(" ")
}
System.out.print("*")
for (int i = 1 i <= ((k - 2) * 2 + 1) i++) {
System.out.print(" ")
}
if (k != 1) {
System.out.print("*")
}
System.out.println()
}
for (int k = num-1 k >= 1 k--) {
for (int i = 1 i 迅首携<= (num - k) i++) {
System.out.print(" ")
}
System.out.print("*")
for (int i = 1 i <= ((k - 2) * 芹中2 + 1) i++) {
System.out.print(" ")
}
if (k != 1) {
System.out.print("*")
}
System.out.println()
}
}
private static void t8(int num) {
// 上半部分
for (int i = 1 i <= num i++) {
// 打印上半部分空格
for (int j = 1 j <= num - i j++) {
System.out.print(" ")
}
// 打印原本实心的*部分
for (int j = 1 j <= 2 * i - 1 j++) {
// 仅在一行的开头和末尾打印*
if (j == 1 || j == 2 * i - 1) {
System.out.print("*")
} else {
System.out.print(" ")
}
}
System.out.println()
}
// 下半部分
for (int i = 1 i <= num - 1 i++) {
for (int j = 1 j <= i j++) {
System.out.print(" ")
}
for (int j = 1 j <= 2 * (num - i - 1) + 1 j++) {
if (j == 1 || j == 2 * (num - i - 1) + 1) {
System.out.print("*")
} else {
System.out.print(" ")
}
}
System.out.println()
}
}
private static void t7(int num) {
// 打印上半部分
for (int i = 1 i <= num i++) {
// 打印空格
for (int j = 1 j <= (num - i) j++) {
System.out.print(" ")
}
// 打印*的个数
if (i == 1) {
System.out.print("*")
} else {
System.out.print("*")
// 打印菱形里的空格
for (int j = 1 j <= (2 * i - 3) j++) {
System.out.print(" ")
}
System.out.print("*")
}
System.out.println()
}
// 打印下半部分
for (int i = num - 1 i > 0 i--) {
// 打印空格
for (int j = 1 j <= (num - i) j++) {
System.out.print(" ")
}
// 打印*的个数
if (i == 1) {
System.out.print("*")
} else {
System.out.print("*")
// 打亩伏印菱形里的空格
for (int j = 1 j <= (2 * i - 3) j++) {
System.out.print(" ")
}
System.out.print("*")
}
System.out.println()
}
}
private static void t6(int num) {
// 上半部分
for (int i = 1 i <= num i++) {
for (int j = 1 j <= num - i j++) {
System.out.print(" ")
}
for (int j = 1 j <= 2 * i - 1 j++) {
System.out.print("*")
}
System.out.println()
}
// 下半部分
for (int i = 1 i <= num - 1 i++) {
for (int j = 1 j <= i j++) {
System.out.print(" ")
}
for (int j = 1 j <= 2 * (num - 1 - i) + 1 j++) {
System.out.print("*")
}
System.out.println()
}
}
private static void t5(int num) {
for (int i = num i > 0 i--) {
for (int j = 1 j <= num - i j++) {
System.out.print(" ")
}
for (int j = 1 j <= 2 * i - 1 j++) {
System.out.print("*")
}
System.out.println()
}
回答于 2022-12-14欢迎分享,转载请注明来源:内存溢出
评论列表(0条)