JAVA31 day03) 문자열 내장 메소드 다른 자료형과는 다르게 String은 객체를 생성할 수 있다. 따라서 String에서만 사용 할 수 있는 메서드가 여럿 있다. String str = "apple"; System.out.println(str.charAt(1)); //★★index는 [0]부터 시작! //2번째에 있는 글자를 출력 //p System.out.println(str.length()); //str의 길이를 알려주는 메서드 //5 System.out.println(str.substring(1,3)); //1이상 3미만의 글자를 출력 //pp System.out.println(str.toUpperCase()); System.out.println(str); //소문자를 대문자로 변경 //str자체에 변화를 주지 않는다! //APPLE.. 2021. 12. 23. day03) Random, 형변환, Final 1) Random 클래스 사용법: Random rand= new Random(); int num=rand.nextInt(100); //0~99중 무작위 숫자가 변수에 저장된다 숫자 맞추기 알고리즘 Scanner sc=new Scanner(System.in); Random rand= new Random(); int ans=rand.nextInt(100)+1;//정답은 랜덤으로 설정 int L=1;//낮은 숫자를 저장할 변수 int H=100;//높은 숫자를 저장할 변수 int cnt=0; while(true) { // [수행횟수] 불분명하여 while문 사용! System.out.print(L+"~"+H+"중에서 숫자하나 선택하세요. "); int num=sc.nextInt(); if(num 2021. 12. 23. day02)제어문 관련 알고리즘 1. 교환 알고리즘 //정수1입력: 10 //정수2입력: 12 //10, 11, 12 //정수1입력: 12 //정수2입력: 10 //10, 11, 12 Scanner sc = new Scanner(System.in); System.out.println("정수입력1: "); int num1 = sc.nextInt(); System.out.println("정수입력2: "); int num2 = sc.nextInt(); if(num2>num1) { //[교환]알고리즘 -> 임시저장 변수 tmp활용 int tmp=num1; num1=num2; num2=tmp; } while(num2= 1; i--) { //1부터 비교하여 가장 큰 값을 출력하는 것보다 뒤에서부터 비교하여 첫번째 값을 출력하는 것이 효율적 if(.. 2021. 12. 23. day02) 제어문(반복문) 반복문을 사용한다면 코드의 재사용성이 증가한다. -while문: 반복되는 수행의 횟수를 모를 때 사용 (무한반복, 계속, 영원히) -for문: 반복수행횟수를 분명히 알 때 사용 (N번.x회,1~10까지(범위)) 1. while문 while(조건식){ 조건식이 true일동안 계속 수행되는 공간 } →while 문의 최소 수행횟수는 0번 →최대 수행횟수는 무한+ 반드시 "종료조건"을 필요로 한다 int i=0; while(true) {//조건문에 true를 사용하여 최대 수행횟수를 무한으로 만든다 System.out.println("확인"); //조건을 만족한다면, 종료시키겠습니다. i++; if(i==3) { break;//자신과 가장 가까운 "반복문"을 빠져나옴 } } /*양수를 3개 입력 양수1 입력:.. 2021. 12. 22. 이전 1 ··· 4 5 6 7 8 다음