如何水平打印输出?
在此处输入图像描述我使用java编写了一个程序,如果您输入名称,它将打印出来星形图案。但输出是垂直的。我想要水平输出,以便显示名称。请帮忙。这是我的代码 代码看起来很长但是很简单。我是初学者请帮助
导入java.util.Scanner;
公共类模式{
// TODO Auto-generated method stub
static void PrintA() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if(((i==2)&&(j==2||j==3||j==4))||((i==4)&&(j==2||j==3||j==4))||((i==5)&&(j==2||j==3||j==4)))
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
static void PrintB() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==3||i==5)&&(j<=3))
System.out.print("*");
else if((i==2||i==4)&&(j==1||j==4))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintC() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==5)&&(j<=5))
System.out.print("*");
else if((i==2||i==3||i==4)&&(j==1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintD() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==5)&&(j<=3))
System.out.print("*");
else if((i==2||i==4)&&(j==4||j==1))
System.out.print("*");
else if(i==3&&(j==5||j==1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintE() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if(i%2!=0&&j<=5)
System.out.print("*");
else if((i>=2 && i<=4) && j==1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintF() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==3)&&j<=5)
System.out.print("*");
else if((i>=2&&i<=5)&&j==1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintG() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1&&j<=5)||(i==2&&j==1)||(i==3&&(j==1||j>=3))||(i==4&&j%2!=0))
System.out.print("*");
else if(i==5&&j!=4)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintH() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&(j==5||j==1))||(i==3&&j<=5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintI() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if(((i==1||i==5)&&j<=5)||(i<=5&&j==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintJ() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1&&j<=5)||(i<=5&&j==3))
System.out.print("*");
else if((i==5&&j<=3)||(i==4&&j==1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintK() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||(i%2==0 && j==3)||((i==1||i==5)&&j==5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintL() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||(i==5&&j<=5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintM() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5)&&(j==1||j==5))
System.out.print("*");
else if((i==2&&(j%2==0))||(i==3&&j==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintN() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5)&&(j==1||j==5))
System.out.print("*");
else if(i==j)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintO() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i>=2&&i<=4)&&(j>=2&&j<=4))
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
static void PrintP() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||((i==1||i==3)&&j<=5))
System.out.print("*");
else if(i==2&&j==5)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintQ() {
int i,j;
for(i=1;i<=7;i++) {
for(j=1;j<=5;j++) {
if((i>=2&&i<=4)&&(j>=2&&j<=4))
System.out.print(" ");
else if((i==6&&j==4)||(i==7&&j==5))
System.out.print("*");
else if((i==6||i==7)&&(j!=4||j!=5))
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
static void PrintR() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||((i==1||i==3)&&j<=5))
System.out.print("*");
else if((i==2&&j==5)||(i==4&&j==4)||(i==5&&j==5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintS() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=3&&j==1)||((i==1||i==3||i==5)&&j<=5)||(i==4&&j==5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintT() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1&&j<=5)||(i<=5&&j==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintU() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||(i<=5&&j==5))
System.out.print("*");
else if(i==5&&(j>2&&j<4))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintV() {
int i,j,n=11;
for(i=1;i<=5;i++) {
for(j=1;j<=10;j++) {
if ((i<=5&&j==(n-i))||(i==j))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintW() {
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++) {
if((i<=5&&(j==1||j==5))||(i==3&&j==3)||(i==4&&j!=3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintX() {
int i,j,n=6;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==(n-i))||(i==j))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintY() {
int i,j,n=6;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=3&&j==(n-i))||(i>=3&&j==3)||(i<=3&&i==j))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintZ() {
int i,j,n=6;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==(n-i))||((i==1||i==5)&&j<=5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void printinput(char input) {
switch (input)
{
case 'A':
case 'a':
PrintA();
System.out.println();
break;
case 'B':
case 'b':
PrintB();
System.out.println();
break;
case 'C':
case 'c':
PrintC();
System.out.println();
break;
case 'D':
case 'd':
PrintD();
System.out.println();
break;
case 'E':
case 'e':
PrintE();
System.out.println();
break;
case 'F':
case 'f':
PrintF();
System.out.println();
break;
case 'G':
case 'g':
PrintG();
System.out.println();
break;
case 'H':
case 'h':
PrintH();
System.out.println();
break;
case 'I':
case 'i':
PrintI();
System.out.println();
break;
case 'J':
case 'j':
PrintJ();
System.out.println();
break;
case 'K':
case 'k':
PrintK();
System.out.println();
break;
case 'L':
case 'l':
PrintL();
System.out.println();
break;
case 'M':
case 'm':
PrintM();
System.out.println();
break;
case 'N':
case 'n':
PrintN();
System.out.println();
break;
case 'O':
case 'o':
PrintO();
System.out.println();
break;
case 'P':
case 'p':
PrintP();
System.out.println();
break;
case 'Q':
case 'q':
PrintQ();
System.out.println();
break;
case 'R':
case 'r':
PrintR();
System.out.println();
break;
case 'S':
case 's':
PrintS();
System.out.println();
break;
case 'T':
case 't':
PrintT();
System.out.println();
break;
case 'U':
case 'u':
PrintU();
System.out.println();
break;
case 'V':
case 'v':
PrintV();
System.out.println();
break;
case 'W':
case 'w':
PrintW();
System.out.println();
break;
case 'X':
case 'x':
PrintX();
System.out.println();
break;
case 'Y':
case'y':
PrintY();
System.out.println();
break;
case 'Z':
case 'z':
PrintZ();
System.out.println();
break;
}
}
public static void main(String[] args) {
Scanner Name = new Scanner(System.in);
System.out.println("Please Enter the Name : ");
String input = Name.nextLine();
for(int i=0;i<=input.length()-1;i++) {
printinput(input.charAt(i));
}
}
}
enter image description hereI have written a program using java where if you input name it will print in star pattern. But the output is vertical. i want output horizontally so name is displayed. please help . Here is my code
It looks long but simple code. I am a beginner please help
import java.util.Scanner;
public class patterns {
// TODO Auto-generated method stub
static void PrintA() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if(((i==2)&&(j==2||j==3||j==4))||((i==4)&&(j==2||j==3||j==4))||((i==5)&&(j==2||j==3||j==4)))
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
static void PrintB() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==3||i==5)&&(j<=3))
System.out.print("*");
else if((i==2||i==4)&&(j==1||j==4))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintC() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==5)&&(j<=5))
System.out.print("*");
else if((i==2||i==3||i==4)&&(j==1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintD() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==5)&&(j<=3))
System.out.print("*");
else if((i==2||i==4)&&(j==4||j==1))
System.out.print("*");
else if(i==3&&(j==5||j==1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintE() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if(i%2!=0&&j<=5)
System.out.print("*");
else if((i>=2 && i<=4) && j==1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintF() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1||i==3)&&j<=5)
System.out.print("*");
else if((i>=2&&i<=5)&&j==1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintG() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1&&j<=5)||(i==2&&j==1)||(i==3&&(j==1||j>=3))||(i==4&&j%2!=0))
System.out.print("*");
else if(i==5&&j!=4)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintH() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&(j==5||j==1))||(i==3&&j<=5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintI() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if(((i==1||i==5)&&j<=5)||(i<=5&&j==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintJ() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1&&j<=5)||(i<=5&&j==3))
System.out.print("*");
else if((i==5&&j<=3)||(i==4&&j==1))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintK() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||(i%2==0 && j==3)||((i==1||i==5)&&j==5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintL() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||(i==5&&j<=5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintM() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5)&&(j==1||j==5))
System.out.print("*");
else if((i==2&&(j%2==0))||(i==3&&j==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintN() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5)&&(j==1||j==5))
System.out.print("*");
else if(i==j)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintO() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i>=2&&i<=4)&&(j>=2&&j<=4))
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
static void PrintP() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||((i==1||i==3)&&j<=5))
System.out.print("*");
else if(i==2&&j==5)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintQ() {
int i,j;
for(i=1;i<=7;i++) {
for(j=1;j<=5;j++) {
if((i>=2&&i<=4)&&(j>=2&&j<=4))
System.out.print(" ");
else if((i==6&&j==4)||(i==7&&j==5))
System.out.print("*");
else if((i==6||i==7)&&(j!=4||j!=5))
System.out.print(" ");
else
System.out.print("*");
}
System.out.println();
}
}
static void PrintR() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||((i==1||i==3)&&j<=5))
System.out.print("*");
else if((i==2&&j==5)||(i==4&&j==4)||(i==5&&j==5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintS() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=3&&j==1)||((i==1||i==3||i==5)&&j<=5)||(i==4&&j==5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintT() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i==1&&j<=5)||(i<=5&&j==3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintU() {
int i,j;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==1)||(i<=5&&j==5))
System.out.print("*");
else if(i==5&&(j>2&&j<4))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintV() {
int i,j,n=11;
for(i=1;i<=5;i++) {
for(j=1;j<=10;j++) {
if ((i<=5&&j==(n-i))||(i==j))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintW() {
int i,j;
for(i=1;i<=5;i++){
for(j=1;j<=5;j++) {
if((i<=5&&(j==1||j==5))||(i==3&&j==3)||(i==4&&j!=3))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintX() {
int i,j,n=6;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==(n-i))||(i==j))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintY() {
int i,j,n=6;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=3&&j==(n-i))||(i>=3&&j==3)||(i<=3&&i==j))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
static void PrintZ() {
int i,j,n=6;
for(i=1;i<=5;i++) {
for(j=1;j<=5;j++) {
if((i<=5&&j==(n-i))||((i==1||i==5)&&j<=5))
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
public static void printinput(char input) {
switch (input)
{
case 'A':
case 'a':
PrintA();
System.out.println();
break;
case 'B':
case 'b':
PrintB();
System.out.println();
break;
case 'C':
case 'c':
PrintC();
System.out.println();
break;
case 'D':
case 'd':
PrintD();
System.out.println();
break;
case 'E':
case 'e':
PrintE();
System.out.println();
break;
case 'F':
case 'f':
PrintF();
System.out.println();
break;
case 'G':
case 'g':
PrintG();
System.out.println();
break;
case 'H':
case 'h':
PrintH();
System.out.println();
break;
case 'I':
case 'i':
PrintI();
System.out.println();
break;
case 'J':
case 'j':
PrintJ();
System.out.println();
break;
case 'K':
case 'k':
PrintK();
System.out.println();
break;
case 'L':
case 'l':
PrintL();
System.out.println();
break;
case 'M':
case 'm':
PrintM();
System.out.println();
break;
case 'N':
case 'n':
PrintN();
System.out.println();
break;
case 'O':
case 'o':
PrintO();
System.out.println();
break;
case 'P':
case 'p':
PrintP();
System.out.println();
break;
case 'Q':
case 'q':
PrintQ();
System.out.println();
break;
case 'R':
case 'r':
PrintR();
System.out.println();
break;
case 'S':
case 's':
PrintS();
System.out.println();
break;
case 'T':
case 't':
PrintT();
System.out.println();
break;
case 'U':
case 'u':
PrintU();
System.out.println();
break;
case 'V':
case 'v':
PrintV();
System.out.println();
break;
case 'W':
case 'w':
PrintW();
System.out.println();
break;
case 'X':
case 'x':
PrintX();
System.out.println();
break;
case 'Y':
case'y':
PrintY();
System.out.println();
break;
case 'Z':
case 'z':
PrintZ();
System.out.println();
break;
}
}
public static void main(String[] args) {
Scanner Name = new Scanner(System.in);
System.out.println("Please Enter the Name : ");
String input = Name.nextLine();
for(int i=0;i<=input.length()-1;i++) {
printinput(input.charAt(i));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论