输入一个字符,用它构造一个边长为5的三角形圣诞树。
数据范围:保证输入的字符是一个 char 类型的值
import java.util.Scanner; /** * * @Title 字符圣诞树 * @Description 输入一个字符,用它构造一个边长为5的三角形圣诞树。 * * 数据范围:保证输入的字符是一个 char 类型的值 * 输入描述: * 输入只有一行,一个字符。 * 输出描述: * 该字符构成的三角形圣诞树。 * @author weijunfu<ijunfu @ qq.com> * @date 2022/03/18 00:01 * @version 1.0.0 * */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); for(int i=0; i<5; i++) { // 左侧空格 for(int j=0; j < 4-i; j++) { System.out.print(" "); } // 输出字符 for(int j=0; j<=i; j++) { System.out.print(s + " "); } // 换行 System.out.println(); } in.close(); } }
解题思路:先获取到行数,确定行数后再确定每行左边的空格,然后再确定右边要输入的内容 import java.util.Scanner; public class Main2\ { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String a = scanner.next(); String b = " "; int n = 5; for (int x=1;x<=n;x++){ for (int y=0;y< n-x;y++){ System.out.print(b); } for (int j= 0;j<2*x-1;j++){ if ((j)%2==0){ System.out.print(a); } else { System.out.print(b); } } System.out.println(); } } }
import java.util.Date; import java.util.Iterator; import java.util.Scanner; import java.util.ServiceConfigurationError; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while(sc.hasNext()){ String s = sc.nextLine(); for(int i=0;i<5;i++){ for(int k=0;k<4-i;k++){System.out.printf(" ");} for(int j=0;j<i+1;j++){ System.out.printf(s+" ");} System.out.println(); }} } }
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1这样的数组大家肯定都会打印,就是每一行打印固定的 [ 1 + " " ],那么这样一看其实就给前面打印空格就好了,第一行打印4个,第二行打印三个......
Scanner sc = new Scanner(System.in); String str = sc.next(); int key = 4;//定义空格打印 for (int i = 0; i < 5; i++) { for(int j = key; j > 0; j--){ System.out.print(" "); } //这里是第一步,就是打印字符数组,现在给前面加空格 for (int j = 0; j < i + 1; j++) { System.out.print(str + " "); } key--; System.out.println(); } }//BC12 字符金字塔
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); String n = in.next(); int i = 1; while (i<=5){ for (int j = 4;j>=i;j--){ System.out.print(" "); } for (int k=1;k<=i;k++){ System.out.print(n+" "); } System.out.println(); i++; } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str, result; result = " "; str = input.nextLine(); for (int i = 0; i < 5; i++){ result += str; System.out.println(result.substring(i)); result += " "; } } }
import java.util.*; public class Main{ public static void main(String arg[]){ Scanner input=new Scanner(System.in); String sum=null;int a=5; sum=input.next(); for(int i=1;i<=a;i++){ for(int k=1;k<=a-i;k++){ System.out.print(" "); } for(int x=1;x<=a;){ System.out.print("*"); if(x%2==0){ System.out.print(" "); continue; } x++; } System.out.println(); } } }用Java写的,哪位大哥能给说一下怎么改进吗,没有通过编译。
import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner scnner=new Scanner(System.in); String c=scnner.nextLine(); int n = 5; for(int i = 1; i <= n; i++){ // 控制行数 for (int m = 1; m <= n-i; m++) { // 左侧空格 System.out.printf(" "); } for (int j = 1; j <= 2*i-1; j++) { if ((j+1)%2 == 0){ // 基数输出1,偶数输出空格 System.out.print(c); }else { System.out.printf(" "); } } System.out.println(); } } }
import java.io.*; public class Main{ public static void main(String[] args)throws IOException{ char c = (char)System.in.read(); for(int i=1;i<=5;i++){ for(int j=5-i;j>0;j--){ System.out.print(" "); } for(int m=1;m<=i;m++) System.out.print(c+" "); System.out.println(); } } }考循环的使用,注意执行顺序即可,另java注意 print 和 peintln的区别
import java.io.IOException; public class Main{ public static void main(String[] args) throws IOException{ char c=(char)System.in.read(); int n = 5; for(int i = 1; i <= n; i++){ // 控制行数 for (int m = 1; m <= n-i; m++) { // 左侧空格 System.out.printf(" "); } for (int j = 1; j <= 2*i-1; j++) { if ((j+1)%2 == 0){ // 基数输出1,偶数输出空格 System.out.print(c); }else { System.out.printf(" "); } } System.out.println(); } } }
import java.util.*; public class Main{ public static void main(String[] args) { Scanner in=new Scanner(System.in); String s=in.nextLine(); sjt(s,5); } public static void sjt(String str,int n){ for(int i=0;i<n;i++) { int left=n-i-1;int right=i+n-1; int j=0; while(j<2*n-1) { if(j>=left&&j<=right) { System.out.print(str+" ");j=j+2;} else { System.out.print(" ");j++;} } System.out.println(""); } } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner scanner = new Scanner(System.in); char c=scanner.nextLine().charAt(0); System.out.println(" "+c); System.out.println(" "+c+" "+c); System.out.println(" "+c+" "+c+" "+c); System.out.println(" "+c+" "+c+" "+c+" "+c); System.out.println(c+" "+c+" "+c+" "+c+" "+c); } }