第一题
打印如下棱形,大小符号从控制台输入
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
分析:以棱形的中心点为中心建立二维坐标系,发现棱形上的所有的点横纵坐标的绝对值之和都小于某个值。反之只要满足横纵坐标绝对值之和小于这个值,那这个点就一定在棱形上。
// 主函数
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("row:");
int row=sc.nextInt();
System.out.println("symbol:");
String symbol=sc.next();
String[][] array=new String[row*2-1][row*2-1];
// 初始化数组
for (int i=0;i<(2*row-1);i++){
for (int j=0;j<(2*row-1);j++){
array[i][j]=" ";
}
}
// 将在棱形上的点(数组元素)重新赋值
for (int i=0;i<(2*row-1);i++){
for (int j=0;j<(2*row-1);j++){
if (isPrint(i,j,row-1,row)){
array[i][j]=symbol;
}
}
}
// 打印数组
printArrays(array);
}
// 判断是否打印
public static boolean isPrint(int i,int j,int focus,int len){
if ((Math.abs(focus-i)+Math.abs(focus-j))<len){
return true;
}
return false;
}
// 打印最终结果
public static void printArrays(String[][] array){
for (String[] arr:array){
for (String i:arr){
System.out.print(i);
}
System.out.println();
}
}
结果:
第二题
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
在问题一的基础上,打印小一点的棱形覆盖上去就行了
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("row:");
int row=sc.nextInt();
System.out.println("symbol:");
String symbol=sc.next();
String[][] array=new String[row*2-1][row*2-1];
// 初始化数组
for (int i=0;i<(2*row-1);i++){
for (int j=0;j<(2*row-1);j++){
array[i][j]=" ";
}
}
// 将在棱形上的点(数组元素)重新赋值为 *
for (int i=0;i<(2*row-1);i++){
for (int j=0;j<(2*row-1);j++){
if (isPrint(i,j,row-1,row)){
array[i][j]=symbol;
}
}
}
// 将在小号棱形上的点(数组元素)重新赋值为空格
for (int i=0;i<(2*row-1);i++){
for (int j=0;j<(2*row-1);j++){
if (isPrint(i,j,row-1,row-1)){
array[i][j]=" ";
}
}
}
printArrays(array);
}
结果:
第三题
*
* *
* *
* * *
* * * *
* * * *
* * * * *
* * * * * *
* * * * *
* * * *
* * * *
* * *
* *
* *
*
import java.util.Scanner;
public class demo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("row:");
int row=sc.nextInt();
System.out.println("symbol:");
String symbol=sc.next();
String[][] array=new String[row*2-1][row*2-1];
// 初始化数组
for (int i=0;i<(2*row-1);i++){
for (int j=0;j<(2*row-1);j++){
array[i][j]=" ";
}
}
overPrint(row,array,symbol,0);
printArrays(array);
}
// 打印覆盖
public static String[][] overPrint(int row,String[][] array,String symbol,int count){
for (int i=0;i<(2*row-1);i++){
for (int j=0;j<(2*row-1);j++){
if (isPrint(i,j,row-1,row-count)){
if (count%2==0){
array[i][j]=symbol;
}else{
array[i][j]="-";
}
}
}
}
count++;
if (row!=count){
overPrint(row,array,symbol,count);
}
return array;
}
// 判断是否打印
public static boolean isPrint(int i,int j,int focus,int len){
if ((Math.abs(focus-i)+Math.abs(focus-j))<len){
return true;
}
return false;
}
// 打印最终结果
public static void printArrays(String[][] array){
for (String[] arr:array){
for (String i:arr){
System.out.print(i);
}
System.out.println();
}
}
}
结果:
这里为了便于观看结果,我将棱形中间的花纹设置成 -
问题四
*
***
*****
*******
*********
*******
*****
***
*
* * *
*** *** ***
***** ***** *****
******* ******* *******
***************************
******* ******* *******
***** ***** *****
*** *** ***
* * *
* * * * *
*** *** *** *** ***
***** ***** ***** ***** *****
******* ******* ******* ******* *******
*********************************************
******* ******* ******* ******* *******
***** ***** ***** ***** *****
*** *** *** *** ***
* * * * *
* * *
*** *** ***
***** ***** *****
******* ******* *******
***************************
******* ******* *******
***** ***** *****
*** *** ***
* * *
*
***
*****
*******
*********
*******
*****
***
*