==Console==

==Console==

<Object>

<String>

==Console==

<String을 도와주는 아이들>

==Console==

 

==Console==

<생성자>

==Console==

==Console==

<instance변수>

<되부름 함수>

==Console==

==Console==

<Call By Value , Call By Reference>

==Console==

<상속>

==Console==

==Console==

 

==Console==

<캡슐화 = 정보의 은닉>

==Console==

<Static>

==Console==

==Console==

==Console==

 

 

 

<Bubble Sort>

==Console==

<주민번호 유효성 검사>

<주민번호 검사 로직>

 

주민번호 검사 로직 )

 

1. 주민번호 13자리중 마지막 자리를 제외하고, 앞에서부터 차례로 [2,3,4,5,6,7,8,9,2,3,4,5]를 각자리에 곱하기.

 

0 0 0 0 0 0 - 0 0 0 0 0 0         0 <=주민번호 마지막자리 숫자, 체크코드

2 3 4 5 6 7 - 8 9 2 3 4 5

 

2. 곱한 각 자리의 수를 모두 더하기.

 

3. 모두 더한 수를 11로 나눈 나머지 구하기.

   ? % 11 -> ?

4. 11 에서 구한 나머지를 빼기.

   단 구한값이 2자리가 되었을 경우(ex. 10, 11)에는 10을 나눈 나머지 값 혹은 10을 빼주어서

   주민번호의 마지막 자리의 숫자와 비교할 숫자값을 구한다.

  11 - ?   -> ?

5. 1~4번을 통해 계산한 값을 주민번호 마지막 자리수와 비교하여 같으면 유효한 주민등록번호이며, 다르면 잘못된 번호이다.

 

==Console==주민등록이 노출이 될수있어 일부러 오류를 냅니다

<2차원 배열>

==Console==

==Console==

 

==Console==

==Console==

<배열을 이용하여 정렬>

==Console==

<Select Sort>

 

==Console==

<성적순으로 나열하기>

//똑같은 것을 퍼가실수 있게 소스코드로 한것임(위에 보시는것 성적순)

package com.day5;

import java.util.Scanner;

public class Test3 {

	public static void main(String[] args) {
		
			//10명 이내의 이름과 점수를 입력받아 점수가 높은 사람에서 낮은 사람 순으로 출력
		
		Scanner sc = new Scanner(System.in);
		// Scanner의 객체를 생성함.//메모리를 할당함 등등...
		
		int i,j,inwon,temp1;
		String temp2;
				
		int[] score;
		String[] name;
		
		do{
			System.out.print("인원수[1~10]? ");//3
			inwon = sc.nextInt();
		}while(inwon<1 || inwon>10);
		
		//객체 생성(메모리 할당)
		score = new int[inwon];
		name = new String[inwon];
		
		//인원수 만큼 이름과 점수 입력
		
		for(i=0;i<inwon;i++){
			
	 		System.out.print((i+1)+"번째 이름?");
			name[i] = sc.next();
			
			System.out.print("점수?");
			score[i] = sc.nextInt();
			
		}	
		
		//정렬
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				
				if(score[i]<score[j]){
					
					temp1 = score[i];
					score[i] = score[j];
					score[j] = temp1;
					
					temp2 = name[i];
					name[i] = name[j];
					name[j] = temp2;
					
				}
			}
		}
		
		//출력
		for(i=0;i<inwon;i++){
		
			System.out.printf("%6s %4d\n",name[i],score[i]);
			
		}
	
		sc.close();
	}

}

==Console==

<로또>

//소스코드

package com.day5;

import java.util.Random;

public class Test4 {

	public static void main(String[] args) {
		
		//1~45까지의 수 중 6개의 난수를 발생시켜 크기 순으로 출력
		
		Random rd = new Random(); //copy의 개념
		
		int[] num = new int[6];
		
		int i,j,su,temp;
		
		su = 0;
		
		// 반복의 횟수를 알 수 없을 때 사용
		while(su<6){
			
			num[su] = rd.nextInt(45)+1; //nextInt(45) : 0에서 44번까지의 45개 숫자를 꺼냄
			
			for(i=0;i<su;i++){
				if(num[i]==num[su]){
					su--;
					break;
				}
			}
			
			su++;

		}
		
		//정렬
		
		for(i=0;i<num.length-1;i++){
			for(j=i+1;j<num.length;j++){
				if(num[i]>num[j]){
					temp = num[i];
					num[i] = num[j];
					num[j] = temp;
											
				}
				
			}
		}
		
		
		//출력
		for(int n : num){
			System.out.printf("%4d",n);
		}
		
		System.out.println();

	}

}

==Console==난수를 발생시켜 로또번호가 나옴

<성적으로 석차를 구하기>

//소스코드

package com.day5;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {
		
			//10명 이내의 이름과 점수를 입력받아 석차(rank)를 구하시오.
		    //석차가 높은 사람에서 낮은 사람 순으로 출력
		
		Scanner sc = new Scanner(System.in);
		
		int i,j,inwon;
				
		int[] score;
		String[] name;
		int[] rank;
		
		do{
			System.out.print("인원수[1~10]? ");//3
			inwon = sc.nextInt();
		}while(inwon<1 || inwon>10);
		
		//객체 생성(메모리 할당)
		score = new int[inwon];
		name = new String[inwon];
		rank = new int[inwon];
		//인원수 만큼 이름과 점수 입력
		
		for(i=0;i<inwon;i++){
			rank[i]++;
		}
		
		for(i=0;i<inwon;i++){
			
	 		System.out.print((i+1)+"번째 이름?");
			name[i] = sc.next();
			
			System.out.print("점수?");
			score[i] = sc.nextInt();
			
		}	
		
		//석차계산
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				if(score[i]>score[j]){
					rank[j]++;
				}else if(score[i]<score[j]){
					rank[i]++;
				}
			}
		}
		
		
		//출력
		for(i=0;i<inwon;i++){
		
			System.out.printf("%6s %4d %4d\n",name[i],score[i],rank[i]);
			
		}
	
		sc.close();
	}

}

==Console==

<앞의 방법과 다르게 1등 순서대로 나오게 하기>

package com.day5;

import java.util.Scanner;

public class Test6 {

	public static void main(String[] args) {
		
			//10명 이내의 이름과 점수를 입력받아 석차(rank)를 구하시오.
			//석차가 높은 사람에서 낮은 사람 순으로 출력
		
		Scanner sc = new Scanner(System.in);
		
		int i,j,inwon,temp1;
		String temp2;
				
		int[] score;
		String[] name;
		int[] rank;
		
		do{
			System.out.print("인원수[1~10]? ");//3
			inwon = sc.nextInt();
		}while(inwon<1 || inwon>10);
		
		//객체 생성(메모리 할당)
		score = new int[inwon];
		name = new String[inwon];
		rank = new int[inwon];
		
		for(i=0;i<inwon;i++){
			rank[i] = 1;
		}
		
		//인원수 만큼 이름과 점수 입력
		
		for(i=0;i<inwon;i++){
			
	 		System.out.print("이름?");
			name[i] = sc.next();
			
			System.out.print("점수?");
			score[i] = sc.nextInt();
			
		}	
		
		//정렬
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				
				if(score[i]<score[j]){
					
					temp1 = score[i];
					score[i] = score[j];
					score[j] = temp1;
					
					temp2 = name[i];
					name[i] = name[j];
					name[j] = temp2;
										
				}
			}
		}
		
		for(i=0;i<inwon-1;i++){
			for(j=i+1;j<inwon;j++){
				
				if(score[i]>score[j]){
					
					rank[j] += 1;
				}
			}
		}
		
		//출력
		
		
		for(i=0;i<inwon;i++){
		
			System.out.printf("%6s %4d %4d등\n",name[i],score[i],rank[i]);
			
		}
	
		sc.close();
	}

}

==Console==

<입력된 값에서 가장 큰수와 작은 수 >

//소스

package com.day5;

import java.util.Scanner;

public class Test7 {

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		int i,max=0,min=0;
		int num[] = new int[5];
		
		System.out.println("5개의 정수를 입력하시오. ");
		for(i=0;i<num.length;i++){
			num[i] = sc.nextInt();
		}
		
		max = num[0];
		min = num[0];
			
		for(i=0;i<num.length;i++){
			if(max<num[i]){
				max = num[i];
			}
			if(min>num[i]){
				min = num[i];
			}
		}

		System.out.printf("가장 큰수 : %d, 가장 작은 수 : %d",max,min);
		
		sc.close();
		
	}

}

==Console==

==소스==

package com.day5;
import java.io.IOException;
import java.util.Random;
import java.util.Scanner;
public class Test9 {
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner(System.in);
		
		Random rd = new Random();
		
		int num=0, nansu;
		char ch;
		String[] str = {"가위","바위","보"}; 	
		
		
		System.out.println("---------가위바위보 게임---------");
		
		while(true){
			nansu = rd.nextInt(3);
			
			do{
				System.out.println("1: 가위, 2: 바위, 3: 보");
				System.out.println("너는 어떤거 낼래? ");
				num = sc.nextInt();
			}while(num<1||num>3);
				
			num -= 1;
			System.out.printf("컴퓨터 : %s, 사람 : %s\n"
			,str[nansu],str[num]);
			System.out.println("-------------------------------");
			
			if(nansu==num){
				System.out.println("컴퓨터와 비겼습니다.");
			}else if((num+2)%3==nansu){
				System.out.println("당신이 이겼습니다.");
			}else{
				System.out.println("컴퓨터가 이겼습니다..");
			}
			
			System.out.println("계속 하시겠습니까? ");
			ch = (char)System.in.read();
			if(ch != 'Y' && ch!= 'y'){
				break;
			}
			System.out.println("----------------------------------------");
		}
		
		sc.close();
	}
}

==Console==

<공포의 별찍기>

//똑같은 코드입니다 퍼가실수 있게 해놓았습니다.

package com.day4;

public class Test1 {

	public static void main(String[] args) {

		int i,j;
		
/*
		for(i=1;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}

*/

/*
 		for(i=1;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i*2-1;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}
	*/
/*		
 		for(i=5;i>=1;i--){
			         //i=i-1
 			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");//공백한칸				
								
			}
				
			for(j=1;j<=i*2-1;j++){
				
				System.out.print("*");
			}
			
			System.out.println();//줄바꿈
			
		}
		
	}
} // 영역은 중괄호를 더블클릭하면 알 수 있음
		*/
		
		for(i=5;i>=1;i--){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");
				
				}
			
			for(j=1;j<=i * 2 -1;j++){ // j(10)<=9
				
				System.out.print("*");
				
			}
			
			System.out.println();
			
		}
		
		for(i=2;i<=5;i++){
			
			for(j=1;j<=5-i;j++){
				
				System.out.print(" ");
				
			}
			
			for(j=1;j<=i * 2 -1;j++){
				
				System.out.print("*");
				
			}
			
			System.out.println();
			
		}
		
	}
}

 

==Console==

==Console==

 

==Console==

 

<계산기>

==Console==

<배열>

 

==추가 설명 ==

// 명령어 ~readLine(): ()로 사용해야 함. // 배열의 length만 ()를 사용하지 않음.

==Console==

 

<달력만들기>

//달력만들기 소스

package com.day4;

import java.util.Scanner;

public class Test5 {

	public static void main(String[] args) {

		
		//만년 달력
				
		Scanner sc = new Scanner(System.in);	
		
		int months[] = {31,28,31,30,31,30,31,31,30,31,30,31}; // 초기화 및 방 형성
		int y,m,nalsu,i,week;
		
		do{
			System.out.print("년도? ");//2018
			y=sc.nextInt();
		}while(y<1);
		
		do{
			System.out.print("월은? ");//12
			m=sc.nextInt();
		}while(m<1||m>12);
		
		//윤년에 따른 2월의 날수 계산
		if(y%4==0 && y%100!=0 ||y%400==0){
			months[1] = 29;
		}
		
		//1년 1월 1일부터 y-1년 12월 31일까지의 날수
		
		nalsu = (y-1) * 365 + (y-1)/4-(y-1)/100+(y-1)/400;

/*		
		int yy = (y-1)/4-(y-1)/100+(y-1)/400;
		System.out.println(nalsu);
		System.out.println(yy);
*/
		
		//월   : 1  2  3  4  5  6  7  8  9  10 11 12
		//배열 : 31,28,31,30,31,30,31,31,30,31,30,31
		//idx  : 0  1  2  3  4  5  6  7  8  9  10 11
		for(i=0;i<(m-1);i++){
			
			//nalsu = nalsu + months[i];
			nalsu += months[i];
			
		}
						
		//System.out.println(nalsu);
	
		//1년 1월 1일부터 y년 m월 1일까지의 날수
		nalsu += 1; //nalsu = nalsu + 1;
		
		//y년 m월 1일의 주의 수 계산
		
		week = nalsu%7;
		
		//System.out.println(week);
		
		System.out.println("            " + y +"-"+ m);
		
		System.out.println("\n  일  월  화  수  목  금  토");
		System.out.println("------------------------------");
		
		//공백지정
		for(i=0;i<week;i++){
			System.out.print("    ");//공백4칸
		}
		
		//월   : 1  2  3  4  5  6  7  8  9  10 11 12
		//배열 : 31,28,31,30,31,30,31,31,30,31,30,31
		//idx  : 0  1  2  3  4  5  6  7  8  9  10 11
		//해당 월의 날짜 출력
		for(i=1;i<=months[m-1];i++){
			
			System.out.printf("%4d",i);//%4d : 자릿수 4자리(4byte) 만들어줌.
			
			week++;
			if(week%7==0)
				System.out.println();
						
		}
		if(week%7!=0)
			System.out.println();
		
		System.out.println("------------------------------");

		sc.close();
		
	}
	
}

==Console==

<사용자에게 입력받아 요일까지 출력하기>

//소스코드

package com.day4;

import java.util.Scanner;

public class Test6 {

	public static void main(String[] args) {

		//년,월,일을 입력받아 요일을 출력하시오.
		//2018년 12월 3일 월요일
		
		Scanner sc = new Scanner(System.in);
		
		int months[] = {31,28,31,30,31,30,31,31,30,31,30,31};
		int y,m,d,week,nalsu,i;
		char ch[] = {'일','월','화','수','목','금','토'};
				
		do{
			System.out.println("년도를 입력하시오. ");
			y = sc.nextInt();
		}while(y<1);
		
		do{
			System.out.println("월을 입력하시오. ");
			m = sc.nextInt();
		}while(m<1 || m>12);
		
		do{
			System.out.println("일을 입력하시오. ");
			d = sc.nextInt();
		}while(d<1 || d>months[m-1]);
		
		if(y%4 ==0 && y%100!=0 || y%400==0){
			months[1] = 29;
		}
		
		nalsu =  (y-1) * 365 + (y-1)/4-(y-1)/100+(y-1)/400;
		
		for(i=0;i<(m-1);i++){
			
			nalsu += months[i];
			
		}
		
		nalsu += d;
			
		week = nalsu%7;
		
		System.out.println(y + "년 " + m + "월 " + d + "일 " + ch[week]+"요일");
		
		sc.close();
	}

}

 

==Console==

<구구단>

//똑같은 코드입니다. 가져가실수 있게 해 놓았습니다.

package com.day3;

import java.util.Scanner;


class Test1 {

	public static void main(String[] args) {

		//반복문(for,while,do~while)

		//for : 시작값과 끝값이 정해져 있을 때 사용
		//while : 끝값이 정해져 있지 않거나 끝값을 모를 때 사용
		//do~while : 일단 실행해 봐야 할 때 사용(사용 후 재실행을 고민해 봐야할 때 사용)

		Scanner sc = new Scanner(System.in);

		int dan;

		System.out.print("단을 입력하시오. "); //7
		dan = sc.nextInt();

		//for(시작값;최대값;증가값)

		for (int i=1;i<=9;i++) {

			System.out.println(dan + " * " + i + " = " + (dan*i));

		}

		System.out.println("--------------------------------------");

		//while(조건)

		int j=0; //while문은 초기값을 0으로 시작함.
		while(j<9) {
			// 조건 작성 시 = 을 잘 안씀
			j++;
			System.out.println(dan + " * " + j + " = " + (dan*j));

		}

		System.out.println("---------------------------------------");

		//do{~}while(조건문);

		int k=0;
		do	{

			k++;
			System.out.println(dan + " * " + k + " = " + (dan*k));

		}
		while (k<9);
		// 1번 실행 후 조건 만족 결과 확인

		System.out.println("---------------------------------------");

		/*		//무한루프
		while (true) {

			System.out.println("나 돌아간다~~");

		}

		 */

		sc.close();
		// stream 안의 데이터를 제거하기 위해 사용(close로 닫아주거나 null로 초기화 시켜줘야 함.)
	}
}

 


==Console==

<입력된 수까지의 합을 구하자!>

==추가 설명==

//InputStreamReader : System.in으로 입력받은 1byte자료를 2byte로 전환해줌

//readLine은 엔터(e) 앞까지 읽어들임

//false가 되어야 do while 조건문에서 빠져나감

////ye 시 System.in.read에서 y만 읽어 냄 e가 남은 상태에서 위의 수입력으로 이동
//System.out.print("수 입력? "); //e 가 입력되어 있는 상태에서 숫자 입력 상태가 됨
//su = Integer.parseInt(br.readLine()); -> e 앞부분만 입력되기 때문에 읽혀진 것은 null

// skip(2->이유 : 1. BufferedReader는 공백을 못 읽음. 2. 엔터의 아스키값(10,13) 때문에)

 

==Console==

<다중 for문을 이용한 구구단>

==Console==

이렇게 해서 9단까지 올라갑니다.

'Java' 카테고리의 다른 글

Java Day5 : 배열,난수(로또)  (0) 2019.06.12
Java Day4 : 반복문을 통해 별찍기,배열,만년달력  (0) 2019.06.11
Java Day1 : Method 메소드  (0) 2019.06.06
Java Day2 : 삼항연산자,조건문(if)  (0) 2019.06.03
Day13 List 1  (0) 2019.02.02

Method = 다른 언어에서는  Function()함수라고도 함

Method?

특정 작업을 수행하는 일련의 문장들을 하나로 묶는것

 

* 참고 

메소드나 객체지향과 같은 개념은 웅장하고, 결함이 없고 유지보수하기 쉬운 애플리케이션을 만들기 위한 기법

Method 핵심적인 가치 : 재활용성, 중복된 코드제거,프로그램의 구조화

 

밑에서 보는 main을 메소드라고 합니다.

==Console ===

*매개변수(Parameter)와 아규먼트의 차이점

매개변수는 함수의 정의부분에 나열되어 있는 변수들을 의미하며,

전달인자는 함수를 호출할때 전달되는 실제 값을 의미

 

//args = arguments
/*
// 변수선언
int num1;
// int의 method는 소문자로 시작, 상수일 경우 대문자 사용
int num2;

// 변수 초기화
        num1 = 20;
// 초기화 전에는 쓰레기 값으로 정해짐
num2 = 3;
*/

//f = format , 문자 안에 정수 입력 시 사용, 숫자 입력은 순서대로 적용

// println(매개변수 안써도 됨), print(매개변수 써야됨)
// \n = %n

//// %d : 정수, %s : 문자, %g : double, %f : float

 

==Console==

// println 은 일일이 작성 필요, printf 는 "네임 + %d, num1"과 같은 입력 가능

==Console==

// import에서 *가 의미하는 것은 io폴더 안의 class를 자동으로 찾아서 로딩할 수 있도록 선정. 그러나 권장은 하지 않음

// IOException 에러를 예외시켜 주는 Class

////BufferedReader 사용자가 입력한 값을 불러오는 class

// System.in : 키보드로 입력한 1개의 문자(Char)를 읽어냄 

// br(변수값).readLine : 사용자 입력값(br)을 읽어주는 method(문자로 읽음)
//Integer.parseInt() : 숫자로 읽어주는 method

 

== Console===

 

 

 

==Console==

<연산>

// 삼항연산자
str = num1 % 2 == 0?"짝수":"홀수";
// ?앞이 true 이면 콜론 앞을 사용, false 일땐 콜론 뒤를 사용

==Console==

==Test2 Console==

==Test3 추가 설명 ==

// for(int 초기값;조건;추가값) 

-> 초기값으로 조건 이행, 추가값 사용 후 조건 확인 진행 조건 불충분 시 중괄호 안 미진행
// i++ = i+1

==Test3 Console==

<한개의 문자를 읽기>

==추가 설명 ==

// char ch = 'a';
// char는 변수 선언 시 문자 1개만 사용 가능하며, 선언 시 ''안에 입력

// System.in.read(); 1개의 문자만 읽어줄 수 있음 -> 뒤에 abcde 써도 a만 읽을 수 있음
// System.in.read(); 으로 읽을 시 바이트(아스키코드)로 읽어들임. -> 강제형변환 필요
/*
System.out.println(ch);                        -> a가 보여짐
System.out.println(Integer.toString(ch));      -> 97을 보여짐
// integer.toString() : 입력되어 있는 아스키 코드를 보여줌
*/

// char값과 int값 비교시 char값이 자동으로 int로 전환되어 비교함.

==Console==

 <수를 입력했을 경우 홀인지 짝인지 알아보기>

==Console==

<if문을 이용한 성적표 >

==Console==

// if문이 여러개일 경우 else if 활용
// 여러 조건을 사용해야 할 경우 위에서 부터 쓰는게 좋음

 

==Console==

 

 

'Java' 카테고리의 다른 글

Java Day5 : 배열,난수(로또)  (0) 2019.06.12
Java Day4 : 반복문을 통해 별찍기,배열,만년달력  (0) 2019.06.11
Java Day3 : 반복문(do~while),구구단  (0) 2019.06.11
Java Day1 : Method 메소드  (0) 2019.06.06
Day13 List 1  (0) 2019.02.02

package com.day13;


import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import java.util.ListIterator;


public class Test1 {


public static void main(String[] args) {


ArrayList<String> lists = new ArrayList<String>();

lists.add("서울");

lists.add("부산");

lists.add("대구");

Iterator<String> it = lists.iterator();

while(it.hasNext()){

String str = it.next();

System.out.print(str + " "); //가로로 찍는 방법

}

System.out.println();

System.out.println("----------------------");

ListIterator<String> it2 = lists.listIterator();

while(it2.hasNext()){

System.out.print(it2.next() + " ");

}

System.out.println();

//출력 후 데이트는 null

while(it2.hasNext()){

System.out.print(it2.next() + " ");

}


System.out.println("----------------------");

//역순으로 출력

//ListIterator<String> it3 = lists.listIterator();

while(it2.hasPrevious()){

System.out.println(it2.previous());

}

System.out.println("----------------------");

List<String> lists1 = new ArrayList<String>();

lists1.addAll(lists);

lists1.add("인천");

int n = lists1.indexOf("부산");//1

lists1.add(n+1, "광주");

for(String c : lists1){

System.out.print(c + " ");

}

System.out.println();

//------------------------------------------------

System.out.println("-----------------------");

List<String> lists2 = new ArrayList<String>();

lists2.add("자바프로그래머");

lists2.add("프레임워크");

lists2.add("스트럿츠");

lists2.add("서블릿");

lists2.add("스프링");

String str;

Iterator<String> it4 = lists2.iterator();

while(it4.hasNext()){

str = it4.next();

if(str.startsWith("서"))

System.out.println(str);

}

}


}




+ Recent posts