백엔드 과정/자바 예습

[자바의정석] 연습 문제 1-1 / 조건식

mim 2021. 12. 22. 16:21
반응형

2021.12.21 팀스터디에서 공유된 자바의 정석 연습 문제를 풀어보았다. 

 

/* 문제 1-1 */
//1. int형 변수 x가 10보다 크고 20보다 작을 때 true인 조건식
	int x= 12;
	boolean res1 =x >=10 && x <= 20;
	
	System.out.println("문제1: " + res1 );

 

//2. char형 변수 ch가 공백이나 탭이 아닐 때 true인 조건식
	char ch1 = '\t';
	boolean res2 = ch1 !=' ' && ch1 != '\t';  // \t 탭을 뜻함
		
	System.out.println("문제2: " + res2 );

 

//3. char형 변수 ch가 ‘x' 또는 ’X'일 때 true인 조건식
	char ch2 = 'x';
	boolean res3 =(ch2 =='x') || (ch2 == 'X');
    
   	System.out.println("문제3: " + res3);

 

//4. char형 변수 ch가 숫자(‘0’~‘9’)일 때 true인 조건식
	char ch3 = '0';
	boolean res4 = ch3 >= '0' && ch3 <='9' ;
	
	System.out.println("문제4: " + res4);

 

//5. char형 변수 ch가 영문자(대문자 또는 소문자)일 때 true인 조건식
	char ch5 = 'y';
	boolean res5 = (ch5>='A' && ch5<='Z')||(ch5>='a' && ch5<='z');
	
	System.out.println("문제5: " + res5);

 

//6. int형 변수 year가 400으로 나눠떨어지거나 또는 4로 나눠떨어지고 100으로 나눠떨어지지 않을 때 true인 조건식
	int year = 777;
	
	int inum1 = year % 400 ;
	int inum2 = year % 4 ;
	int inum3 = year % 100 ;
			
	boolean res6 = (inum1 == 0) || (inum2 ==0 && inum3 ==0) ;
	
	System.out.println("문제6: " + res6);

 

//7. boolean형 변수 powerOn가 false일 때 true인 조건식
	boolean powerOn = false;
	
	System.out.println("문제7: " + !powerOn);

 

//8. 문자열 참조변수 str이 “yes”일 때 true인 조건식
	String str = "no";
	
	boolean res8 = str =="yes";
	System.out.println("문제8: " + res8 );

 

반응형