Boolean Datatype

 

true와 false로 이루어져있는 Boolean type

 

public class BooleanApp {

	public static void main(String[] args) {

		System.out.println("One");
		System.out.println(1);
		System.out.println(true);
		System.out.println(false);
		
		String foo = "Hello world";
//		Stirng true = "Hello world"; reserved word
		
		System.out.println(foo.contains("world"));
		System.out.println(foo.contains("A"));
	}

}

contains는 포함되어있으면 true 없으면 false

 

비교연산자

 

비교연산자는 왼쪽의 값과 오른쪽의 값을 비교할 때 쓰임

 

public class ComparisonOperatorApp {

	public static void main(String[] args) {

		System.out.println(1 > 1); // false
		System.out.println(1 == 1); //true
		System.out.println(1 < 1);
		System.out.println(1 >= 1);

	}

}

 

조건문

public class IfApp {

	public static void main(String[] args) {

		System.out.println("a");
		if(false) {
			System.out.println(1);
		} else if(true) {
			System.out.println(2);
		} else {
			System.out.println(3);
		}
		System.out.println("b");
	}
}

 

조건문 응용1

public class AuthApp {

	public static void main(String[] args) {

		String id = "A";
		String inputId = args[0];
		
		System.out.println("Hi.");
		
//		if(inputId == id) {
		if(inputId.equals(id)) {
			System.out.println("Master!");
		} else {
			System.out.println("Who are you?");
		}
	}

}

조건문 응용2

public class AuthApp {

	public static void main(String[] args) {

		String id = "A";
		String inputId = args[0];
		
		String pass = "1111";
		String inputPass = args[1];
		
		System.out.println("Hi.");
		
//		if(inputId == id) {
//		if(inputId.equals(id)) {
//			if(inputPass.equals(pass)) {
//				System.out.println("Master!");
//			} else {
//				System.out.println("Wrong password");
//			}
//		} else {
//			System.out.println("Who are you?");
//		}
		
		if(inputId.equals(id) && inputPass.equals(pass)) {
				System.out.println("Master!");
			} else {
			System.out.println("Who are you?");
		}
	}

}

'프로그래밍 언어 > JAVA' 카테고리의 다른 글

공부(11)  (0) 2021.09.24
공부(10)  (0) 2021.09.24
공부(8)  (0) 2021.09.24
공부(7)  (0) 2021.09.23
공부(6)  (0) 2021.09.23

+ Recent posts