【发布时间】:2015-07-13 01:31:58
【问题描述】:
我必须计算正在阅读的博客条目中的前 1o 个单词……但我的代码不允许这种情况发生。我不能使用 .split 或 string isempty 或数组......这让我有 indexof 和子字符串。我的代码现在只得到前 3 个字......对我有任何帮助......
这是我必须使用的......
字符串 getSummary() 方法 1. 返回条目的前十个单词作为条目的摘要。如果条目有 10 个字或更少,则该方法返回整个条目。 2.可能的逻辑——String类的indexOf方法可以找到空格的位置。将其与循环结构一起使用以查找前 10 个单词。
public class BlogEntry
{
private String username;
private Date dateOfBlog;
private String blog;
public BlogEntry()
{
username = "";
dateOfBlog = new Date();
blog = "";
}
public BlogEntry(String sName, Date dBlogDate, String sBlog)
{
username = sName;
dateOfBlog = dBlogDate;
blog = sBlog;
}
public String getUsername()
{
return username;
}
public Date getDateOfBlog()
{
return dateOfBlog;
}
public String getBlog()
{
return blog;
}
public void setUsername(String sName)
{
username = sName;
}
public void setDateOfBlog(Date dBlogDate)
{
dateOfBlog.setDate(dBlogDate.getMonth(), dBlogDate.getDay(), dBlogDate.getYear());
}
public void setBlog(String sBlog)
{
blog = sBlog;
}
public String getSummary()
{
String summary = "";
int position;
int wordCount = 0;
int start = 0;
int last;
position = blog.indexOf(" ");
while (position != -1 && wordCount < 10)
{
summary += blog.substring(start, position) + " ";
start = position + 1;
position = blog.indexOf(" ", position + 1);
wordCount++;
}
return summary;
}
public String toString()
{
return "Author: " + this.getUsername() + "\n\n" + "Date posted: " + this.getDateOfBlog() + "\n\n" + "Text body: " + this.getBlog();
}
}
【问题讨论】:
-
这是什么语言? (Java?)为什么你不能使用那些方法?
-
对不起,它是 Java...
-
我从研究中了解这些方法,但因为这是一门课程,我无法超越我们所涵盖的范围......我一直在努力解决这个问题,似乎只是无法弄清楚我是什么做错了... .split 似乎又是最简单的 ut 我不能超出我们所涵盖的范围。 :(@NathanTuggy
-
blog 可以是任何长度的任何字符串....例如“这个程序让我很生气!”可能是字符串,我的代码只是拉这个程序这就是我需要帮助的原因
-
给出的关闭原因是错误的。戈尔迪给出了明确的问题陈述。 “我必须计算正在阅读的博客条目中的前 1o 个单词。”她提出了一个具体的问题:“只得到前 3 个单词” 问题是只有在特定输入时才成立:4 个单词。如果给定 6,它得到 5。我的编译器很好地解决了这个问题。当然,重新打开问题会带来很多好处,但这是在教 Goldie 关于 stackoverflow 的错误知识。