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);
}
}