英文:
Getting ArrayList printed to another method
问题
我对于如何从另一个方法获取ArrayList,并在另一个方法中打印它感到非常困惑。所以我的问题在于,我的代码是关于学生注册系统的,其中有两个方法,分别是管理员方法和学生方法。从管理员方法中,我们可以添加我们想要的数组,当我们在管理员方法中打印出来时,一切正常。但是当我尝试在学生方法中打印出来时,似乎不起作用。以下是代码部分:
import java.util.*;
public class myStudent {
// ...(你的代码在这里)
public static void main(String[] args){
myStudent obj0 = new myStudent();
obj0.login();
}
// ...(你的代码在这里)
void showMenuAdmin(){
// ...(你的代码在这里)
}
void showMenuStudent(){
// ...(你的代码在这里)
}
// ...(你的代码在这里)
}
这里是从开始到结果的输出:
输出:
学生注册
--登录--
1)以学生身份登录
2)以管理员身份登录
3)退出
>2
--登录--
管理员ID:123
管理员密码:321
--欢迎--
A. 创建课程
B. 删除课程
C. 修改课程
D. 显示课程
E. 登出
输入选项> A
创建课程(最多为6):CS123
成功添加课程!
--欢迎--
A. 创建课程
B. 删除课程
C. 修改课程
D. 显示课程
E. 登出
输入选项> E
成功登出。谢谢!
学生注册
--登录--
1)以学生身份登录
2)以管理员身份登录
3)退出
>1
--登录--
学生ID:123
学生密码(默认为身份证号码):321
[]输入选项>
// 在这一点上,我不知道为什么它会运行成这样,我只是想打印出管理员在数组中输入的内容,但似乎学生方法会重新运行管理员方法,即使我没有调用它,并且还显示数组为空。哪里出了错?或者我在这里漏掉了什么?
英文:
I'm super confuse because how is it possible to get ArrayList from another method, and print it to another method? So my problem here is, my code is about student registration system where it have 2 methods which are admin and student, so from admin method, we can add arrays we want and when we print that out in admin method, it works fine. But when I try to print that out in student method, seems to be not working. Here's the code:
import java.util.*;
public class myStudent {
boolean logins = false;
int myId;
String addCourse="No Record";
String NewCourse;
String removeCourse="No Record";
int modifyCourse;
String password = null;
ArrayList test = new ArrayList();
public static void main(String[] args){
myStudent obj0 = new myStudent();
obj0.login();
}
void login(){
myStudent obj1 = new myStudent();
Scanner myScanner = new Scanner(System.in);
System.out.println(" **Student Registration**");
System.out.println("\t--LOGIN--");
System.out.println("\n1)Login as student \n2)Login as admin \n3)Exit");
System.out.print(">");
int optionMenu = myScanner.nextInt();
if(optionMenu == 1){
obj1.showMenuStudent();
}
else if(optionMenu == 2){
obj1.showMenuAdmin();
}
else if(optionMenu == 3){
System.out.println("THANK YOU!");
System.exit(0);
}
}
void showMenuAdmin(){
if (!logins) {
//String myPass;
Scanner myScanner = new Scanner(System.in);
System.out.println("\n--LOGIN--");
try{
System.out.print("Admin ID : ");
myId = myScanner.nextInt();
}
catch(Exception e){
System.out.println("INVALID ID!");
System.exit(0);
}
System.out.print("Admin Password : ");
password = myScanner.next();
logins = true;
}
char option;
Scanner myScanner = new Scanner(System.in);
System.out.println("\n--WELCOME--");
System.out.println("A. Create Course");
System.out.println("B. Drop Course");
System.out.println("C. Modify Course");
System.out.println("D. Display Course");
System.out.println("E. Logout");
do
{
System.out.print("Enter An Option > ");
option = myScanner.next().charAt(0);
System.out.print("\n");
switch(option){
case 'A':
System.out.print("Create Course(Maximum is 6) :");
addCourse = myScanner.next();
test.add(addCourse);
System.out.println("\nCourse Added Successfully!");
display();
break;
case 'B':
Object[] myArray = test.toArray();
System.out.println("Current Courses List: ");
int k=1;
for(int i=0; i < myArray.length; i++){
System.out.println(k + ". " + myArray[i]);
k++;
}
System.out.println("Enter course name you want to remove :");
System.out.println("Enter 'back' to cancel");
System.out.print(">");
removeCourse = myScanner.next();
if(removeCourse!="back"){
test.remove(removeCourse);
System.out.println("\nSucceed");
display();
}else
display();
break;
case 'C':
Object[] myArray2 = test.toArray();
System.out.println("Current Courses List: ");
int m=1;
for(int i=0; i < myArray2.length; i++){
System.out.println(m + ". " + myArray2[i]);
m++;
}
System.out.println("Select course you want to modify :");
System.out.print(">");
modifyCourse = myScanner.nextInt();
try{
if(modifyCourse==1){
System.out.print("Change to :");
NewCourse=myScanner.next();
test.set(0, NewCourse);
System.out.println("Succeed!");
display();
}
else if(modifyCourse==2){
System.out.print("Change to :");
NewCourse=myScanner.next();
test.set(1, NewCourse);
System.out.println("Succeed!");
display();
}else if(modifyCourse==3){
System.out.print("Change to :");
NewCourse=myScanner.next();
test.set(2, NewCourse);
System.out.println("Succeed!");
display();
}else if(modifyCourse==4){
System.out.print("Change to :");
NewCourse=myScanner.next();
test.set(3, NewCourse);
System.out.println("Succeed!");
display();
}else if(modifyCourse==5){
System.out.print("Change to :");
NewCourse=myScanner.next();
test.set(4, NewCourse);
System.out.println("Succeed!");
display();
}else if(modifyCourse==6){
System.out.print("Change to :");
NewCourse=myScanner.next();
test.set(5, NewCourse);
System.out.println("Succeed!");
display();
}
else{
System.out.print("Invalid!");
display();
}
}
catch(Exception e){
System.out.print("Invalid, Course not available!");
display();
}
break;
case 'D':
Object[] myArray3 = test.toArray();
System.out.println("Courses List: ");
int j=1;
for(int i=0; i < myArray3.length; i++){
System.out.println(j + ". " + myArray3[i]);
j++;
}
display();
break;
}
}while(option != 'E');
System.out.println("Logged Out Successfully.Thank You!");
login();
}
void showMenuStudent(){
if (!logins) {
Scanner myScanner = new Scanner(System.in);
System.out.println("\n--LOGIN--");
try{
System.out.print("Student ID : ");
myId = myScanner.nextInt();
}
catch(Exception e){
System.out.println("INVALID ID!");
System.exit(0);
}
System.out.print("Student Password(Default is IC Number) : ");
password = myScanner.next();
logins = true;
}
Object[] myArray4 = test.toArray();
int n=1;
for (Object myArray41 : myArray4) {
System.out.println(n + ". " + myArray41);
n++;
}
}
void display(){
showMenuAdmin();
}
}
I really hope someone would understand my problem and trying to fix it.. Please and thank you
Here's the output from beginning to the result:
OUTPUT:
Student Registration
--LOGIN--
1)Login as student
2)Login as admin
3)Exit
>2
--LOGIN--
Admin ID : 123
Admin Password : 321
--WELCOME--
A. Create Course
B. Drop Course
C. Modify Course
D. Display Course
E. Logout
Enter An Option > A
Create Course(Maximum is 6) :CS123
Course Added Successfully!
--WELCOME--
A. Create Course
B. Drop Course
C. Modify Course
D. Display Course
E. Logout
Enter An Option > E
Logged Out Successfully.Thank You!
Student Registration
--LOGIN--
1)Login as student
2)Login as admin
3)Exit
>1
--LOGIN--
Student ID : 123
Student Password(Default is IC Number) : 321
[]Enter An Option >
//at this point, idk why it run like that,I just want to print out what the admin had entered in the array,but its like the student method just rerun the admin method again even i never call it, and it also shown that the array is empty. Which part is my mistake? or am i missing something here?
答案1
得分: 1
以下是翻译好的部分:
所以我运行了你的代码并找到了问题。你在每次login()
时都创建了一个新的myStudent
对象。如果你:
- 移除这行
myStudent obj1 = new myStudent();
- 将
obj1.showMenuStudent();
替换为showMenuStudent()
- 将
obj1.showMenuAdmin();
替换为showMenuAdmin();
你将会解决这个问题,并且得到你想要的输出。这是我得到的输出,希望它与你想要获得的结果同步:
**学生注册**
--登录--
1)作为学生登录
2)作为管理员登录
3)退出
>2
--登录--
管理员ID:1
管理员密码:1
--欢迎--
A. 创建课程
B. 退选课程
C. 修改课程
D. 显示课程
E. 登出
输入选项 > A
创建课程(最多为6门):CS123
课程添加成功!
--欢迎--
A. 创建课程
B. 退选课程
C. 修改课程
D. 显示课程
E. 登出
输入选项 > E
登出成功。谢谢!
**学生注册**
--登录--
1)作为学生登录
2)作为管理员登录
3)退出
>1
1. CS123
输入选项 >
英文:
So i ran your code and found the problem. You're creating a new myStudent
object at every login()
, If you:
- Remove the line
myStudent obj1 = new myStudent();
- And replace
obj1.showMenuStudent();
withshowMenuStudent()
- And replace
obj1.showMenuAdmin();
withshowMenuAdmin();
You will have fixed this issue and will get the output you want. Here is the output I got, hopefully its in sync with what you want to obtain:
**Student Registration**
--LOGIN--
1)Login as student
2)Login as admin
3)Exit
>2
--LOGIN--
Admin ID : 1
Admin Password : 1
--WELCOME--
A. Create Course
B. Drop Course
C. Modify Course
D. Display Course
E. Logout
Enter An Option > A
Create Course(Maximum is 6) :CS123
Course Added Successfully!
--WELCOME--
A. Create Course
B. Drop Course
C. Modify Course
D. Display Course
E. Logout
Enter An Option > E
Logged Out Successfully.Thank You!
**Student Registration**
--LOGIN--
1)Login as student
2)Login as admin
3)Exit
>1
1. CS123
Enter An Option >
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论