Очень простой список дел, который запрашивает ввод, а затем распечатывает список в виде ArrayList, разделенного на разделы (подсписки) (у меня действительно плохое зрение, поэтому мне приходится использовать большие шрифты, и когдасписок становится слишком длинным, проблема в том, что конец списка выходит за пределы страницы. Поскольку я могу использовать кнопки «домой» / «конец» для быстрого просмотра страницы, это не оптимальная ситуация. Я бы лучше сломал ArrayListв подсписки и распечатайте подсписки, по одному в каждой строке, как показано ниже:
Вот список текущих дел на сегодня: [Проснись, выгуливай собаку, съешь завтрак] [Убери постель, подмети назад, Изучение Java]
import java.util.Scanner; import java.util.ArrayList;
/**
* @author Troy
*
*/
public class HelloWorld {
public static void main(String[] args) {
// I chose an ArrayList because the size does not have to be predetermined.
ArrayList<String> to_do = new<String>ArrayList();
System.out.println("What would you like to add to your to-do list?");
Scanner user_input = new Scanner(System.in);
//While the user_input still has entries, perform the following:
while (user_input.hasNextLine()) {
//Add next entry in the to-do list(user_input) to the ArrayList
String input = user_input.nextLine();
//If input = remove, remove the last item in the to_do list.(ArrayList)
if ("remove".equals(input)) {
if (to_do.size() > 0) {
to_do.remove(to_do.size() -1);
}}
/**If the user types in "exit", when prompted for the next item in their
* to_do list, close user_input, and print out...
*/
if ("exit".equals(input)) {
user_input.close();
System.out.println("Your to-do list is complete!");
ArrayList<String> sect1 = new ArrayList<String>(to_do.subList(0, to_do.size()));
if (to_do.size() <= 5) {
System.out.println(sect1 + "\n");
break;
}
ArrayList<String> sect2 = new ArrayList<String>(to_do.subList(6, to_do.size()));
if (to_do.size() > 5 && to_do.size() <=10) {
System.out.println(sect1 + "\n" + sect2);
break;
}
//If input does NOT equal "remove", add user_input to the to_do list.
if (!"remove".equals(input)) {
to_do.add(input);
}
System.out.println("\n");
/**Print the ArrayList called "to_do" split into sections AFTER writing,
* "Here is today's to-do list:"
* */
System.out.println("Here is today's to-do list: " + "\n");
if (to_do.size() <= 5) {
System.out.println(sect1 + "\n");
}
if (to_do.size() > 5 && to_do.size() <=10) {
System.out.println(sect1 + "\n" + sect2);
}
}
}
}}