English 中文(简体)
我如何在休闲场所使用空间
原标题:How do I make use of spaces in a for loop

My goal with this program is to • Obtain a word with more than 5 letters from the console. • Verify that an empty String was not entered and that the word has more than five letters. • Enter a number greater than 2 and less than 5 for the trunk length. • Verify that the number meets the requirements. • Convert the word to all capitals. • Determine if the word has an even or odd number of letters. • If it is even, the tree will start with two letters on top and then increase the number of letters on each row by 2. The trunk will be made up of the two middle letters of the word and the number of rows will match the trunk length. • If it is odd, the tree will start with one letter on top and then increase the number of letters on each row by 2. The trunk will be made up of the three middle letters of the word and the number of rows will match the trunk length.

目标产出甚至包括:

      CH 
     CHRI
    CHRIST
   CHRISTMA
  CHRISTMAST
 CHRISTMASTRE
CHRISTMASTREES
      MA
      MA
      MA

目标产出是字母奇数:

     C
    CHR
   CHRIST
  CHRISTMA
 CHRISTMASTR
CHRISTMASTREE
     TMA
     TMA
     TMA

The Code I used:

Scanner input = new Scanner(System.in);

// Obtain a word with more than 5 letters from the console
System.out.print("Enter a word with more than 5 letters: ");
String theWord = input.nextLine();

// Verify that an empty String was not entered and that the 
word has more than five letters
if (theWord.isEmpty() || theWord.length() <= 5) {
System.out.println("Please enter a non-empty word with more than 
5 letters.");
return;
}

// Enter a number greater than 2 and less than 5 for the trunk 
length
System.out.print("Enter a number greater than 2 and less than 5 
for the trunk length: ");
int trunkLength = input.nextInt();

// Verify that the number meets the requirements
if (trunkLength <= 2 || trunkLength >= 5) {
System.out.println("Please enter a number greater than 2 and less 
than 5 for the trunk length.");
return;
}

// Convert the word to all capitals
String theWord2 = theWord.toUpperCase();

// Determine if the word has an even number of letters
if (theWord2.length() % 2 == 0) {
String space = "";
for (int i = 0; i < theWord2.length(); i += 2) {
String twoLetters = theWord2.substring(i, i + 2);
space += twoLetters;
System.out.println(space);
}
for (int j = 0; j < trunkLength; j++) {
int centerIndex = theWord2.length() / 2;
char firstCenterLetter = theWord2.charAt(centerIndex - 1);
char secondCenterLetter = theWord2.charAt(centerIndex);
System.out.println(firstCenterLetter + "" + secondCenterLetter);
}
}

// Determine if the word has an odd number of letters
if (theWord2.length() % 2 != 0) {
for (int p = 0; p < theWord2.length(); p += 2) {
String letters = theWord2.substring(0, p + 1);
System.out.println(letters);
}

for (int k = 0; k < trunkLength; k++) {
int centerIndex = theWord2.length() / 2;
char firstCenterLetter = theWord2.charAt(centerIndex - 1);
char middleCenterLetter = theWord2.charAt(centerIndex);
char lastCenterLetter = theWord2.charAt(centerIndex + 1);
System.out.println(firstCenterLetter + "" + middleCenterLetter + 
"" + lastCenterLetter);
}
}
问题回答

我尝试执行你的法典,没有产生你声称的结果。 我修改了你的法典如下,以产生你所说的结果,然后添加了dding,以产生预期产出。

public static void main (String args[])
    {
        String inputString = "CHRISTMASTREES";
        int trunkLength = 2;

        String space = "", pad = "";
        for (int i = 0; i < inputString.length(); i += 2) {
            String twoLetters = inputString.substring(i, i + 2);
            space += twoLetters;
            pad = " ".repeat(inputString.length()/2 - space.length()/2);
            System.out.println(pad+space);
        }
        for (int j = 0; j < trunkLength; j++) {
            int centerIndex = inputString.length() / 2;
            char firstCenterLetter = inputString.charAt(centerIndex - 1);
            char secondCenterLetter = inputString.charAt(centerIndex);
            pad = " ".repeat(inputString.length()/2-trunkLength/2);
            System.out.println(pad+firstCenterLetter + "" + secondCenterLetter);
        }
    }

我通过检查一半的插图和一半的平面图纸(在你的代码中,这称为空间,但名称不甚明)。 然后,我与投入String.length()/2-trunkLength/2一样。

      CH
     CHRI
    CHRIST
   CHRISTMA
  CHRISTMAST
 CHRISTMASTRE
CHRISTMASTREES
      MA
      MA

虽然我注意到你关于生产中继器的守则,但除了2台中继器外,没有任何其他用途。 你应当研究一下:





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签