我目前正在圣何塞的编程入门课程中,作为我们作业的一部分,我们创建了一个类,其方法是返回一个字符串,其中包含每个单词的首字母或每个单词的最后一个字母。
实例变量“phrase”包含方法中访问的短语。
以下是规则:
单词用空格分隔, 它以一封信开头, 它不以空间结束, 永远不会有2个连续的空间, 永远不会有2个连续数字或标点符号。
如果短语为空,则firstLetter()和lastLetter()方法都必须返回空字符串。
我的问题是:这个问题的解决方案是什么?我是算法的新手,所以我希望对这个简单的问题采用更加经验丰富的方法。在firstLetter()和lastLetter()方法中,我会在for循环中一次检查两个字符的状态还是只检查一个?
这是我的代码:
/**
* Processes first and last letters of words
* @author (Adrian DeRose)
*/
public class StartToFinish
{
private String phrase;
/**
* Constructs a StartToFinish object
* @param myString the phase for this object
*/
public StartToFinish(String myString)
{
this.phrase = myString;
}
/**
* Gets first letter of every word in string.
*
* @return first letter of every word in string
*/
public String firstLetters()
{
String firstLetters = "";
if (Character.isLetter(this.phrase.charAt(0)))
{
firstLetters += this.phrase.substring(0,1);
}
for (int i = 1; i < this.phrase.length(); i++)
{
char currentCharacter = this.phrase.charAt(i);
String previousCharacter = Character.toString(this.phrase.charAt(i-1));
if (Character.isLetter(currentCharacter) && previousCharacter.equals(" "))
{
String characterString = Character.toString(currentCharacter);
firstLetters += characterString;
}
}
return firstLetters;
}
/**
* Gets last letter of every word in string.
*
* @return last letter of every word in string
*/
public String lastLetters()
{
String lastLetters = "";
char lastCharacter = this.phrase.charAt(lastIndex);
if (this.phrase.length() == 0)
{
return "";
}
for (int i = 1; i < this.phrase.length(); i++)
{
char currentCharacter = this.phrase.charAt(i);
char previousCharacter = this.phrase.charAt(i-1);
if (Character.isLetter(previousCharacter) && !Character.isLetter(currentCharacter))
{
String previousCharacterString = Character.toString(previousCharacter);
lastLetters += previousCharacterString;
}
}
if (Character.isLetter(lastCharacter))
{
lastLetters += Character.toString(lastCharacter);
}
return lastLetters;
}
}
谢谢!
答案 0 :(得分:1)
我不知道这是不是你想要的,但是写这个是更简单的方法(对不起我的英文)
String a="john snow winter is comming";
String[] parts = a.split(" ");
for(String word:parts){
System.out.println("first letter "+word.charAt(0)+ " last letter "+word.charAt(word.length()-1));
}
答案 1 :(得分:1)
我不这么认为你必须使用java函数编写所有这些代码:
String a = "Hello";
System.out.println("First:"+a.charAt(0));
System.out.println("Last:"+a.charAt(a.length()-1));
输出:
First:H
Last:o
答案 2 :(得分:0)
我将提供的解决方案之一是: 1.检查构造函数中的短语是否为空。 2.从拆分开始,然后进行检查。
在构造函数中(在你的情况下不需要btw)
splitedPhrase = phrase.split(' ');
在专用功能
中public String firstLetters() {
String result = "";
for(String word : splitedPhrase) {
if (Character.isLetter(word.charAt(0)))
result+=word.charAt(0);
}
return result;
}
你只需更改LastLetter函数的charAt,就像
word.charAt(word.length-1)
希望这个帮助,尽管有些人已经发布,但我认为这会更好地满足您的需求。
答案 3 :(得分:0)
如果我理解你的问题,我认为这就是你要找的: -
public class StartToFinish {
private String phrase;
private String[] words;
private String firstLetters = "";
private String lastLetters = "";
/**
* Constructs a StartToFinish object
*
* @param myString
* the phase for this object
*/
public StartToFinish(String myString) {
this.phrase = myString;
words = phrase.split(" ");
for (String string : words) {
if (string.length() == 0)
continue;
if (Character.isLetter(string.charAt(0))) {
firstLetters += string.charAt(0);
}
if (Character.isLetter(string.charAt(string.length() - 1))) {
lastLetters += string.charAt(string.length() - 1);
}
}
}
/**
* Gets first letter of every word in string.
*
* @return first letter of every word in string
*/
public String firstLetters() {
return firstLetters;
}
/**
* Gets last letter of every word in string.
*
* @return last letter of every word in string
*/
public String lastLetters() {
return lastLetters;
}
}