我需要创建一个程序,显示用户输入的两个单词,然后我需要显示第一个和最后一个字母以及这两个单词的字符数。
那部分没问题。但接下来我需要用第二个单词的第一个字母切换第一个单词的第一个字母。然后是第一个单词的最后一个字母,第二个单词的最后一个字母。和第二个词一样。
然后我需要对第二个单词做同样的事情。
我使用了下面的代码,但它用第二个单词中的字符替换了第一个单词中的所有字符(即相同的字符)。
请帮忙。我一直试图这样做几个小时!而且我对此很陌生。
public static void main(String[] args)
{
Scanner user_input = new Scanner(System.in);
String word_one;
String word_two;
System.out.print("Enter 2 words on one line separated by at least one space"
+ " (no white space allowed IN each word): ");
word_one = user_input.next( );
word_two = user_input.nextLine( );
System.out.println("\nFirst word you entered is <"
+ word_one
+ "> which is "
+ word_one.length()
+ " characters long.\nIt starts with the character '"
+ word_one.charAt(word_one.length() - word_one.length())
+ "' and ends with the character'"
+ word_one.charAt(word_one.length() - 1)
+ "'");
System.out.println("\nSecond word you entered is <"
+ (word_two.replace(" ", ""))
+ "> which is "
+ (word_two.replace(" ", "")).length()
+ " characters long. \nIt starts with the character '"
+ word_two.charAt(word_two.length() - word_two.length() + 1)
+ "' and ends with the character'"
+ word_two.charAt(word_two.length() - 1) + "'");
user_input.close();
System.out.println("\nNew words: "
+ word_one.replace(
word_one.charAt(word_one.length() - word_one.length()),
word_two.charAt(word_two.length() - word_two.length() + 1))
.replace(
word_one.charAt(word_one.length() - 1),
word_two.charAt(word_two.length() - 1))
+ word_two.replace(
word_two.charAt(word_two.length() - word_two.length() + 1),
word_one.charAt(word_one.length() - word_one.length()))
.replace(
word_two.charAt(word_two.length() - 1),
word_one.charAt(word_one.length() - 1)));
}
答案 0 :(得分:0)
replace(...)
用另一个子串替换给定子串的所有出现。您要找的是replaceFirst(...)
替换第一个字母,但替换最后一个字母并不是那么容易。在这种情况下,我会使用substring(...)
和手动连接。
或者,您可以使用与replaceAll(...)
一起使用的regular expressions并使用行/输入结构的开头/结尾(^$\A\Z
)。
答案 1 :(得分:0)
如果使用char数组而不是字符串,这会容易得多。然后你可以指向第一个(wordOne [0])和last(wordOne [wordOne.length])元素。
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
final Scanner userInput = new Scanner(System.in);
System.out.print("Enter 2 words on one line separated by at least one space (no white space allowed IN"
+ " each word): ");
final char[] wordOne = nextInput(userInput);
final char[] wordTwo = nextInput(userInput);
System.out.println("\nFirst word you entered is <" + stringOf(wordOne) + "> which is " + wordOne.length
+ " characters long.\nIt starts with the character '" + wordOne[(wordOne.length - wordOne.length)] + "' "
+ "and ends with the character'" + wordOne[wordOne.length - 1] + "'");
System.out.println("\nSecond word you entered is <" + stringOf(wordTwo) + "> which is " + wordTwo.length + " characters long. \nIt starts with the character '" + wordTwo[0] +
"' and ends with the character'" + wordTwo[wordTwo.length - 1] + "'");
userInput.close();
char[] newWordOne = wordOne.clone();
newWordOne[0] = wordTwo[0];
newWordOne[wordOne.length - 1] = wordTwo[wordTwo.length - 1];
char[] newWordTwo = wordTwo.clone();
newWordTwo[0] = wordOne[0];
newWordTwo[wordTwo.length - 1] = wordOne[wordOne.length - 1];
System.out.println("\nNew words: " + stringOf(newWordOne) + " " + stringOf(newWordTwo));
}
private static String stringOf(final char[] word) {
return new String(word);
}
private static char[] nextInput(final Scanner scanner) {
return scanner.next().trim().toCharArray();
}
}
答案 2 :(得分:0)
感谢您的所有建议!但事实证明,我需要做的就是使用子串,然后将它们与我需要的单词的第一个和最后一个字母连接起来。另外,我需要更换所有空间&#34; &#34;用&#34;&#34;因为它不断切换字母和空格,我不想让它计算空格。
希望这有助于其他人!