本来是一个不难的题目,由于自己没有分析好,导致出现了一些问题。
问题:
利用split(" "),这样利用单个空格字符去分割字符串,导致得到了很多空字符。?
解决方法:
利用正则表达式,split("\s+") ,这样可以一次分割单个或多个空白字符。?
除了这个空格,还有就是udebug中的,就也有前面是空格,所以我就用了trim()去掉了首尾的空格
还有就是对in.hasNextLine以及对应的文件结束符EOF有了更深刻的认识.
debug的时候利用ctrl+z的时候一直不成功的痛苦,希望我能铭记。因为这个题的主要是时间都花在这上面了…
以下是原理的解释
https://blog.csdn.net/u010632454/article/details/9324869
提前看过题目评论,知道每行的最后一个不要进行补充或者加空格。
public class Uva1593 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = "";
int[] max = new int[250];
LinkedList<String[]> result = new LinkedList<>();
Arrays.fill(max, 0);
while(in.hasNextLine())
{
str = in.nextLine();
/*先读取一行 在分开处理*/
if(str.equals(""))
str = in.nextLine();
/*去除首尾空格*/
str = str.trim();
String[] word = str.split("\\s+"); /*匹配一个或多个空格 正则表达式\s表示匹配任何空白字符,+表示匹配一次或多次*/
for(int i =0;i<word.length;i++)
{
if(max[i]<word[i].length())
{
max[i] = word[i].length();
}
}
/*str = in.nextLine();*/
result.add(word);
}
/* System.out.println();*/
/*输出部分*/
for(int i =0;i<result.size();i++)
{
String[] temp = result.get(i); /*这是一行的内容 */
for(int j =0;j<temp.length;j++)
{
System.out.print(temp[j]);
if(/*i==result.size()-1&&*/j==temp.length-1) /*最后一个单词不需要这样*/
continue;
for(int space = max[j]-temp[j].length();space>0;space--)
{
System.out.print(" ");
}
System.out.print(" ");
}
System.out.println();
}
in.close();
}
}
总结
现在在慢慢调整状态,还是觉得做题很有意思,思考也有意思hhh
经常拿做题来逃避做其他的事情,但是这个不是很好的事情噢
实验还是得做,项目也要继续,毕竟是对以后有益的事情,不能只看眼前哈。
现在要去挑战例题了!加油!✔ (那几个表情,只是因为没放过图,所以试试,太蠢了的表情,我才不会用