split分割,然后写字母行号,然后从单词数组里面逐个拿出单词,如果该行每超出40,则在改行输出,如果超出,另起一行,再写行号,然后如上循环,直到结束。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; } }
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); } }
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()); } }
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;
}
}
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);
}
}
接收输入到一个字符串中,然后用split分割到字符串数组中。
用一个变量做行标 char line='A', int length=0
1.输出line +空格,length=2
2.判断字符串数组下一个单词的长度+length是否小于40
如果小于等于40输出他+空格
如果大于,输出换行,下标减1(这个字符串放到下次循环中判断),返回1
循环在数组所有元素都遍历完结束。
{
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());
}
}