[개발] - Java/Java, SpringBoot 추가 공부
230317 수업 조건문, 반복문 (1)
완벽한 장면
2023. 3. 20. 02:32
2 .
중첩 IF
public class Main1 {
public static void main(String[] args) {
int x = 1;
int y = 2;
if (x == 1) { // 첫 번째 if가 참이여야만 두 번째 if 검사하지.
if (y == 2) {
System.out.println("hello");
}
}
}
}
위에건 사실상
public class Main1 {
public static void main(String[] args) {
int x = 1;
int y = 2;
if (x == 1) {
if (y == 2) {
System.out.println("hello");
}
else if (y == 3) {
}
}
}
}
이것과 똑같다.
3.
로그인 로직 반복문으로 짜기
import java.util.Scanner;
/**
아이디가 틀리면 -> "존재하지 않는 회원입니다." 출력
비밀번호가 틀리면 -> "비밀번호 틀립니다." 출력
로그인에 성공하면 -> "로그인되었습니다." 출력
*/
public class Login1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("아이디를 입력하세요 : ");
String inputId = sc.next();
System.out.print("비밀번호를 입력하세요 : ");
String inputPw = sc.next();
String id = "hello";
String pw = "world";
if (id.equals(inputId)) {
if (!pw.equals(inputPw)) {
System.out.println("비밀번호가 틀립니다.");
} else {
System.out.println("로그인 되었습니다.");
}
}
else {
System.out.println("존재하지 않는 회원입니다.");
}
}
}
(또는 이것도 가능함.)
내가 처음에 짰던 코드
/*
//또는 if / else if 로도 표현이 가능하더라 => 내가 처음에 짰던 코드. 이게 더 단순할수도
if (id.equals(inputId)) {
System.out.println("존재하지 않는 회원입니다.");
} else if (pw.equals(inputPw)) {
System.out.println("비밀번호가 틀립니다.");
} else {
System.out.println("로그인 성공하였습니다.");
}
*/
728x90
반응형