多组输入,一个整数(3~20),表示输出的行数,也表示组成三角形边的“*”的数量。
针对每行输入,输出用“*”组成的“空心”三角形,每个“*”后面有一个空格。
4
* * * * * * * * *
5
* * * * * * * * * * * *
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int n = in.nextInt();
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
if (i == 0 || i == n - 1 || j == 0 || j == i) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
in.close();
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int n = in.nextInt();
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
if(j==1||i==n||j==i){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
} import java.util.Scanner;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while(scan.hasNextInt()){
int num = scan.nextInt();
for (int line = 1; line < num; line++){
for(int colu = 1; colu <= line; colu++){
if(line == colu || colu == 1){
System.out.print("* ");
}else if(line > 2){
System.out.print(" ");
}
}
System.out.println();
}
//单独输出最后一行
for(int i = 0; i < num; i++){
System.out.print("* ");
}
System.out.println();
}
}
} import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()){
int x=sc.nextInt();
for(int i=1;i<=x;i++){
for(int j=1;j<=i;j++){
if(i==1||i==2||i==x){
System.out.printf("* ");
}else{
if(j==1||j==i) System.out.printf("* ");
else System.out.printf(" ");
}
}
System.out.println();
}
}
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
//第一二行
System.out.println("* ");
System.out.println("* * ");
//中间行
for (int j = 1; j <= n - 3; j++) {
System.out.print("* ");
for (int i = 0; i < j; i++) {
System.out.print(" ");
}
System.out.println("* ");
}
//最后一行
for (int i = 0; i < n; i++) {
System.out.print("* ");
}
System.out.println();
}
}
}
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while(scanner.hasNext()){
int n = scanner.nextInt();
for(int i = 0 ; i < n ;i++){
for(int j = 0;j <= i ;j++){
if(j > 0 && j < i && i < (n - 1)){
System.out.print(" ");
}else{
System.out.print("* ");
}
}
System.out.println("");
}
}
}
} import java.util.Scanner;
//实际是输出一个正方形,只是根据不同条件输出不同的字符
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int a = sc.nextInt();
for(int i=1;i<=a;i++) {
for(int j=1;j<=a;j++) {
if(i==a||j==1||j==i) {
System.out.print("* ");
}
else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
import java.util.Scanner;
/**
* @Title: 空心三角形图案
* @Remark: KiKi学习了循环,BoBo老师给他出了一系列打印图案的练习,该任务是打印用“*”组成的“空心”三角形图案。
* 输入描述:
* 多组输入,一个整数(3~20),表示输出的行数,也表示组成三角形边的“*”的数量。
* 输出描述:
* 针对每行输入,输出用“*”组成的“空心”三角形,每个“*”后面有一个空格。
* @Author: ijunfu
* @Version: 1.0.0
* @Date: 2022-03-20
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNextLine()) {
int count = Integer.parseInt(in.nextLine());
for(int i=0; i<count; i++) {
for(int j=0; j<count; j++) {
if(j == 0 || j == i || i == count - 1) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(j==0||j==i||i==n-1) System.out.print("* ");
else System.out.print(" ");
}
System.out.println();
}
}
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
while(in.hasNextInt()){
int n = in.nextInt();
int m = 2*n;
for(int i = 0; i < n; i++) {
char[] ch = new char[m];
for(int j = 0; j < m; j++){
ch[j] = ' ';
}
ch[0] = '*';
ch[2*i] = '*';
if(i == n-1) {
for(int j = 0; j < m; j++){
if(j%2 == 0){
ch[j] = '*';
}
}
}
System.out.println(String.valueOf(ch));
}
}
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
int n=sc.nextInt();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(i==j||j==0||i==(n-1)){
System.out.print("* ");
}else{
System.out.print(" ");
}
}
System.out.println(" ");
}
}
}
} import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main
{
public static void main(String args[])throws IOException
{
BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
String str;
while((str=bf.readLine())!=null)
{
StringBuilder sb=new StringBuilder();
int n=Integer.parseInt(str);
if(n<3||n>20)
{
System.out.println("error input");
continue;
}
else
{
for(int i=0;i<n;i++)
{
for(int j=i;j>=0;j--)
{
if(i>1&&i!=n-1)
{
if(j>0&&j<i)
sb.append(" ");
else
sb.append("* ");
}
else
sb.append("* ");
}
for(int k=i+1;k<n;k++)
sb.append(" ");
sb.append("\n");
}
System.out.print(sb);
sb.setLength(0);
}
}
}
} import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int count;
boolean flag;
while(scanner.hasNext()) {
count = scanner.nextInt();
for(int i=0;i<count;i++) {
for(int j=0;j<=count*2;j++) {
//条件是精髓
flag = i==count-1?j%2==0:j==0||(j%2==0&&j/2==i);
if(flag) {
System.out.printf("*");
}else {
System.out.printf(" ");
}
}
System.out.println();
}
}
}
} package exer; import java.util.Collections; import java.util.Scanner; public class test3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); for (int i=1;i<=n;i++){ if (i==1||i==2||i==n){ String str = String.join("", Collections.nCopies(i,"* ")); System.out.print(str); }else{ for (int j=1;j<=n;j++){ if (j==i||j==1){ System.out.print("* "); }else { System.out.print(" "); } } } System.out.println(); } } }}通过不了 显示一样
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
while(input.hasNext()){
int a = input.nextInt();
for(int i=1; i<=a; i++){
if(i==1 || i==2 || i==a){
for(int j=1; j<=i; j++){
System.out.print("* ");
}
for(int k=i+1;k<=a;k++) {
System.out.print(" ");
}
System.out.println();
}else{
System.out.print("* ");
for(int j=3; j<=i; j++){
System.out.print(" ");
}
System.out.print("* ");
for(int k=i+1; k<=a; k++){
System.out.print(" ");
}
System.out.println();
}
}
}
}
}
后面的空格一定不能忘记