데일리로그C:
article thumbnail

<1교시>

실수라면 double
정수라면 int

double > float > int 순

char 은 문자!!!!

숫자 값 비교 == 
문자열 == 비교 : 주소값 비교
문자열 equlas 비교 : 변수명.equals(" ")

 

연산방향 : 좌  --> 우  (예외 : 증감(++, --), 부호(+,-), 논리(!), 대입은 반대로 우  --> 좌)

 

증감연산자( P112)

++ x   or    --y     --> 먼저 x와 y에 1 증가, 감소시킨 후에 다른 연산 (x or y > x+1 or y-1 >  result)

x++    or    y--     --> 다른 연산 한 다음 1증가 또는 감소시킴  (x or y > result > x+1 or y-1)

 

산술연산자(P114)

1. byte, short, char 타입인 경우 : int로 변환

2. 정수 + long 타입 경우 : long(정수중에 가장 큼)로 변환

3. 정수 + 실수 타입 경우 : double(실수)로 변환

                                  

<2교시>

삼항 연산자 (if - else if - else 구조 생각하면서 적기)
(조건) ? "값1" : "값2" ; 
(조건1)? "값1" : ((조건2)? "값2" : "값3" )
(조건1)? ((조건2)? "값1" : "값2" ) : "값3"


<3교시>

int 는 소수점 자리를 버린다. 


<4교시>

float : 소수점 값이 존재한다면 double로 변경되어야함
 ㄴ> 소수점 있을 때 없을 때 f 붙이는거 다름

String(문자열) --> 숫자로 
1. int a = Integer.parseInt( sc.nextLine( ) );
2. double b = Double.parsedouble( sc.nextLine( ) );

실수 --> 정수로
1. int a = (int) ~~~~~


<5교시 - 조건문, 반복문>

1. 조건문

주사위  -->  int num = (int) ( Math.random()*6 ) +1 ;

 

1~100 사이 랜덤 값  --> int num = (int) ( Math.random()*100 ) +1;

 

switch 구문의 단점 : 비교 애매 (값이 정해져있는 경우에 사용하기 좋음)

 

2. 반복문

1) for

int sum = 0;  //  총합
for (int 초기화식 ; 조건식; 증감식) {
            실행구문;
            (ex) sum +=i ; 
}
for(~~~~~) {
      if(조건) { // 참이라면 건너뛰고 다음껄로
              continue;
      }
~~~~~
}

 

2) while  --> 조건식 true 라면 break; or  a = false; 로 반복  stop 시킬 수 있음

int 초기화식
while (조건식) {
            실행구문;
            증감식;
}

 

int 초기화식
while (true) {
     if(조건) {
            실행구문; // 참일때
            break;
     } else {
            실행구문; // 거짓일때
    }
    증감식;

 

 

3) do

int  초기화식
do {
            실행구문;
            증감식;
} while(조건식);

 

전역변수 사용 시 
모든 지역변수에 값이 정해져있는 변수라면 초기화만 해도됨
근데 하나라도 값이 빈다면 무조건 초기화+선언까지 



package ex03;

public class Ex01 {

	public static void main(String[] args) {
		
		int a = 10;
		// a = a * -1;
		a = -a;
		System.out.println("a : "+a);
		
		/*
		boolean b = true;
		while(b) {
			 
			// break; or b = false; 로 둘 중 하나로 while 구문 빠져나오기
			
		}
		*/
		
		/////////////////////////////////////////////////////////
		char c1 = 'A' + 1;  // 65 + 1 = 66
		char c2 = 'A';
		char c3 = (char)(c2 + 1);
		System.out.println("c1 : " + c1);  // B
		System.out.println("c2 : " + c2);  // A
		System.out.println("c1 : " + c3);  // B
		
		/*
		int n1 = 10;
		int n2 = 10;
		
		if(n1 <= n2) {  // n1 < n2 || n1 == n2의 의미(or)
			
		}
		*/
		
		//////////////////////////////////////////////////
		char ch01 = 'a';
		char ch02 = 'b';
		System.out.println(ch01 < ch02); // true
		
		String str01 = "a";
		String str02 = "a";
		String str03 = new String("a"); // 주소값 다름
		
		//System.out.println(str01 < str02);  문자열이라서 크기 비교 안됨
		
		System.out.println(str01 == str02); // 비교 가능 true
		System.out.println(str01.equals(str02)); // 문자열 비교하려면 equals 사용 true
		System.out.println(str01 == str03); //false
		System.out.println(str01.equals(str03)); // true
		
		
		char charCode = 'A';
		if(!(charCode < 48) & !(charCode > 57)) { // charCode >= 48 & charCode <= 57 과 동일
			
		}
	} 
}

 

package ex03;

import java.util.Scanner;

public class Ex02 {
	public static void main(String[] args) {
		// 90 이상 'A', 80 이상 'B', 80 미만 'C'
		
		//Scanner sc = new Scanner(System.in);
		//int num = Integer.parseInt(sc).nextLine();
		
		int num=80;
		// 삼항 연산자 처리
		char result = (num >=90)? 'A':(num>=80)? 'B' : 'C';
		System.out.println(result);
	}
}

 

package ex03;

public class Page125 {

	public static void main(String[] args) {
		int i = 2;
		if(i >= 0) {
			System.out.println("0 이상");
		} else {
			System.out.println("0 미만");
		}
		
		// 삼항 연산자
		String result = (i>=0)? "0 이상":"0 미만";
		System.out.println(result);
				
				
		/////////////////////////////////////
		int a = 0;
		if(a > 0) {
			System.out.println("양수");
		} else {
			if(a == 0) {
				System.out.println("0");
			} else {
				System.out.println("음수");
			}
		}

		// 삼항 연산자
		String result2 = (a > 0)? "양수":((a == 0)? "0":"음수");
		System.out.println(result2);
		
		///////////////////////////////////////
		int b = 0;
		if(b >= 0) {
			if(b>0) {
				System.out.println("양수");
			} else {
				System.out.println("0");
			}
		} else {
			System.out.println("음수");
		}
		
		//삼항 연산자
		String result3 = (b>=0)? ((b>0)? "양수": "0") : "음수";
		System.out.println(result3);
	}
}

 

package ex03;

public class Q03 {

	public static void main(String[] args) {
		boolean stop = false;
		while(!stop) {  // stop!=true
			System.out.println("실행");
			stop = true;
		}
	}
}

 

package ex03;

public class Q04 {

	public static void main(String[] args) {
		int p = 534;
		int s = 30;
		
		// 학생 1명이 가지는 연필 개수(몫)
		int ps = p/s;
		System.out.println("몫 : " + ps);
		
		// 남은 연필 개수
		int pl = p%s;
		System.out.println("나머지 : " + pl);
	}
}

 

package ex03;

public class Q05 {

	public static void main(String[] args) {
		int v1 = 5;
		int v2 = 2;
		//double v3 = v1/v2;    double = 2  : 2를 double에 대입(2.0)  
		double  v3 = v1/(double)v2;   //2.5
		int v4 = (int)(v3*v2);  // (int)2.5*2 = (int)5.0 = 5
		System.out.println(v4);
	}
}

 

package ex03;

public class Q06 {

	public static void main(String[] args) {
		// 356 - 300, 3456 - 3400, 10 - 0
		int value = 356 / 100 * 100;  //  int 속성(소수점 없애기) 3*100 = 300
		System.out.println(value);
	}
}

 

package ex03;

public class Q07 {

	public static void main(String[] args) {
		float v1 = 10f;
		float v2 = v1 / 100;
		
		if(v1 == 0.1) {  // 0.1f
			System.out.println("같다");
		} else {
			System.out.println("다르다");
		}
	}
}

 

package ex03;

public class Q08 {

	public static void main(String[] args) {
    	// 사다리꼴 넓이 구하기(소수 나오게)
		int t = 5;
		int b = 10;
		int h = 7;
		
		double area = (t+b)*h/(double)2;  // (15)*7/2 = 105/2 =52.5
		System.out.println(area); 
	}
}

 

package ex03;

import java.util.Scanner;

public class Q09 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);  // 입력값 문자열임
		
		System.out.print("첫번째 수 : ");
		double n1 = Double.parseDouble(sc.nextLine());  //문자열 --> 실수로 변환
		
		System.out.print("두번째 수 : ");
		double n2 = Double.parseDouble(sc.nextLine());
		
		System.out.println("------------------");
		
		
		if(n2==0 || n2==0.0) {
			System.out.print("결과 : 무한대" );
		} else {
			System.out.print("결과: " + n1/n2);
		}
	}
}

 

package ex03;

public class Q10 {

	public static void main(String[] args) {
    	// 반지름이 10인 원의 넓이 구하기
		int v1 = 10;
		int v2 = 3;
		int v3 = 14;
		
        // double v4 = v1 * v1 * v2 + "." + v3;  --> 10*10*3+.+14 = 300 + .14
		double v4 = v1 * v1 * Double.parseDouble(v2 +"." + v3); // 100*(3.14) 
		
		int v5 = (int)v4;
		
		System.out.println("원의 넓이: " + v4);  //314.0
		System.out.println(v5); // 314
	}
}

 

package ex03;

import java.util.Scanner;

public class Q11 {

	public static void main(String[] args) {
		// 아이디 - "java" 패스워드 - int 12345
		Scanner sc = new Scanner(System.in);
		
		System.out.print("아이디: ");
		String id = sc.nextLine();
		
		System.out.print("패스워드: ");
		String pass = sc.nextLine();
		int pw = Integer.parseInt(pass);
		
		if(id.equals("java")) {
			if(pw == 12345) {
				System.out.println("로그인 성공");
			} else {
				System.out.println("로그인 실패: 패스워드가 틀림");
			}
		} else {
			System.out.println("로그인 실패: 아이디 존재하지 않음");
		}
	}
}

 

package ex03;

public class Q12 {

	public static void main(String[] args) {
		int x = 10;
		int y = 5;
		System.out.println((x>7) && (y<=5));  // true
		System.out.println((x%3 == 2)||(y%2 !=1));  // false
		
		
		/////////////////// Q13
		
		int v = 10;
		v = v + 10;  // v +=10;
		v = v - 10;  // v -=10;
		v = v * 10;  // v *= 10;
		v = v / 10;  // v /= 10;
		
		
		//////////////////Q14
		int s = 85;
		String result = (!(s>90))? "가":"나";  // s<=90를 의미
		System.out.println(result);  // 가
	}
}

 

package ex01;

public class Page136 {

	public static void main(String[] args) {
		// if구문
		
		int s = 93;  
		
		if(s>=90) {
			System.out.println("점수가 90보다 크거나 같다");
			System.out.println("등급은 A입니다");
		} 
		if(s<90) {  
			System.out.println("점수가 90보다 작다");
			System.out.println("등급은 B입니다");
		}
		
		// if else 구문  : 
		if(s>=90) {
			System.out.println("점수가 90보다 크거나 같다");
			System.out.println("등급은 A입니다");
		} else {
			System.out.println("점수가 90보다 작다");
			System.out.println("등급은 B입니다");
		}

	}

}

 

package ex01;

import java.util.Scanner;

public class Test01 {

	public static void main(String[] args) {
		// 0-100 사이의 값을 입력 받아 학점을 출력
		
		Scanner sc = new Scanner(System.in);
		System.out.println("0~100 사이 숫자를 입력하시오: ");
		int num = Integer.parseInt(sc.nextLine());
		
		// char ch; 전역변수 (각 값이 정해져있다면 초기화만 해도됨) 문자인 경우
		String result;  // 문자열인 경우
		
		if(num>=90) {
			result = "A";
		} else if(num>=80) {
			result = "B";
		} else if(num>=70) {
			result = "C";
		} else if(num>=60) {
			result = "D";
		} else {
			result = "F";
		}
		System.out.println("점수: " + num + ", 학점: " + result);
	}

}

 

package ex01;

import java.util.Scanner;

public class Test02 {

	public static void main(String[] args) {
		// 주사위를 던진다(1~6)
		// 몇번만에 맞추는지를 구현
		
		int num = (int)(Math.random()*6+1);  // 1~6 랜덤값
		
		Scanner sc = new Scanner(System.in); 
		
		
		
		int total = 1;  // 시작하자 맞췄다는 전제를 한다면 1번 떠야하므로 1!!
		while(true) {
			System.out.print("숫자: ");
		
			int you = Integer.parseInt(sc.nextLine());  // 입력값
			
			if(num==you) { // 랜덤값 == 입력값
				System.out.println("랜덤: " + num + ", 횟수 : " + total);
				// System.out.println("정답입니다");
				break;
			} else {
				System.out.println("틀렸습니다");
			}
			total ++;	
		} 
	}
}

 

package ex01;

public class Test03 {

	public static void main(String[] args) {
		// \t(tab) 이용 2~9단
		
        // for문
		for(int i=2; i<=9; i++) { //단
			for(int j =1; j<=9; j++) { // 단수
				System.out.print(i+"*"+j+"="+(i*j)+"\t");
			}
			System.out.println();
		}
		
		
        // while문
		int i =2;
		while(i<=9) {
			int j =1;
			while(j<=9) {
				System.out.print(i+"*"+j+"="+(i*j)+"\t");
				j++;
			}
			System.out.println();
			i++;
		}
		
		
        // do while문
		i =2;
		do {
			int j =1;
			do {
				System.out.print(i+"*"+j+"="+(i*j)+"\t");
				j++;
			} while(j<=9);
			System.out.println();
			i++;
		} while(i<=9);
		
		
        // 홀수단만 나오게
		for(i=2; i<=9; i++) { //단
			if(i%2==0) {   // 짝수단 건너뛰고 홀수단만
				continue;
			}
			
			for(int j =1; j<=9; j++) {
				System.out.print(i+"*"+j+"="+(i*j)+"\t");
			}
			System.out.println();
		}
	}
}

 

package ex01;

public class Page149 {

	public static void main(String[] args) {
		
		// for문
		int sum = 0;   // for문 안에 있는 것이 아닌 바깥에 있는 전역변수
		for(int i =1; i<=100; i++) {
			sum +=i;
		}
		System.out.println(sum);
		
		
		// while문
		int i = 1;
		sum =0;  // 위 for문 전역변수이므로 업데이트로 됨 (int 적지않기)
		while(i<=100) {
			sum +=i;
			i++;
		}
		System.out.println(sum);
		
		
		// do while문
		
		i = 1;
		sum = 0;
		do {
			sum +=i;
			i++;
		} while(i<=100);
		System.out.println(sum);
	}
}

 

package ex01;

public class Q02 {

	public static void main(String[] args) {
		// 1~100까지 정수 중에서 3의 배수의 총합(for문)
		
		int sum =0;
		for(int i=1; i<=100; i++ ) {
			if(i%3==0) {
				sum+=i;
			}
		}
		System.out.println("3의 배수 총합: " + sum);
		
		
		/// for, continue 이용
		sum =0;
		for(int i=1; i<=100; i++ ) {
			if(i%3 != 0) {  // 3의 배수가 아니라면 건너뛰기
				continue;
			}
			sum +=i;
		}
		System.out.println("3의 배수 총합: " + sum);
	}
}

 

package ex01;

public class Q03 {

	public static void main(String[] args) {
		// 두 수의 합이 5가 나오면 반복구문 종료시키기
		
		/*
		while(true) {
			int a = (int)(Math.random()*6)+1;
			int b = (int)(Math.random()*6)+1;
			
			System.out.println("("+a+","+b+")");
			if((a+b) == 5) {
				break;
			} 
		}
		*/
		

		boolean result = true;
		
		while(result) {
			int a = (int)(Math.random()*6)+1;
			int b = (int)(Math.random()*6)+1;
			
			
			if(a+b == 5) {
				result = false;
			} 
			System.out.println("("+a+","+b+")");
		}  
	}
}

 

package ex01;

public class Q04 {

	public static void main(String[] args) {
		// 중첩 for문 4x + 5y = 60
		
		
		for(int x=1; x<=10; x++) {
			for(int y =1; y<=10; y++) {
				if((4*x)+(5*y)==60) {
					System.out.println( "("+ x +"," + y + ")" );
				}	
			}
		}
	}
}

 

package ex01;

import java.util.Scanner;

public class Q05 {  

	public static void main(String[] args) {
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("몇층? ");
		int num = Integer.parseInt(sc.nextLine());
		System.out.println("===================");
		System.out.println();
		System.out.println("<피라미드>");
		for(int i=1; i<=num; i++) {
			
			for(int j=1; j<=i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

package ex01;

import java.util.Scanner;

public class Q07 {

	public static void main(String[] args) {
		boolean run = true;
		int balance = 0;  // 잔고
		Scanner sc = new Scanner(System.in);
		
		while(run) {
			System.out.println("--------------------");
			System.out.println("1.예금 2.출금 3.잔고 4.종료 5.거래내역");
			System.out.println("--------------------");
			
			System.out.print("선택> ");
			String menu = sc.nextLine();
			
			
			if(menu.equals("1") || menu.equals("예금")) { //1. 예금
				System.out.print("예금액> ");
				balance += Integer.parseInt(sc.nextLine());
			} else if(menu.equals("2") || menu.equals("출금")) { // 2. 출금
				System.out.print("출금액> ");
				//balance -= Integer.parseInt(sc.nextLine());
				int price = Integer.parseInt(sc.nextLine()); // 원하는 출금액
				if(balance >= price) { // 잔고보다 작거나 같으면
					balance -= price; // 입력한 출금액 출력
				} else {
					System.out.println("출금이 불가능합니다."); // 아니라면 불가능 출력
				}
			} else if(menu.equals("3") || menu.equals("잔고")) { // 3. 잔고
				System.out.println("잔고> "+balance);
			} else if(menu.equals("4") || menu.equals("종료")){ // 4. 종료
				System.out.println("프로그램 종료");
				
			} else if(menu.equals("5") || menu.equals("거래내역")) { // 거래내역
				// 나중에....(for문, push 사용)
			} else {
				System.out.println("존재하지 않는 메뉴입니다. 다시 선택하세요");
			}
			
		}
		

	}

}

 

profile

데일리로그C:

@망밍

포스팅이 도움됐다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

profile on loading

Loading...