如何在Java中以这种模式打印不同的字符串?
我正在尝试用java编写一个程序,它将为输入的字符串提供一个模式,如下所示如何在Java中以这种模式打印不同的字符串?,java,Java,我正在尝试用java编写一个程序,它将为输入的字符串提供一个模式,如下所示 C O M P U T E R O E M T P U U P T M E O R E T U P M O C 这是我的程序代码 import java.util.Scanner; class pandapattern { public static void main
C O M P U T E R
O E
M T
P U
U P
T M
E O
R E T U P M O C
这是我的程序代码
import java.util.Scanner;
class pandapattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a word : ");
String s=sc.nextLine();
System.out.println();
int l=s.length();
for(int i=0;i<+l;i++)
{
System.out.print(s.charAt(i)+" ");
}
char[][] frwd = new char[l][1];
char[][] bcwd = new char[l][1];
for(int f=1;f<l;f++)
{
frwd[f][0]=s.charAt(f);
}
for(int b=l-2;b>=0;b--)
{
bcwd[b][0]=s.charAt(b);
}
for(int p=1;p<l;p++)
{
System.out.print("\n"+frwd[p][0]);
}
for(int p1=l-1;p1>=0;p1--)
{
System.out.print(bcwd[p1][0]+" ");
}
}
}
我怎样才能把整个图案打印出来
请帮我弄清楚。首先,对于此任务,您需要一维数组
frwd
和bcwr
其次,以与frwd
相同的方式填充数组bcwd
为任务正确重写了部分方法:
int length = s.length();
//printing first line
for (int i = 0; i < +length; i++) {
System.out.print(s.charAt(i) + " ");
}
System.out.println();
//filling arrays
char[] frwd = new char[length];
char[] bcwd = new char[length];
for (int f = 1; f < length; f++) {
frwd[f] = s.charAt(f);
}
for (int b = 0; b < length; b++) {
bcwd[b] = s.charAt(length-1 - b);
}
for (int p = 1; p < length-1; p++) {
System.out.print(frwd[p]);
//filling spaces to line by length of input string
for (int p3 = 1; p3 < frwd.length-1; p3++) {
System.out.print(" " + " ");
}
System.out.print(" " + bcwd[p]);
System.out.println();
}
for (int p = 0; p <= length - 1; p++) {
System.out.print(bcwd[p] + " ");
}
System.out.println();
int length=s.length();
//打印第一行
对于(int i=0;i<+长度;i++){
系统输出打印(s.charAt(i)+“”);
}
System.out.println();
//填充阵列
char[]frwd=新字符[长度];
char[]bcwd=新字符[长度];
对于(int f=1;f 对于(int p=0;p首先,对于此任务,您需要一维数组frwd
和bcwr
其次,以与frwd
相同的方式填充数组bcwd
为任务正确重写了部分方法:
int length = s.length();
//printing first line
for (int i = 0; i < +length; i++) {
System.out.print(s.charAt(i) + " ");
}
System.out.println();
//filling arrays
char[] frwd = new char[length];
char[] bcwd = new char[length];
for (int f = 1; f < length; f++) {
frwd[f] = s.charAt(f);
}
for (int b = 0; b < length; b++) {
bcwd[b] = s.charAt(length-1 - b);
}
for (int p = 1; p < length-1; p++) {
System.out.print(frwd[p]);
//filling spaces to line by length of input string
for (int p3 = 1; p3 < frwd.length-1; p3++) {
System.out.print(" " + " ");
}
System.out.print(" " + bcwd[p]);
System.out.println();
}
for (int p = 0; p <= length - 1; p++) {
System.out.print(bcwd[p] + " ");
}
System.out.println();
int length=s.length();
//打印第一行
对于(int i=0;i<+长度;i++){
系统输出打印(s.charAt(i)+“”);
}
System.out.println();
//填充阵列
char[]frwd=新字符[长度];
char[]bcwd=新字符[长度];
对于(int f=1;f一个char[]
数组,而不是更多。
诀窍是“一行一行地”解决问题
见下文:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter word for Panda Pattern: ");
String word = scanner.nextLine();
String wspace = " ";
//convert user input from String to char[]
char[] wordLetters = word.toCharArray();
//define array's length, for ease of reference
int length = wordLetters.length;
//initially print the sentence in a horizontal line
for (char wordLetter : wordLetters) {
System.out.print(wordLetter + wspace);
}
//insert new line to start printing for the pattern
System.out.print("\n");
/*A for loop that will print the left-most letters vertically
We start the loop from 1, because the first letter was already printed*/
for(int i=1; i<length; i++){
System.out.print(wordLetters[i]);
/*now we have an inner loop that will print the spaces and the
rest of the letters in reverse order*/
for(int j=1; j<length; j++){
//conditional for IF we are at final line
if(i == length-1 && j != length-1)
System.out.print(wspace + wordLetters[i-j]);
//conditional for printing right-most letters
else if(j == length-1) {
System.out.print(wspace + wordLetters[j-i]+"\n");
}
//THIS WILL PRINT 2 WHITE-SPACES.
else
System.out.print(wspace + wspace);
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.print(“输入熊猫图案的单词:”);
字符串字=scanner.nextLine();
字符串wspace=“”;
//将用户输入从字符串转换为字符[]
char[]wordLetters=word.toCharArray();
//定义数组的长度,以便于参考
int length=wordLetters.length;
//首先将句子打印成水平行
for(字符wordLetter:wordLetters){
系统输出打印(wordLetter+wspace);
}
//插入新行以开始打印图案
系统输出打印(“\n”);
/*用于垂直打印最左边字母的for循环
我们从1开始循环,因为第一个字母已经打印出来了*/
对于(inti=1;i您只需要一个char[]
数组,而不是更多。
诀窍是“一行一行地”解决问题
见下文:
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter word for Panda Pattern: ");
String word = scanner.nextLine();
String wspace = " ";
//convert user input from String to char[]
char[] wordLetters = word.toCharArray();
//define array's length, for ease of reference
int length = wordLetters.length;
//initially print the sentence in a horizontal line
for (char wordLetter : wordLetters) {
System.out.print(wordLetter + wspace);
}
//insert new line to start printing for the pattern
System.out.print("\n");
/*A for loop that will print the left-most letters vertically
We start the loop from 1, because the first letter was already printed*/
for(int i=1; i<length; i++){
System.out.print(wordLetters[i]);
/*now we have an inner loop that will print the spaces and the
rest of the letters in reverse order*/
for(int j=1; j<length; j++){
//conditional for IF we are at final line
if(i == length-1 && j != length-1)
System.out.print(wspace + wordLetters[i-j]);
//conditional for printing right-most letters
else if(j == length-1) {
System.out.print(wspace + wordLetters[j-i]+"\n");
}
//THIS WILL PRINT 2 WHITE-SPACES.
else
System.out.print(wspace + wspace);
}
}
}
publicstaticvoidmain(字符串[]args){
扫描仪=新的扫描仪(System.in);
System.out.print(“输入熊猫图案的单词:”);
字符串字=scanner.nextLine();
字符串wspace=“”;
//将用户输入从字符串转换为字符[]
char[]wordLetters=word.toCharArray();
//定义数组的长度,以便于参考
int length=wordLetters.length;
//首先将句子打印成水平行
for(字符wordLetter:wordLetters){
系统输出打印(wordLetter+wspace);
}
//插入新行以开始打印图案
系统输出打印(“\n”);
/*用于垂直打印最左边字母的for循环
我们从1开始循环,因为第一个字母已经打印出来了*/
对于(int i=1;i除了你的主要问题,new char[l][1]
没有意义。它是ix1
数组,它只是一维i
数组(你只需从[p][0]
中删除[0]
)。在你的下一个循环中(for(int p=1;从你的主要问题中删除,new char l][1]
没有意义。它是ix1
数组,它只是一维i
数组(您只需从[p][0]
中删除[0]
)(int p=1;p@JyotirmayKumarJha添加适当的空格,以便可以获得所需的格式want@JyotirmayKumarJha添加适当的空格,以便获得所需的格式