package com.javase.demo;
import java.util.Scanner;
/**
* 金字塔
* @author Mr.Zhang
*
*/
public class Pyramid {
static Scanner input = new Scanner(System.in);
/**
* *****打印金字塔*****
* 1,确定金字塔行数
* 2,确认空格数
* 3,确认星星数
* @param args
*/
public static void main(String[] args) {
entrance();
}
/**
* 入口项
*/
public static void entrance() {
System.out.println("请选择(0--正金字塔,1--倒金字塔,2--菱形金字塔)");
String select = input.nextLine();
if(isNumber(select)){
int selectInt = Integer.parseInt(select);
switch(selectInt){
case 0:
uprightPyramid();
break;
case 1:
fallPyramid();
break;
case 2:
System.out.println("该功能尚未完善!");
break;
default:
System.out.println("请输入正确的选项!");
entrance();
break;
}
}else if(!select.equals(0) || !select.equals(1) || !select.equals(2)){
nullSuccess();
}
}
/**
* 打印正金字塔
* @param input
*/
public static void uprightPyramid() {
System.out.println("请输入行数:");
String row = input.nextLine();
if(isNumber(row)){
int rows = Integer.parseInt(row);
for(int i = 1;i <= rows;i++){ //循环输入的行数,
for(int j = 1;j <= rows - i;j++){ //输出循环每行的空格
System.out.print(" ");
}
for(int k = 1;k <= 2 * i - 1;k++){ // 输出循环每行的★
System.out.print("★");
}
System.out.println();
}
System.out.println("打印完成,线程结束");
}else{
nullSuccess();
}
}
/**
* 打印倒金字塔
*/
public static void fallPyramid(){
System.out.println("请输入行数:");
String row = input.nextLine();
if(isNumber(row)){
int rows = Integer.parseInt(row);
for(int i = rows;i >= 1;i--){
for(int j = 0;j < rows-i;j++){ //打印空格数
System.out.print(" ");
}
for(int k = 0;k < i * 2 - 1;k++){ //打印★数
System.out.print("★");
}
System.out.println();
}
System.out.println("打印完成,线程结束");
}else{
nullSuccess();
}
}
/**
* 判断是否为数字
* @param str
* @return
*/
public static boolean isNumber(String str){
boolean ok = false;
if(null != str && str.matches("^[0-9]")){
return true;
}
return ok;
}
/**
* 调用错误结果
*/
public static void nullSuccess(){
System.out.println("您输入的不是数字,一遍浪去,不听话的孩子!");
}
}