请问这个方法怎么写啊?

panzhixiong_cn 2014-06-25 05:34:59
我有个如下的字符串

Flanked by climate change campaigner and former US vice-president Al Gore, Mr Palmer announced his party would vote against the Government's bid to abolish the Clean Energy Finance Corporation, the Renewable Energy Target and the Climate Change Authority.

我通过一个方法想把它自动转成:
A Flanked by climate change campaigner
B and former US vice-president Al Gore, Mr
C Palmer announced his party would vote
D against the Government's bid to abolish
E the Clean Energy Finance Corporation,
F the Renewable Energy Target and the
G Climate Change Authority.

要求
1.每行长度不超过40
2.开头有按照字母顺序的排序

谢谢!
...全文
282 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
业余草 2014-06-26
  • 打赏
  • 举报
回复
高手在人间了,学习了
Edward-liang 2014-06-26
  • 打赏
  • 举报
回复
提供一个思路 接收输入到一个字符串中,然后用split分割到字符串数组中。 用一个变量做行标 char line='A', int length=0 1.输出line +空格,length=2 2.判断字符串数组下一个单词的长度+length是否小于40 如果小于等于40输出他+空格 如果大于,输出换行,下标减1(这个字符串放到下次循环中判断),返回1 循环在数组所有元素都遍历完结束。
冰思雨 2014-06-26
  • 打赏
  • 举报
回复
	public static void main(String[] args) {
		String input = "Flanked by climate change campaigner and former US vice-president Al Gore, Mr Palmer announced his party would vote against the Government's bid to abolish the Clean Energy Finance Corporation, the Renewable Energy Target and the Climate Change Authority.";
		final int MAX_COUNT = 40;
		ArrayList<String> array = new ArrayList<String>();
		int beginIndex = 0, endIndex = 0, count = 0;
		char LineNumber = 'A';
		for(int i=0;i<input.length();i++){
			if(input.charAt(i)==' '){
				endIndex = i;
			}
			count++;
			if(count>=MAX_COUNT){
				if(i+1<input.length() && input.charAt(i+1)==' '){
					endIndex = i+1;
				}
				array.add(LineNumber+" "+input.substring(beginIndex, endIndex));
				LineNumber++;
				beginIndex=endIndex+1;
				count = i-endIndex;
			}
		}
		if(endIndex<input.length()){
			array.add(LineNumber+" "+input.substring(beginIndex));
		}
		for(String value : array){
			System.out.println(value);
		}
	}
随记N 2014-06-26
  • 打赏
  • 举报
回复
引用 5 楼 stonefeng 的回复:
[quote=引用 4 楼 panzhixiong_cn 的回复:] 貌似都无法实现。
split分割,然后写字母行号,然后从单词数组里面逐个拿出单词,如果该行每超出40,则在改行输出,如果超出,另起一行,再写行号,然后如上循环,直到结束。[/quote] String str = "Flanked by climate change campaigner and former US vice-president Al Gore, Mr Palmer announced his party would vote against the Government's bid to abolish the Clean Energy Finance Corporation, the Renewable Energy Target and the Climate Change Authority."; String[] strs = str.split(" "); String[] bb = {"A","B","C","D","E","F","G","H","J"}; String qq = ""; int a = 0; for(int i = 0; i < strs.length; i++) { qq += strs[i]+ " "; if(qq.length() > 40) { String oo = bb[a] + " ";//必须保证bb的元素个数大于等于输出的行数 int lastLength = (strs[i]+ " ").length(); oo += qq.substring(0,qq.length()-lastLength); i = i - 1; System.out.println(oo); qq = ""; a = a + 1; } }
疯癫行者 2014-06-26
  • 打赏
  • 举报
回复
引用 4 楼 panzhixiong_cn 的回复:
貌似都无法实现。
split分割,然后写字母行号,然后从单词数组里面逐个拿出单词,如果该行每超出40,则在改行输出,如果超出,另起一行,再写行号,然后如上循环,直到结束。
panzhixiong_cn 2014-06-26
  • 打赏
  • 举报
回复
貌似都无法实现。
疯癫行者 2014-06-26
  • 打赏
  • 举报
回复
引用 2 楼 rui888 的回复:
开头有按照字母顺序的排序 什么意思
相当于行号,只不过用ABCD表示的,不是1234。
tony4geek 2014-06-26
  • 打赏
  • 举报
回复
开头有按照字母顺序的排序 什么意思
lliiqiang 2014-06-26
  • 打赏
  • 举报
回复
stringtokenizer分割,然后统计字数,如果字符串过长,就重新建立一个空字符串对象,否则就在原来字符串上加,每次产生的新字符串必须加到集合中,最后根据首字母对于集合中的字符串排序.
LinY_X 2014-06-26
  • 打赏
  • 举报
回复
引用 13 楼 shine333 的回复:
几个问题 1 换行的依据是什么?空格肯定算了,其他符号呢,比如连字符,或者其他。 2 如果允许分隔符换行,是否有些符号只能在行末,不能在行首。有些则只能在行首,不能行末? 3 换行之后,原先的空格是不是就不要了? 4 是否会出现,插入连字符的需求?比如碰巧有个十几个字母的单词,加上他肯定超40,不加他,前面行才20几个字母。印刷业的话,是直接用连字符断开。当然,互联网很少这样子搞,楼主这里怎么说? 5 超过40个字母的单词有可能吗? 6 第27行用什么,AA?
还是楼上最仔细了 要真的搞的话 问题有很多的
shine333 2014-06-26
  • 打赏
  • 举报
回复
几个问题 1 换行的依据是什么?空格肯定算了,其他符号呢,比如连字符,或者其他。 2 如果允许分隔符换行,是否有些符号只能在行末,不能在行首。有些则只能在行首,不能行末? 3 换行之后,原先的空格是不是就不要了? 4 是否会出现,插入连字符的需求?比如碰巧有个十几个字母的单词,加上他肯定超40,不加他,前面行才20几个字母。印刷业的话,是直接用连字符断开。当然,互联网很少这样子搞,楼主这里怎么说? 5 超过40个字母的单词有可能吗? 6 第27行用什么,AA?
满无解 2014-06-26
  • 打赏
  • 举报
回复
class AddBigChar
{
	public static void main(String[] args) 
	{
		String str = "Flanked by climate change campaigner and former US vice-president Al Gore, Mr Palmer announced his party would vote against the Government's bid to abolish the Clean Energy Finance Corporation, the Renewable Energy Target and the Climate Change Authority.";
		int length = 38;
		StringBuilder sb = new StringBuilder();
		for(int i=0; i<str.length()/length+1; i++){
			if(i==0){
				sb.append(((char)(65+i))+" "+new String(str.substring(0,length-1))+"\n");
			} else if(i==(str.length()/length)) {
				sb.append(((char)(65+i))+" "+new String(str.substring(i*length-1, str.length()-1))+"\n");
			} else {
				sb.append(((char)(65+i))+" "+new String(str.substring(i*length-1, (i+1)*length-1)+"\n"));
			}
		}
		System.out.println(sb.toString());
	}
}
zgycsmb 2014-06-26
  • 打赏
  • 举报
回复
7楼的应该可以了吧. 学习接分.
tony4geek 2014-06-26
  • 打赏
  • 举报
回复
高手在人间了,学习了

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧