Closed. 这个问题需要调试细节 /help/minimal-reproducible-example 。目前不接受答案。
我正在编写代码,但遇到了问题,我的代码在输出前面打印了一个新行。我想避免这种情况。
这是我的代码
if (movieName.length() > 44)
{
movieName = movieName.substring(0, 44);
}
if(frqName.equals(movieName) && frqRating.equals(movieRating))
{
System.out.print(" " + movieTime);
}
else
{
System.out.println();
System.out.printf("%-44s | %5s | %s", movieName, movieRating, movieTime);
}
frqName = movieName;
frqRating = movieRating;
}
}
}
这是我的输出 - 首先是不需要的空行:
Wonders of the World | G | 16:40 20:00
Journey to Space | PG-13 | 19:00
Buffalo Bill And The Indians or Sitting Bull | PG | 12:45 15:00 19:30
Adventure of Lewis and Clark | PG-13 | 10:00 14:30
Halloween | R | 19:00
我认为问题出在第一个打印语句上;但我该如何正确修复它呢?
我已经尝试删除System.out.println();
来自else
部分并将换行符附加到下一行的格式字符串中。但是同一动作的进一步显示时间将打印在下一行,这仍然更糟。
写吧:
if (frqName.equals(movieName) && frqRating.equals(movieRating)) {
System.out.println(" " + movieTime);
} else {
System.out.printf("%-44s | %5s | %s \n", movieName, movieRating, movieTime);
}
...并且您的输出不会以空行开头。
我也会(虽然我不知道你的意图)删除" " +
before movieTime
在新的System.out.println()
System.out.println(movieTime);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)