자바 배열을 이용한 아이디 저장 인데 에러가 나옵니다.

조회수 1384회

밑에 코드는 자바 배열을 이용한 로그인 하고 그걸 보여주는 프로그램 인데요, 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); 
    } 

}

결과가 이렇게 나옵니다.

이미지

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • } 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;
    }
    

    이 부분에서 첫 번째 스캔에 아이디, 두 번째 스캔에 행 번호, 세 번째 스캔에 열 번호를 받게 하신거 같은데요. 코드를 보니 행 번호와 열 번호 모두 cid라는 변수에 들어가네요. 그래서 입력된 열 번호를 기준으로 좌표 1, 1, 2, 2, 3, 3에만 데이터가 입력되는것처럼 보이는게 아닐까요?

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)