iknowahra / cspiEdu

CSPI OJT
0 stars 0 forks source link

2021.09.14 교육내용 정리 #2

Open iknowahra opened 2 years ago

iknowahra commented 2 years ago

입력하는 방법

cf. 출력하는 방법 System.out.print();

Scanner sc = new Scanner(System.in); 
// System.in console 창에 프린트

=> 반복 설정을 하지 않아서 입력은 일 회만 가능하다.

🟡 단축키

Scanner sc = new Scanner(System.in);
int i = sc.nextInt();
int x = 10;
System.out.println(i+x);
Scanner sc = new Scanner(System.in);
String str = sc.nextLine(); 
System.out.println(str + "입니다.");

if 와 switch

switch

정형화된 조건 유형에 따를 경우 switch

eg) 서양에서 월을 표현할 때. 학생들의 성적

int : 정수 타입이라서 뒤의 소수를 알아서 제거한다.

Scanner sc = new Scanner(System.in);

int score; 
score = sc.nextInt();
score = (score / 10)* 10;

switch (score) {
  case 100:
        System.out.println("A");
        break;
  case 90:
        System.out.println("A");
        break;
  case 80:
        System.out.println("B");
        break;
  case 70:
        System.out.println("C");
        break;
  default: 
        System.out.println("F");
        break;
}

if

Scanner sc = new Scanner(System.in);

int i = sc.nextInt();

if(i > 100) {
    System.out.println(i+ " 는 백보다 크다.");
}
else if (i == 100) {
    System.out.println(i+ " 는 백과 같다.");
}
else {
    System.out.println(i+ " 는 백보다 작다.");
}
Scanner sc = new Scanner(System.in);

String str = sc.nextLine();

if(str.equals("test")) {
    System.out.println(str+" is test");
} else {
    System.out.println(str+" isn't test.");
}

String은 객체이기 때문에 String == String 어렵다.

String 내장 매소드 .equals()를 이용해야 한다.

🌟 String 자체 내장 매소드는 자주 사용하기 때문에 내장 매소드를 전체 공부하는 게 좋다.

반복문

for, while, do~while, iterator

list를 iterator로 하면 편하다.

while

int i = 1;
while (i <= 10) {
  if(i % 2 == 0) {
    System.out.println(i++); // 주석에 언급해주면 좋다.
  }
}

위의 경우에는 i % 2 != 0 일 때,i++가 적용이 안되서... while 무한 반복

int i = 1;
while (i <= 10) {
   if(i % 2 == 0) {
    System.out.println(i); 
  }
  i++;
}

🌟i++ vs ++i

✔️과제는 메일로

다음 수업은 9월 23일 목요일