java 如何在java中制作一个功能性待办事项列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31152539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make a functioning to do list in java
提问by Nicholas Dry
I am trying to make a functioning to do list with a limit of 10 elements, however I am having an issue with two major things in the to do list.
我正在尝试制作一个功能限制为 10 个元素的待办事项列表,但是我在待办事项列表中遇到了两个主要问题。
The first is that after I first compile and run the program and select to add elements to the list, that functions properly, but if I add two elements and the 'stop' sentinel, when I select the next option to print the to do list, I am presented with a list, showing my two elements and then the stop sentinel along with 7 null values in the list. So the first issue I am having is to get rid of the null values, I attempted using a counter as you can see in my code however that was not proving to be effective.
第一个是,在我第一次编译并运行程序并选择将元素添加到列表后,该功能正常,但是如果我添加两个元素和“停止”哨兵,当我选择下一个选项打印待办事项列表时,我看到一个列表,显示我的两个元素,然后是停止标记以及列表中的 7 个空值。所以我遇到的第一个问题是摆脱空值,我尝试使用一个计数器,正如你在我的代码中看到的那样,但是这并没有被证明是有效的。
The next issue that I am having is that I am trying to make it so that you can add to the list, so once you select to add more things to the list, the new options the user writes, rewrites over them in the array and prints out the new values and not along with the old ones. I am assuming that can be done through some sort of recursion method but I am having a hard time figuring it out.
我遇到的下一个问题是我正在尝试将其添加到列表中,因此一旦您选择将更多内容添加到列表中,用户编写的新选项会在数组中重写它们并打印出新值而不是旧值。我假设可以通过某种递归方法来完成,但我很难弄清楚。
Any help would be greatly appreciated!
任何帮助将不胜感激!
Thanks!
谢谢!
import java.util.Scanner;
public class ToDo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int MAX = 10;
String[] list = new String[MAX];
int choice = 0;
while (choice != 3) {
System.out.println();
System.out.println("Type 1 to add a new thing to your to do list.");
System.out.println("Type 2 to print the to do list.");
System.out.println("Type 3 to exit the program.");
System.out.print("Select an option: ");
choice = input.nextInt();
int count = 0;
if (choice == 1) {
System.out.println("Keep hitting enter after to do's, if you want to stop, type 'stop'.");
for (int i=0;i<MAX;i++) {
list[i] = input.nextLine();
if (list[i].equals("stop")) break;
count++;
}
}
if (choice == 2) {
for (int index = 0;index < list.length; index++) {
System.out.println(list[index]);
}
}
}
}
}
采纳答案by Codebender
As I have mentioned in the comment, you can use an ArrayList
instead of String[]
to make your processing much easier.
正如我在评论中提到的,您可以使用ArrayList
而不是String[]
使您的处理更容易。
But if you want to use the array itself, there are 3 minor issues with your code.
但是如果你想使用数组本身,你的代码有 3 个小问题。
In your choice 1 for loop, start the loop from count
,
在您选择的 1 for 循环中,从 开始循环count
,
for (int i=count;i<MAX;i++) {
list[i] = input.nextLine();
if (list[i].equals("stop")) break;
count++;
}
In your choice 2 for loop, end the loop before reaching count
,
在您选择的 2 for 循环中,在到达之前结束循环count
,
for (int index = 0;index < count; index++) {
System.out.println(list[index]);
}
And move your count initialization outside the while loop.
并将您的计数初始化移到 while 循环之外。
int count = 0;
But beware, if you decide to implement removing tasks, this could get complicated and using ArrayList
would become much simpler.
但请注意,如果您决定实施删除任务,这可能会变得复杂并且使用ArrayList
会变得更加简单。
回答by akhil_mittal
It is better to use ArrayList
but if you still want to stick to String[]
then the following program will work for you:
最好使用ArrayList
但如果您仍然想坚持,String[]
那么以下程序将适合您:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
final int MAX = 10;
String[] list = new String[MAX];
int choice = 0;
while (choice != 3) {
System.out.println();
System.out.println("Type 1 to add a new thing to your to do list.");
System.out.println("Type 2 to print the to do list.");
System.out.println("Type 3 to exit the program.");
System.out.print("Select an option: ");
choice = input.nextInt();
String userEnteredItem;
if (choice == 1) {
System.out.println("Keep hitting enter after to do's, if you want to stop, type 'stop'.");
for (int i=0;i<MAX;i++) {
userEnteredItem = input.nextLine();
if(!userEnteredItem.isEmpty()) {
list[i] = userEnteredItem;
if (userEnteredItem.equals("stop")) {
break;
}
count++;
} else {
i--; // Do not increase index for empty item.
}
}
}
else if (choice == 2) {
for (int index = 0;index < count; index++) {
System.out.println(list[index]);
}
}
else {
input.close();
}
}
}
It keeps track of user items in static int count
and it also closes the scanner when you do not need it.
它在静态 int 中跟踪用户项目,count
并且在您不需要它时也会关闭扫描仪。
回答by paisanco
Instead of using a fixed size array of Strings, use an ArrayList of Strings. Then you can add elements to it as you go.
不使用固定大小的字符串数组,而是使用字符串的 ArrayList。然后,您可以随时向其中添加元素。
Make sure to
确保
import java.util.ArrayList;
Declaration syntax is
声明语法是
ArrayList<String> myList = new ArrayList<String>();
Add elements to your list with the add() method:
使用 add() 方法将元素添加到您的列表中:
myList.add(input.nextLine())
You don't need that inner for loop, instead break out of the while loop of input options when you've iterated through it 10 times.
您不需要那个内部 for 循环,而是在您遍历它 10 次时跳出输入选项的 while 循环。
To solve your problem of "stop" being in your list, check that the input is "stop", and stop, beforeyou attempt to add to the list.
要解决“停止”出现在列表中的问题,请在尝试添加到列表之前检查输入是否为“停止”并停止 。