if/if else 조건문을 활용한 로그인 데모 프로그램
public class LoginDemo {
public static void main(String[] args) {
String id = args[0];
String password = args[1];
if (id.equals("Rara")) {
if (password.equals("1234")) {
System.out.println("Right");
} else {
System.out.println("Please, check your password.");
}
} else {
System.out.println("Your id and password are wrong");
}
}
}
Arguments(입력값)에 올바른 id와 잘못된 비밀번호 입력
실행 결과 : 정상 출력
Switch와 if 차이
public class Variable {
public static void main(String[] args) {
System.out.println("Switch(1)");
switch(2) {
case 1:
System.out.println("one"); // 실행X
case 2:
System.out.println("two"); //순차구조로 실행
case 3:
System.out.println("three"); // 'break'명령어로 'case 3'까지만 실행
break;
case 4:
System.out.println("four");
}
int a = 1;
if (a == 1) {
System.out.println("one"); // 1출력, 아래 코드들은 실행X
}
else if (a == 2) {
System.out.println("two");
}
else if (a == 3) {
System.out.println("three");
}
else if (a == 4) {
System.out.println("four");
}
}