[英]Can someone help me understand why I need an IF and WHILE for this answer?
[英]Print string in maxlines 3 and width parametar, I'm not understand all, can someone help me
我們目前正致力於在周視圖和月視圖中向我們的用戶顯示日歷事件。 日歷事件可能類似於“9:00 John Doe 直到 10 30 在辦公室”。 不幸的是,我們只有有限的屏幕空間來顯示有關每個事件的信息。 我們只能顯示有限的最大字符數的幾行。 請編寫一個帶有方法的類,該方法獲取摘要字符串並將其分解為最大行數(maxLines 參數)和每行最大字符數(寬度參數)。 除了第一行之外的所有行都應該縮進(它們應該以空格字符開頭),但縮進行中的第二個字符不應是另一個空格(因此不能包含第二個空格)。
示例(文本 =“9:00 John Doe 直到 10 30 在辦公室”,width=7,maxLines = 3):9:00 John Doe 直到
包com.TnationChallange;
導入 java.util.ArrayList; 導入 java.util.List;
公共課主要{
public static void main(String[] args) {
for (String part : getParts("limited screen space to display information", 7, 3)) {
System.out.println(part);
}
}
private static List<String> getParts(String string, int partitionSize, int maxLine) {
List<String> parts = new ArrayList<String>();
int len = string.length();
for (int i = 0; i < len; i += partitionSize) {
parts.add(string.substring(i, Math.min(len, i + partitionSize)));
maxLine++;
if (maxLine == 3) {
break;
}
}
return parts;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.