我的问题很简单。当输入== 1时发送x的值时,该方法应该将x的值添加到al ArrayList中。它这样做,但是当我尝试打印al的值时,它会打印出空的。Java ArrayList打印出空的?
第一类:
import java.util.Scanner;
public class ToDo {
Scanner s = new Scanner(System.in);
Scanner g = new Scanner(System.in);
void start() {
IncompleteTasks incompletetasks = new IncompleteTasks();
CompleteTasks completetasks = new CompleteTasks();
AllTasks alltasks = new AllTasks();
int input = 999;
String x = "";
while (input != 0) {
System.out.println("What would you like to do? Type '0' to cancel");
System.out.println();
System.out.println("1. Add a task"); // done
System.out.println("2. View current tasks");
System.out.println("3. Delete a task"); // done
input = s.nextInt();
if (input == 1) {
while (!x.equals("quit")) {
System.out.print("Enter a task: (Type 'quit' to finish! ");
x = g.nextLine();
if (x.equals("quit")) {
start();
}
else {
incompletetasks.IncompleteTasksAdd(x);
}
}
}
else if (input == 2) {
System.out.println("\t1. All Tasks");
System.out.println("\t2. Complete Tasks");
System.out.println("\t3. Incomplete Tasks");
input = s.nextInt();
if (input == 1) {
alltasks.AllTasks();
}
else if (input == 2) {
completetasks.CompleteTasks();
}
else if (input == 3) {
incompletetasks.IncompleteTasksDisplay();
}
else if (input == 0) {
System.exit(0);
}
else {
System.out.println("\t\t\t\tInvalid choice! Try again!");
start();
}
}
else if (input == 3) {
System.out.println("hello");
x = s.nextLine();
incompletetasks.IncompleteTasksDelete(x);
}
else if (input == 0) {
System.exit(0);
}
else {
System.out.println("\t\t\t\tInvalid choice! Try again!");
start();
}
}
}
}
二等:
import java.util.ArrayList;
import java.util.Scanner;
public class IncompleteTasks {
int counter = 0;
Scanner g = new Scanner(System.in);
ArrayList<String> al = new ArrayList<String>();
void IncompleteTasksDisplay() {
System.out.println("----------------------------------------------------------");
for (String f : al) {
counter++;
System.out.println(f);
// System.out.println(counter+". " +al.get(f));
}
System.out.println("----------------------------------------------------------");
}
void IncompleteTasksAdd(String x) {
al.add(x);
System.out.println("\tTask Added!");
}
void IncompleteTasksDelete(String x) {
for (int k = 0 ; k < al.size() ; k++) {
if (al.get(k) == x) {
al.remove(x);
}
}
}
}
结果:
What would you like to do? Type '0' to cancel
1. Add a task
2. View current tasks
3. Delete a task
1
Enter a task: (Type 'quit' to finish! Test
Task Added!
Enter a task: (Type 'quit' to finish! Test 1
Task Added!
Enter a task: (Type 'quit' to finish! Test 2
Task Added!
Enter a task: (Type 'quit' to finish! quit
What would you like to do? Type '0' to cancel
1. Add a task
2. View current tasks
3. Delete a task
2
1. All Tasks
2. Complete Tasks
3. Incomplete Tasks
3
----------------------------------------------------------
----------------------------------------------------------
What would you like to do? Type '0' to cancel
1. Add a task
2. View current tasks
3. Delete a task
我是新来的Java,所以不感到惊讶,如果我有坏的编码在其他部分或如果我失去了一些非常明显的东西。我感谢所有我能得到的帮助,因为我很难过,我不知道我做错了什么。
我不是专家,但为什么不使用开关盒,而不是如果..如果.. – user562 2014-10-11 05:18:50
我听说过一个开关盒,但我还没有学会它,但我的优先事项,所以我会学习它很快。感谢您的建议。 – Tacent 2014-10-11 05:20:04
在调用nextInt()之后使用nextLine()。那么你将不需要两个Scanner实例。例如'input = s。nextInt(); s.nextLine();' – Niroshan 2014-10-11 05:42:40