2020년 11월 09일 9시 ~ 15시 30분 zoom
논리연산자 사용 예제
package com.test01;
import java.util.Scanner;
public class Operator03 { //1109
//&& (그리고) //|| (또는) //논리값&&논리값 논리값||논리값 public static void main(String[] args) { Operator03 test = new Operator03(); test.opTest1(); }
public void opTest1() { // [ 1 <= 숫자 <= 100 ] -- >> [ 숫자 >= 1 && 숫자 <= 100 ] Scanner sc = new Scanner(System.in);
System.out.println("정수 하나 입력 : "); int num = sc.nextInt();
System.out.println("1부터 100사이의 숫자인지 확인 : " + (num>=1 && num<=100));
//입력받은 문자가 대문자인지 확인 System.out.print("문자 하나 입력: "); char ch = sc.next().charAt(0);
System.out.println("영어 대문자인지 확인 : "+ (ch>='A' && ch<='Z'));
// || // 입력한 문자가 대소문자 상관없이 'y' 'Y' 인지 물어볼때
System.out.print("종료하시려면 y 혹은 Y를 입력하세요 : "); char ch2 = sc.next().charAt(0);
System.out.println("영문자 y인지 확인 : " + (ch2 == 'y' || ch2 == 'Y'));
} } |
package com.test01;
import java.util.Scanner;
public class Operator04 {
//삼항 연산자 //항목이 3개 : (조건식)?참일때 사용할 값1 : 거짓일 때 사용할 값2; public static void main(String[] args) { new Operator04().opTest(); }
public void opTest() { Scanner sc = new Scanner(System.in);
System.out.println("정수 하나 입력 >> "); int num = sc.nextInt();
String res = (num>0)?"양수":(num==0)?" 0 ":"음수";
System.out.println("입력받은 정수 " + num + " 은 " + res + " 입니다. ");
}
} |
package com.test01;
import java.util.Scanner;
public class Operator05 {
//산술 대입연산자 : += , -= , *= , /= , %= public static void main(String[] args) { new Operator05().opTest(); }
public void opTest() { int num=12, result;
System.out.println("num : " + num);// 12
num = num + 3; System.out.println("num : " + num);// 15
num += 3; System.out.println("num : " + num);// 18
num -= 5;//num = num - 5; 연산과 동일 System.out.println("num : " + num);// 13
num *= 2; System.out.println("num : " + num);// 26
num /= 2; System.out.println("num : " + num);// 13
} } |
제어문
제어문에는 조건문, 반복문, 분기문이 있다
조건문
프로그램 수행 흐름을 바꾸는 역할을 하는 제어문 중 하나,
조건에따라 다른 문장이 수행되도록 한다
- if 문
if (조건식) { … } |
조건식의 결과값이 true 일 경우 … 안의 내용을 실행하고, false일 경우 실행하지 않음
if (조건식) { a… } else { b… } |
조건식의 결과값이 true면 a의 내용을 실행하고, false 일 경우 b의 내용을 실행한다.
if (조건식) { a… } else if (조건식2) { b… } else { c… } |
조건식의 결과값이 true면 a의 내용을 실행하고, 조건식2의 값이 true면 b 실행, 모두 false일 경우 c를 실행한다
둘의 차이점
앞의 경우 처음 if 문에서 조건식 확인하고 실행하거나 실행하지않고,
다음 if문에서 또 조건식 확인하고 실행하거나 실행하지 않는다. 무조건 2개 다 실행
뒤의 경우 if문에서 조건식 확인하고 true 일 경우 a를 실행하고, false 일 경우 b를 실행하고 종료된다.
- switch case 문
조건식 하나로 많은 경우의 수를 처리할 때 사용,
이때 조건식의 결과는 정수 또는 문자, 문자열만 올 수 있습니다
조건식의 결과 값과 일치하는 case 문으로 이동하고 , 일치하는 결과값이 없을 경우 default 문을 실행합니다.
switch(조건식) { |
if 문 실습
package com.test01;
public class IfTestMain {
public static void main(String[] args) { IfTest01 it01 = new IfTest01(); it01.testIf(); } } |
package com.test01;
import java.util.Scanner;
public class IfTest01 {
public void testIf() { //단독 if //조건식의 결과가 true면 {} 안의 코드 실행 //조건식의 결과가 false면 {} 안의 코드 실행
Scanner sc = new Scanner(System.in);
System.out.println("정수 한 개 입력 >> "); int num = sc.nextInt();
//짝수인지 확인 if(num % 2 == 0) { //조건식이 true 일때 실행 System.out.println("입력하신 숫자는 짝수입니다."); }
//짝수가 아닌지 확인 (즉, 홀수인지) if(num % 2 != 0) { System.out.println("입력하신 숫자는 홀수입니다."); }
System.out.println("종료!");
} } |
package com.test01;
public class IfTestMain {
public static void main(String[] args) { /* IfTest01 it01 = new IfTest01(); it01.testIf(); */
IfTest02 it02 = new IfTest02(); it02.test(); } } |
package com.test01;
import java.util.Scanner;
public class IfTest02 {
//if ~ else : 둘 중 하나를 선택하는 조건문 public void test() { int i = 20;
if(i<10) { System.out.println("i가 10보다 작습니다."); } else { System.out.println("i가 10보다 큽니다"); }
} } |
package com.test01;
public class IfTestMain {
public static void main(String[] args) { /* IfTest01 it01 = new IfTest01(); it01.testIf(); */
IfTest02 it02 = new IfTest02(); it02.test(); System.out.println("----"); it02.testIfElse(); System.out.println("----"); it02.testIfElse2(); } } |
package com.test01;
import java.util.Scanner;
public class IfTest02 { //if ~ else : 둘 중 하나를 선택하는 조건문 public void test() { int i = 20; if(i<10) { System.out.println("i가 10보다 작습니다."); } else { System.out.println("i가 10보다 큽니다"); }
}
public void testIfElse() { //숫자를 하나 입력받고 홀수인지 짝수인지 판단하기 Scanner sc = new Scanner(System.in);
System.out.println("정수 하나 입력 >> "); int num = sc.nextInt();
if(num%2 == 0) { System.out.println(num + "은 짝수입니다."); } else { System.out.println(num + "은 홀수입니다."); }
}
public void testIfElse2() { //50보다 큰 수라면 짝수인지 홀수인지 출력하기 //50보다 작은 수라면 작다 라고 출력하기
Scanner sc = new Scanner(System.in);
System.out.println("정수를 하나 입력해주세요 >> "); int num = sc.nextInt();
if(num >= 50) { if(num % 2 == 0) { System.out.println(num + " 은 50보다 큰 짝수입니다."); } else { System.out.println(num + " 은 50보다 큰 홀수입니다."); } } else { System.out.println(num + " 은 50보다 작습니다."); }
} } |
package com.test01;
public class IfTestMain {
public static void main(String[] args) { /* IfTest01 it01 = new IfTest01(); it01.testIf(); */ /* IfTest02 it02 = new IfTest02(); it02.test(); System.out.println("----"); it02.testIfElse(); System.out.println("----"); it02.testIfElse2(); */
IfTest03 it03 = new IfTest03(); it03.test2(); } } |
package com.test01;
import java.util.Scanner;
public class IfTest03 {
//if ~ else if public void test() { int i = 10, j = 20;
if(i > j) { System.out.println("i 가 j 보다 큽니다."); } else if(i == j) { System.out.println("i 와 j 가 같습니다."); } else { System.out.println("i 가 j 보다 작습니다."); } }
public void test2() { //점수 하나를 입력받아 등급을 나누어 점수와 등급을 출력 //else if 이용 //90점 이상은 A //90점 미만 80점 이상 B //80점 미만 70점 이상 C //70점 미만 60점 이상 D //60점 미만은 F
Scanner sc = new Scanner(System.in);
System.out.print("점수 입력 : "); int num = sc.nextInt(); String grade;
if(num >= 90) { grade = "A"; if(num >= 95) { grade += "+"; } } else if(num < 90 && num >= 80) { grade = "B"; } else if(num < 80 && num >= 70) { grade = "C"; } else if(num < 70 && num >= 60) { grade = "D"; } else { grade = "F"; }
System.out.printf("당신의 점수는 %d 점 이고, %s 등급입니다. ",num, grade);
}
public void test3() { boolean bool = true;
if(bool) { System.out.println("true"); } else { System.out.println("false"); } } } |
package com.test02;
public class TestMain {
public static void main(String[] args) {
//1. 정수가 5의 배수일경우 5의 배수입니다 출력 new Test().test01(10); System.out.println(""); //2. 정수가 2의 배수이면서 3의 배수이면, 2와 3의 배수입니다 new Test().test02(4); System.out.println(""); //3. 문자가 소문자라면 소문자입니다, 대문자이면 대문자입니다 출력 new Test().test03('A'); } } |
package com.test02;
public class Test {
public void test01(int i) { //i가 5의 배수인지 확인하는 함수
if(i % 5 == 0) { System.out.println("i( " + i + " ) 는 5의 배수입니다. "); } else { System.out.println("i( " + i + " ) 는 5의 배수가 아닙니다. "); }
}
public void test02(int i) { //i가 2와 3의 공배수인지 확인하는 함수 if(i % 2 == 0 && i % 3 == 0) { System.out.println("i( " + i + " ) 는 2와 3의 공배수입니다. "); } else { System.out.println("i( " + i + " ) 는 2와 3의 공배수가 아닙니다. "); } }
public void test03(char c) { //대문자인지 소문자인지 확인하는 메소드 /* if(c >= 65 && c <= 90) { System.out.println("c ( " + c + " ) 는 대문자입니다."); } else if(c >= 97 && c <= 122){ System.out.println("c ( " + c + " ) 는 소문자입니다."); } */
if(Character.isUpperCase(c)) { System.out.println("c ( " + c + " ) 는 대문자입니다."); } else { System.out.println("c ( " + c + " ) 는 소문자입니다."); } } } |
package com.test03;
import java.util.Random;
public class MathTest {
public static void main(String[] args) { // new MathTest().testMath(); new MathTest().testMath2(); }
public void testMath() { System.out.println("상수 E : " + Math.E); System.out.println("상수 PI : " + Math.PI);
System.out.println("-7의 절대값 : " + Math.abs(-7));
//난수 : 임의의 값, 무작위로 만들어지는 값 System.out.println("임의의 난수 : " + Math.random()); }
public void testMath2() { //1 ~ 100 까지의 난수 구하기 //Math.random() -> 0.0 ~ 1.0 (0.0 이상 1.0 미만!) int random = (int) ((Math.random()*100)+1); //+1을 해주는 이유 //Math.random은 0.000..~ 0.999... 사이의 숫자가 나오는데 //여기다 100을 곱하면 00.00 ... ~ 99.99... 가 된다 //이 상태에서 (int)형으로 형변환해서 소수점 날리면 0 ~ 99사이의 숫자가 나오게 된다 //우리가 구해야하는 숫자의 범위는 1 ~ 100 사이의 숫자이기 때문에 //저 상태에서 1을 더해주어야 랜덤값의 범위가 1~100이 된다. //0~100 범위를 구하려면? //Math.random()*101 을하면 0~100 사이의 랜덤값 범위를 갖게 된다. System.out.println("난수 발생 : " + random);
//0부터 시작하는 경우 //(int)(Math.random()*최대값)
//1부터 시작하는 경우 //(int)(Math.random()*최대값)+1
//Math.random() * (max - min + 1) + min
//0~9 사이의 랜덤 값 int r1 = (int)(Math.random()*10); int rand1 = new Random().nextInt(10);//위에 것과 동일하게 나온다 : 0~9 System.out.println("0부터 9 사이의 랜덤 값 : " + r1); System.out.println("0부터 9 사이의 랜덤 값 : " + rand1); System.out.println("----");
//1~10 사이의 랜덤 값 int r2 = (int)(Math.random()*10)+1; int rand2 = new Random().nextInt(10)+1; System.out.println("1부터 10 사이의 랜덤 값 : " + r2); System.out.println("1부터 10 사이의 랜덤 값 : " + rand2); System.out.println("----");
//20 ~ 35 사이의 랜덤 값 int r3 = (int)((Math.random()*16)+20); int rand3 = new Random().nextInt(16)+20; System.out.println("20부터 35 사이의 랜덤 값 : " + r3); System.out.println("20부터 35 사이의 랜덤 값 : " + rand3); System.out.println("----");
//0 또는 1 int r4 = (int)Math.random()*2; int rand4 = new Random().nextInt(2); System.out.println("0부터 1 사이의 랜덤 값 : " + r4); System.out.println("0부터 1 사이의 랜덤 값 : " + rand4); System.out.println("----");
} } |
'기록 > 자바_국비' 카테고리의 다른 글
[배운 내용 정리] 1111 자바 국비교육 (0) | 2020.11.14 |
---|---|
[배운 내용 정리] 1110 자바 국비교육 (0) | 2020.11.10 |
[배운 내용 정리] 1106 자바 국비교육 (0) | 2020.11.06 |
[배운 내용 정리] 1105 자바 국비교육 (0) | 2020.11.06 |
[배운 내용 정리] 1104 자바 국비교육 (0) | 2020.11.04 |