자바 배열을 이용한 아이디 저장 코드인데 출력이 잘 안됩니다.
밑에 코드는
자바 배열을 이용한 로그인 하고 그걸 보여주는 프로그램 인데요,
2째줄에 넣으면 첫째줄로 입력되고
어쩔때는 3번째로 넘어가네요.
마지막에 서치 항목은 아예 에러가 나네요,
코드에는 에러가 없다고 나오는데 뭐가 문제일까요?
public static void main(String[] args) { //varaible declaration int i,choice,lid,cid,f; String uid,chid,sid; String l1[]=new String[5]; String l2[]=new String[6]; String l3[]=new String[5]; String l4[]=new String[5]; for(i=0;i<4;i++) { l3[i]="empty"; } for(i=0;i<3;i++) { l4[i]="empty"; } do { f=0; //status of the machines System.out.println("LAB STATUS"); System.out.println("Lab # Computer Stations"); System.out.print("1 "); for(i=0;i<5;i++) { System.out.print((i+1)+": "+l1[i]+" "); } System.out.println(); System.out.print("2 "); for(i=0;i<6;i++) { System.out.print((i+1)+": "+l2[i]+" "); } System.out.println(); System.out.print("3 "); for(i=0;i<4;i++) { System.out.print((i+1)+": "+l3[i]+" "); } System.out.println(); System.out.print("4 "); for(i=0;i<3;i++) { System.out.print((i+1)+": "+l4[i]+" "); } System.out.println(); Scanner sc=new Scanner(System.in); System.out.println("MAIN MENU"); System.out.println("0) Quit"); System.out.println("1) Simulate login"); System.out.println("2) Simulate logoff"); System.out.println("3) Search"); choice = sc.nextInt();//asking for user choice if(choice == 0) { System.exit(0); } else if(choice == 1) { //taking input from user System.out.println("Enter the 5 digit ID number of the user logging in:"); uid=sc.next(); System.out.println("Enter the lab number the user is logging in from (1-4):"); cid=sc.nextInt(); System.out.println("Enter computer station number the user is logging in to (1-6):"); cid=sc.nextInt(); if(cid==1) { l1[cid-1]=uid; } else if(cid==2) { l2[cid-1]=uid; } else if(cid==3) { l3[cid-1]=uid; } else l4[cid-1]=uid; } else if(choice == 2) { //taking input from user System.out.println("Enter the 5 digit ID number of the user to find:"); chid=sc.next(); for(i=0;i<5;i++) { if(l1[i].equalsIgnoreCase(chid)) l1[i]="empty"; } for(i=0;i<6;i++) { if(l2[i].equalsIgnoreCase(chid)) l2[i]="empty"; } for(i=0;i<4;i++) { if(l3[i].equalsIgnoreCase(chid)) l3[i]="empty"; } for(i=0;i<3;i++) { if(l4[i].equalsIgnoreCase(chid)) l4[i]="empty"; } System.out.println("User "+chid+" is logged off."); } else if(choice == 3) { //taking input from user System.out.println("Enter the 5 digit ID number of the user to find:"); sid=sc.next(); for(i=0;i<5;i++) { if(l1[i].equalsIgnoreCase(sid)) { System.out.println("User "+sid+" logged in lab 1 in system "+(i+1)); f=1; break; } } for(i=0;i<6;i++) { if(l2[i].equalsIgnoreCase(sid)) { System.out.println("User "+sid+" logged in lab 2 in system "+(i+1)); f=1; break; } } for(i=0;i<4;i++) { if(l3[i].equalsIgnoreCase(sid)) { System.out.println("User "+sid+" logged in lab 3 in system "+(i+1)); f=1; break; } } for(i=0;i<3;i++) { if(l4[i].equalsIgnoreCase(sid)) { System.out.println("User "+sid+" logged in lab 4 in system "+(i+1)); f=1; break; } } if(f==0) { System.out.println("None of the user Id is not logged into any computer station"); f=0; } } } while(choice!=0); } }
결과가 이렇게 나옵니다.
MAIN MENU
0) Quit
1) Simulate login
2) Simulate logoff
3) Search
1
Enter the 5 digit ID number of the user logging in:
ansdy
Enter the lab number the user is logging in from (1-4):
2
Enter computer station number the user is logging in to (1-6):
1
LAB STATUS
Lab # Computer Stations
1 1: ansdy 2: null 3: null 4: null 5: null
2 1: null 2: null 3: null 4: null 5: null 6: null
3 1: empty 2: empty 3: empty 4: empty
4 1: empty 2: empty 3: empty
MAIN MENU
0) Quit
1) Simulate login
2) Simulate logoff
3) Search
1
Enter the 5 digit ID number of the user logging in:
mfmfm
Enter the lab number the user is logging in from (1-4):
2
Enter computer station number the user is logging in to (1-6):
3
LAB STATUS
Lab # Computer Stations
1 1: ansdy 2: null 3: null 4: null 5: null
2 1: null 2: null 3: null 4: null 5: null 6: null
3 1: empty 2: empty 3: mfmfm 4: empty
4 1: empty 2: empty 3: empty
첨부 | 파일 크기 |
---|---|
![]() | 77.1 KB |
에러 메시지는 없는데, 원하는 대로 동작하지 않는다면
에러 메시지는 없는데, 원하는 대로 동작하지 않는다면 경고(Warning)메시지도 잘 읽어보세요.
도움 될 때가 많습니다.
세벌 https://sebuls.blogspot.kr/
choice == 1 아래 부분에...
uid, cid, 그 다음에 chid 값을 입력받으셔야 하는거 아닐까요? ^^;
cid 값을 두 번 받으면서 본래 chid 값이 들어갈 것이 cid 로 들어가서 엉뚱한 곳에 입력된 것 같습니다.
검색을 하실 때 오류는 l1 배열과 l2 배열에 값이 입력되지 않았을 때 실행하셨을 것 같습니다. l1, l2에 대한 초기화도 추가하신다음에 실행해보시면 어떨까요?
배열값을 할당하실 때,
l1.fill("empty");
라고 쓰셔도 for 문 안 쓰시고 초기화 하실 수 있으실 겁니다.
이렇게 말인가요?
else if(choice == 1) {
//taking input from user
System.out.println("Enter the 5 digit ID number of the user logging in:");
uid=sc.next(); System.out.println("Enter the lab number the user is logging in from (1-4):");
cid=sc.nextInt(); System.out.println("Enter computer station number the user is logging in to (1-6):");
chid=sc.nextLine();
이렇게 해봤는데 아예 랩실 번호 다음이 입력이 안되네요.
제가 잘 못 이해하고 있는 건가요?
chid=sc.next();
로 해도 원하는 랩 하고 열에 안들어가져서요...
저도 소스코드를 어떤 의도로 작성하신 것인지 몰랐네요.
네. 제가 말씀드린대로 하셨는데, chid 가 String 인 것을 이제 봤네요.
변수 이름이 무엇을 의미하는지 잘 모르고 있었는데, lid 가 l1, l2, l3, l4를 선택하는 것이고, cid 가 l1[i], l2[i], l3[i], l4[i] 의 i 값으로 쓰시려고 선언하셨나보네요.
그럼 해당 소스코드를 이렇게 써야 하지 않을까요?
연습삼아 만드시는 것이겠지만, 코드를 잘 정리해서 써주시고, 어떤 이유로 썼는지 주석을 달아두시는 것이 좋습니다. :)
추가로 그 밑에 if 문들도...
라고 쓰셔야겠네요...
감사합니다.
다음에 올릴때는
주석으로 설명 달아서 올리겠습니다!!!
댓글 달기