nextInt 다음에 nextLine 이 오는 경우, 문자열 입력을 받지않고 넘어가 버린다.
조건문과 반복문
If : 범위비교 if (조건식) {문장}
-else if
Switch : 동일비교
for ( ,조건식, ) { }
while (for 문이 각각 떨어져 있는 형태)
단축키
F10 : 단독실행 단축키
F5 : 디버그 모드
shift+F5 : 디버그 모드 해제
ctrl+C : 루프 중단
Code
import java.util.Scanner;
public class SwitchEx {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("====================");
System.out.println(" 1. 1번 기능");
System.out.println(" 2. 2번 기능");
System.out.println(" 3. 3번 기능");
System.out.println(" 4. 4번 기능");
System.out.println("선택 : ");
int sel = s.nextInt();
switch(sel) {
case 1:
System.out.println("1번 기능 선택됨");
break; //switch에서 빠져나가라는 지시
case 2:
System.out.println("2번 기능 선택됨");
break;
case 3:
System.out.println("3번 기능 선택됨");
break;
case 4:
System.out.println("4번 기능 선택됨");
break;
default:
System.out.println("잘못된 번호 선택입니다.");
}
// if로 작성 시
// if(sel == 1) System.out.println("1번 기능 선택됨");
// else if(sel == 2) System.out.println("2번 기능 선택됨");
// else if(sel == 3) System.out.println("3번 기능 선택됨");
// else if(sel == 4) System.out.println("4번 기능 선택됨");
// else System.out.println("잘못된 번호 입력");
s.close();
}
}
import java.util.Scanner;
public class ScannerExercise {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("====== 회원등록 ======");
System.out.print("아이디 : ");
String user_id = s.nextLine();
System.out.print("비밀번호 : ");
String user_pwd = s.nextLine();
// Int user_pwd 사용시 에러 발생
// s.nextLine(); 을 한번 더 입력해 줌으로써 해결가능 //입력 스트림 메모리 비우기 역할
System.out.print("이름 : ");
String user_name = s.nextLine(); // 문자열 입력
System.out.print("이메일 : ");
String user_email = s.nextLine();
System.out.print("생년월일 (8자리) : ");
int user_birth = s.nextInt();
System.out.print("성별 (0-선택안함, 1-남, 2-여) : ");
int user_gen = s.nextInt();
System.out.println("==== 가입 정보 ====");
System.out.println("아이디 : " + user_id + " / 비밀번호 : " + user_pwd);
// 성별을 0, 1, 2 가 아닌 다른 말로 출력하기(condition check operator)
String gen = user_gen == 0 ? "선택안함" : user_gen == 1? "남" : "여";
System.out.println("이름 : " + user_name + " / 생년월일 : " + user_birth + " / 성별 : " + gen);
System.out.println("이메일 : " + user_email);
s.close();
}
}
public class LoopEx2 {
public static void main(String[] args) {
int sum = 0;
for(int i = 1; i <= 10; i++) {
sum = sum + i; // sum += i;
}
System.out.println(sum);
}
}
public class NestedLoop {
public static void main(String[] args) {
for(int i=0; i<5; i++) {
for(int j=0; j <= i; j++) {
System.out.print(" * ");
}
System.out.println();
}
}
}
import java.util.Scanner;
public class ConditionCheck01 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("점수를 입력하시오 : ");
// s. 잊어버리지말기!!
double score = s.nextDouble();
// s.close(); 여기서 스캐너 닫아도 됨.
// int score = 90;
final int PASS_SCORE = 60;
if (score >= PASS_SCORE) {
// score의 값이 PASS_SCORE 이상일 때 실행
System.out.println("합격입니다.");
}
else {
System.out.println("불합격입니다.");
}
// 점수 0~100
if(score > 100) {
System.out.println("잘못된 점수 입니다.");}
else if(score >= 90)
System.out.println("A");
else if(score >= 80)
System.out.println("B");
else if(score >= 70)
System.out.println("C");
else if(score >= 60)
System.out.println("D");
else if(score >= 0)
System.out.println("F");
else
System.out.println("잘못된 입력 값입니다.");
// 아래와 같은 형태도 가능
// String grade = "";
// if(score > 100) grade = "잘못된 입력 값입니다.";
// else if(score >= 90) grade = "A";
// else if(score >= 80) grade = "B";
// else if(score >= 70) grade = "C";
// else if(score >= 60) grade = "D";
// else if(score >= 0) grade = "F";
// else grade = "잘못된 입력 값입니다."
// System.out.println(grade);
s.close();
}
}