1. img 파일 확인
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | // 파일명을 입력받아서 그림 파일이 맞는지 여부를 출력하세요. // 그림 파일의 확장자 : jpg , gif , png // gif : 색상 216 표현 System.out.println("파일을 입력하세요"); boolean bImg = false; String[] strImgCheck = {".jpg",".gif",".png"}; String str = scanner.next(); //str = "sdf.jpag"; for(String strImg : strImgCheck) if( str.lastIndexOf(strImg) > 0 ) bImg = true; if(bImg ==true) System.out.println("그림 파일 O "); else System.out.println("그림 파일 X "); | cs |
2. 핸드폰 번호
1 2 3 4 5 6 7 8 9 10 11 12 13 | //해드폰 번호를 분리된 상태로 받아서 //(지역번호)1234-5678식으로 합쳐서 출력 후 //(지역번호)12345678형태로 분해 출력 String[] telarr = {"010","1234","5678"} ; String tel =""; for(String t : telarr) { tel += t+"-"; } tel = tel.replace(telarr[0]+"-", "("+telarr[0]+")" ); System.out.println("번호 : "+tel.substring(0 , (tel.length()-1))); System.out.print("번호 : "+tel.replace("-","")); | cs |
3.문장의 공백 개수 및 위치 확인 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | //문장을 입력받아 문장안에 포함된 공백의 개수와 공백의 위치를 출력하시오 String str0 = "0123456789_123456789-123456789=123456789"; // 1234567 8901 234 56 7890 123 4567 String str = "2023년 9월은 추석이 포함되어 있는 달입니다."; // 01234567 89 0123 456789 012 345 67 //공백 : 5 int nCnt = 0; int Check = -1; int CheckPos = 0; int CheckCnt = 0; String strCheck = str; System.out.println(str); System.out.println(str.length()); System.out.println(); //1. 찾으면 줄려가면서 확인 while(true) { strCheck = strCheck.substring( Check + 1 , strCheck.length()); //나의 위치부터 출력하기때문에 찾은 값보다 한칸 위를 찾는다. Check = strCheck.indexOf(" "); // System.out.println(strCheck); // System.out.println(Check); if( Check > 0) { CheckCnt++; //배열기준 위치는 실제보다 1작다 //위치를 shift하면서 체크하기 때문에 중첩한다. CheckPos += Check +1; } if( Check < 0 || nCnt > str.length()) break; //System.out.println(); System.out.println( CheckCnt +"번째 위치 : " + CheckPos ); nCnt++; } System.out.println(); System.out.println("총개수 : " + CheckCnt ); //출력 : 공백의 개수 , 공백의 위치 | cs |
3.문장의 공백 개수 및 위치 확인 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //문장을 입력받아 문장안에 포함된 공백의 개수와 공백의 위치를 출력하시오 String str = "2023년 9월은 추석이 포함되어 있는 달입니다."; // 01234567 89 0123 456789 012 345 67 System.out.println(str); System.out.println(str.length()); System.out.println(); String[] strArr1 = str.split(" "); System.out.println("개수 : " + (strArr1.length-1)); int nPos = 0; for(int i =0; i < strArr1.length - 1; i++) { //맨뒤는 없지만 배열에 들어간다. System.out.println(" 위치 : " + (nPos += strArr1[i].length()+1)); //배열이라 +1 } | cs |
4. 영어, 숫자 ,_ 만 사용
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | // 정규식 X // 영문자와 숫자만 입력 // 아이디 : 영문자 , 숫자 , _ 허용 //System.out.println("id를 입력하세요 "); //String str = scanner.next(); String str ="zxcvbnmasdfghjklqwertyuiopZXCVBNMASDFGHJKLQWERTYUIOP0123456789_AZaz09_"; System.out.println("ID : " + str); System.out.println(); boolean bCheck = true; System.out.print("A : " + (int)'A'); System.out.println(" ~ Z : " + (int)'Z'); System.out.print("a : " + (int)'a'); System.out.println(" ~ z : " + (int)'z'); System.out.print("0 : " + (int)'0'); System.out.println(" ~ 9 : " + (int)'9'); System.out.println("_ : " + (int)'_'); System.out.println("---------------------------" ); System.out.println("- : " + (int)'-'); System.out.println(" " ); char ch1_Es = 'A'; char ch2_Ee = 'Z'; for(int i =0;i<str.length();i++) { char chStar = str.charAt(i); //1. 범위에 들어오면 true 아니면 false // if( ((int)'A' <= (int)chStar && (int)'Z' >= (int)chStar) || // ((int)'a' <= (int)chStar && (int)'z' >= (int)chStar) || // ((int)'0' <= (int)chStar && (int)'9' >= (int)chStar) || // (int)'_' == (int)chStar) { // //System.out.println("1 : " +chStar +"("+(int)chStar+")"); // bCheck =true; // } // else { // System.out.println("2 : " +chStar +"("+(int)chStar+")"); // bCheck =false; // break; // } //범위에 안들어오면 false truen if( ((int)'A' > (int)chStar || (int)'Z' < (int)chStar) & ((int)'a' > (int)chStar || (int)'z' < (int)chStar) & ((int)'0' > (int)chStar || (int)'9' < (int)chStar) & (int)'_' != (int)chStar) { System.out.println("사용불가 : " +chStar +" ("+(int)chStar+")"); bCheck =false; break; } } if(!bCheck) { System.out.println("ID생성 실패"); } else System.out.println("ID생성 O"); | cs |
5. 로또 번호 (중첩 제거 )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | // 중복배제시키기 // 임의의 난수 6개 발생 , 숫자6개 입력받아서 당첨 여부 확인하기 // 보너스 1개 // 45 // 1~ 45 로또 int[] Lotto = new int[6]; int[] Lottochoice = new int[6]; for(int i=0; i<Lotto.length;i++ ) { Lotto[i] = (int)(Math.random() * 45) +1; //기존에 값이 존재하는지 확인 for(int j = 0 ; j <i; j++) { if(Lotto[j] == Lotto[i]) { i--; //존재하면 다시 진행 break; } } } int rand = (int)(Math.random() * 45) +1; for(int i=0; i<Lottochoice.length;i++ ) { System.out.println("로또 번호를 입력하세요 (1~45)"); Lottochoice[i] = scanner.nextInt(); //Lottochoice[i] = (int)(Math.random() * 45) +1; if(Lottochoice[i] <= 0 || Lottochoice[i] > 45) { System.out.println("범위에 맞게 다시 입력하세요 (1~45)"); i--; continue; } //기존에 값이 존재하는지 확인 for(int j = 0 ; j <i; j++) { if(Lottochoice[j] == Lottochoice[i]) { System.out.println("같은 번호를 입력하셨습니다. 다른 번호를 선택해주세요"); i--; //존재하면 다시 진행 break; } } } int nCnt =0; for(int i : Lotto) { for(int j : Lottochoice) { if(i == j) { System.out.println("!!!"); nCnt++; } } } System.out.println("로또 번호는 "); for(int i : Lotto) { System.out.print(i +" "); } System.out.println(); System.out.println("선택하신 번호는 "); for(int i : Lottochoice) { System.out.print(i +" "); } System.out.println(); if(nCnt == 6){ System.out.println("축하드립니다. "+nCnt+"개가 맞았습니다."); System.out.println("자동차에 당첨되셨습니다. "); } else if (nCnt == 5){ System.out.println("축하드립니다. "+nCnt+"개가 맞았습니다."); System.out.println("냉장고에 당첨되셨습니다. "); } else if (nCnt == 4){ System.out.println("축하드립니다. "+nCnt+"개가 맞았습니다."); System.out.println("컴퓨터에 당첨되셨습니다. "); } else if (nCnt == 3){ System.out.println("축하드립니다. "+nCnt+"개가 맞았습니다."); System.out.println("무선마우스에 당첨되셨습니다. "); } else if (nCnt == 2){ System.out.println("축하드립니다. "+nCnt+"개가 맞았습니다."); System.out.println("커피쿠폰에 당첨되셨습니다. "); } else{ System.out.println(nCnt+"개가 맞았습니다."); System.out.println("다음 기회에..."); } | cs |
첫댓글 3번 배열에 넣어서 처리하니 많이 스탭이 줄었네요. 좋습니다.
다양한 알고리즘 처리 잘 보았어요. 수고하셨습니다.