我的问题很简单。当输入== 1时发送x的值时,该方法应该将x的值添加到al ArrayList中。它会这样做,但是当我尝试打印al的值时,它会打印出来。
头等舱:
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的新手,所以如果我在其他部分编写错误或者我错过了一些非常明显的内容,请不要感到惊讶。我很感激能得到的所有帮助,因为我很难过,我不知道自己做错了什么。
答案 0 :(得分:2)
这一行:
IncompleteTasks incompletetasks = new IncompleteTasks();
声明本地变量incompletetasks
,特定于当前对start()
的调用,并将其初始化为新的空任务列表。
这一行:
start();
创建一个新的单独调用start()
,因此会有一个单独的incompletetasks
变量,并且没有关于您incompletetasks
变量的任何信息将所有内容添加到。
start()
没有必要自称为自己。
解决此问题的最快方法是更改此内容:
while (input != 0){
到此:
mainloop: while (input != 0) {
(其中mainloop
是标签,为while循环提供一个名称,以便您可以引用它)以及几乎每次出现的情况:
start();
到此:
continue mainloop;
(粗略地说,&#34;回到名为mainloop
&#34;的循环的开头。)
更好的修复方法是将您的方法拆分为更小的部分。这将允许更清晰的代码。一般来说,通常最好不要使用嵌套循环的方法(虽然有很多例外,所以不要把它作为一个硬性规则)。