我已經搜索了互聯網上的問題的答案,我有和似乎無法得到我正在尋找的答案,我有兩個類和應用程序類和一個用戶類,我提示用戶輸入一個新用戶,或者返回已設置爲保存用戶對象的數組列表中的用戶的結果。應用程序結束後,我期望數組列表繼續存放對象,以便在應用程序類中每次連續運行主方法時,都可以引用arrayList以便稍後進行交叉檢查。在arrayList中存儲用戶創建的對象多次迭代
所以我的問題是,當主方法完成,我再次運行它,它是否重新創建所有我的對象和arrayList從頭開始?
下面是我正在使用的兩個類。首先是應用程序類,其次是用戶。
import java.util.ArrayList;
import java.util.Scanner;
public class Application{
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
//Creating admin user object that will be able to access everything
Users admin = new Users();
Users result = null;
//creating a new user that is not an admin
System.out.println("Are you a new user?");
String answer = null;
answer = in.nextLine();
if(answer.equalsIgnoreCase("YES") || answer.equalsIgnoreCase("Y")) {
result = admin.addNewUser();
result.addUsertoArrayList(result);
}else {
result.displayUsers(result.users);
}
}//End of Main Method.
}//End of Application Class
import java.util.ArrayList;
public class Users extends Application {
private String username;
private double biWeeklyIncome = 0;
private String password;
private String email;
// ArrayList to store all the objects of Type Users.
ArrayList<Users> users = new ArrayList<Users>();
// Default Constructor.
Users() {
}
// Constructor that takes a string as a name parameter.
public String name;
Users(String name) {
this.name = name;
}
// Setter Methods.
// User name
public void setUsername() {
System.out.println("Enter the username that you wish to go by:\n Ex. bigBoss45");
username = in.nextLine();
}
// Income
public void setBiWeeklyIncome() {
System.out.println("Enter your current bi-weekly income: \n Ex. 4500.00");
try {
biWeeklyIncome = Double.parseDouble(in.nextLine());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// Password
public void setPassword() {
System.out.println("Enter the password that you wish to access your account with:\n Ex. bigBoss45");
password = in.nextLine();
}
// Email
public void setEmail() {
System.out.println("Enter a valid email address: \n Ex. [email protected]");
email = in.nextLine();
}
// Getter Methods
// User name
public String getUsername() {
return username;
}
// Income
public double getBiWeeklyIncome() {
return biWeeklyIncome;
}
// Password
public String getPassword() {
return password;
}
// Email
public String getEmail() {
return email;
}
// Method to create a new user
public Users addNewUser() {
String name = null;
System.out.println("Enter the name of the new user\n Ex.John Smith");
name = in.nextLine();
// Creating the new
Users newUser = new Users(name);
// Setting the new users information
newUser.setUsername();
newUser.setPassword();
newUser.setBiWeeklyIncome();
newUser.setEmail();
//adding the new user to the users arrayList
displayUsers(users);
return newUser;
}// end of addNewUser method.
//Method that is going to add a new user to the array List.
public void addUsertoArrayList(Users nUser) {
users.add(nUser);
}
public void displayUsers(ArrayList<Users> users) {
// Printing out the user added to the array list for testing purposes.
for (Users user : users) {
System.out.println(user.getUsername());
}
}//End of displayUser method.
}
我是一種新的Java和所有的面向對象的,所以任何的幫助深表感謝,我感謝你抽出時間來看看我的代碼!
這是通過不結束程序(無限循環)完成的,如果用戶輸入「exit」,則關閉程序,否則執行迭代。如果要永久存儲值,請使用數據庫(例如,如果不想設置數據庫,則使用SQLite)。 – maraca