此代碼的目標是接受用戶輸入並將它們顯示爲單個記錄。我編寫了一個菜單,後面跟着2個方法,set和display,都使用Scanner類來填充arrayList,但不能使用display方法來打印它們。從不同功能打印ArrayList
這裏是代碼,請告訴我如何解決這個問題。
import java.io.*;
import java.util.*;
public class Records2 {
static ArrayList information = new ArrayList();
public static void main(String[] args) {
int choice;
do {
System.out.println("1.Add \n 2.Delete \n 3.Update \n 4.Show \n Exit");
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//int choice;
System.out.println("Enter your Choice : ");
Scanner sc = new Scanner(System.in);
choice = sc.nextInt();
switch (choice) {
case 1: System.out.println("Getting ready to Add a Record ");
set();
break;
case 2: System.out.println("Getting ready to Delete a Record ");
//delete();
break;
case 3: System.out.println("Getting ready to Update a Record ");
//update();
break;
case 4: System.out.println("Here is your record ");
display();
break;
case 5: System.out.println("Out we go.");
System.exit(0);
//exit();
break;
default: System.out.println("Try again");
break;
}
} while (choice > 5 || choice < 1);
}
public static void set() {
System.out.println("Please enter the ID, Name and Address of the person \n");
Scanner readId = new Scanner(System.in);
int ID = readId.nextInt();
Scanner readName = new Scanner(System.in);
String name = readName.nextLine();
Scanner readAddress = new Scanner(System.in);
String address = readAddress.nextLine();
//ArrayList information = new ArrayList();
information.add(ID);
information.add(name);
information.add(address);
}
public static void display() {
System.out.println("here is your list" + information);
//how to differentiate from one set to another?
}
}
你怎麼卡住了?你打算列出清單的哪個位置?爲什麼不能用for循環打印出信息ArrayList? –
打印您的錯誤堆棧。 –
預期顯示選項#4的選項是什麼?最後設定的結果? – sp00m