我的問題很簡單。當輸入== 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