课后随机练习
产生几个不同的菱形
package jie0717;
/*
* 产生几个不同的菱形
*/
import java.util.Scanner;;
public class Test11 {
public static void main(String[] args) {
System.out.println("1、实心菱形;2、空心菱形;3、左飞机形;4、对称飞机;5、宽体菱形;6、镖形;7、右飞机形");
boolean is=false;
for(int i=0;i<1;i++){
System.out.print("请选择一个图形的标号:");
Scanner scanner=new Scanner(System.in);
int c=scanner.nextInt();
switch(c){
case 1:
Test1();
break;
case 2:
Test2();
break;
case 3:
Test3();
break;
case 4:
Test4();
break;
case 5:
Test5();
break;
case 6:
Test6();
break;
case 7:
Test7();
break;
default:
System.out.println("没有您要输入的图形标号!请从新输入:");
i--;
break;
}
System.out.println("是否重新输入:1、是;2、否");
Scanner scanner2=new Scanner(System.in);
int a=scanner2.nextInt();
if(a==1){
i--;
}
else if(a==2){
System.out.println("选择结束!退出!");
}
}
//Test11 test1=new Test11(b);
}
public static void Test1(){
System.out.print("请输入图形的参数:");
Scanner scanner1=new Scanner(System.in);
int b=scanner1.nextInt();
int a=b;
for(int i=1;i<=a;i++){
for(int j=1;j<a-i+1;j++){
System.out.print(" ");
}
for(int j=1;j<2*i;j++){
System.out.print("*");
}
System.out.println();
}
for(int i=a+1;i<2*a;i++){
for(int j=1;j<=i-a;j++){
System.out.print(" ");
}
for(int j=1;j<2*(2*a-i);j++){
System.out.print("*");
}
System.out.println();
}
}
public static void Test2(){
System.out.print("请输入图形的参数:");
Scanner scanner1=new Scanner(System.in);
int b=scanner1.nextInt();
int count=b;
for(int i=1;i<=count*2-1;i++){
if(i<=(count*2-1)/2){
for(int j=0;j<=count-i;j++){
System.out.print(" ");
}
System.out.print("*");
if(i==1){
}
else{
for(int k=0;k<(i-1)*2-1;k++){
System.out.print(" ");
}
System.out.print("*");
}
System.out.print("\n");
}
else{
for(int j=0;j<=i-count;j++){
System.out.print(" ");
}
System.out.print("*");
if(i==(count*2-1)){
}
else{
for(int k=0;k<(count*2-1-i)*2-1;k++){
System.out.print(" ");
}
System.out.print("*");
}
System.out.print("\n");
}
}
}
public static void Test3(){
System.out.print("请输入图形的参数:");
Scanner scanner1=new Scanner(System.in);
int b=scanner1.nextInt();
int a=b;
for(int i=1;i<=a;i++){
for(int j=1;j<a-i+1;j++){
System.out.print(" ");
}
for(int j=1;j<2*i;j++){
System.out.print("*");
}
System.out.println();
}
for(int i=a+1;i<2*a;i++){
for(int j=1;j<=i-a;j++){
System.out.print(" ");
}
for(int j=1;j<2*(2*a-i);j++){
System.out.print("*");
}
System.out.println();
}
}
public static void Test4(){
System.out.print("请输入图形的参数:");
Scanner scanner1=new Scanner(System.in);
int b=scanner1.nextInt();
int a=b;
for(int i=1;i<=a;i++){
for(int j=1;j<a-i+1;j++){
System.out.print(" ");
}
for(int j=1;j<2*i;j++){
System.out.print("*");
}
for(int j=1;j<2*i;j++){
System.out.print(" ");
}
for(int j=1;j<2*i;j++){
System.out.print("*");
}
System.out.println();
}
for(int i=a+1;i<2*a;i++){
for(int j=1;j<=i-a;j++){
System.out.print(" ");
}
for(int j=1;j<2*(2*a-i);j++){
System.out.print("*");
}
for(int j=1;j<=2*(2*a-i)-1;j++){
System.out.print(" ");
}
for(int j=1;j<2*(2*a-i);j++){
System.out.print("*");
}
System.out.println();
}
}
public static void Test5(){
System.out.print("请输入图形的参数:");
Scanner scanner1=new Scanner(System.in);
int b=scanner1.nextInt();
int a=b;
for(int i=1;i<=a;i++){
for(int j=1;j<a-i+1;j++){
System.out.print(" ");
}
for(int j=1;j<2*i;j++){
System.out.print(" * ");
}
System.out.println();
}
for(int i=a+1;i<2*a;i++){
for(int j=1;j<=i-a;j++){
System.out.print(" ");
}
for(int j=1;j<2*(2*a-i);j++){
System.out.print(" * ");
}
System.out.println();
}
}
public static void Test6(){
System.out.print("请输入图形的参数:");
Scanner scanner1=new Scanner(System.in);
int b=scanner1.nextInt();
int a=b;
for(int i=1;i<=a;i++){
for(int j=1;j<a+i;j++){
System.out.print(" ");
}
for(int j=1;j<2*i;j++){
System.out.print("*");
}
System.out.println();
}
for(int i=a+1;i<=2*a;i++){
for(int j=1;j<i-a;j++){
System.out.print(" ");
}
for(int j=1;j<2*(2*a-i)+2;j++){
System.out.print("*");
}
System.out.println();
}
}
public static void Test7(){
System.out.print("请输入图形的参数:");
Scanner scanner1=new Scanner(System.in);
int b=scanner1.nextInt();
int a=b;
for(int i=1;i<=a;i++){
for(int j=1;j<i;j++){
System.out.print(" ");
}
for(int j=1;j<2*i;j++){
System.out.print("*");
}
System.out.println();
}
for(int i=a+1;i<2*a;i++){
for(int j=1;j<=2*a-i;j++){
System.out.print(" ");
}
for(int j=1;j<2*(2*a-i);j++){
System.out.print("*");
}
System.out.println();
}
}
}
测试结果: