Ctrl + F : 써놓은 메소드 검색하기
생성자
new 클래스_이름(생성자 ();)
명시적 형변환
(int) num = (int)Math.random();
중첩 IF문 (Nested IF)
package kr.co.hanuledu.controlex;
public class IfDiceEX {
public static void main(String[] args) {
// Math.random() 난수를 발생
// 0.0 ~ 1.0 사이의 double 타입 난수
// (0.0*6) <= (Math.random() * 6 + 1) < (1.0 * 6)\
// (int)0.0 (int)0.0, 1.0, 2.0, - 5.0 (int)6.0
// 0 + 1 1 ~ 6 7
int num = (int)(Math.random() * 6) + 1;
System.out.println(num);
}
}
주사위 랜덤
package kr.co.hanuledu.controlex;
public class IfDiceEX {
public static void main(String[] args) {
// Math.random() 난수를 발생
// 0.0 ~ 1.0 사이의 double 타입 난수
// (0.0*6) <= (Math.random() * 6 + 1) < (1.0 * 6)\
// (int)0.0 (int)0.0, 1.0, 2.0, - 5.0 (int)6.0
// 0 + 1 1 ~ 6 7
int num = (int)(Math.random() * 6) + 1;
if(num == 1) {
System.out.println(num + "번이 나왔습니다.");
} else if(num == 2) {
System.out.println(num + "번이 나왔습니다.");
} else if(num == 3) {
System.out.println(num + "번이 나왔습니다.");
} else if(num == 4) {
System.out.println(num + "번이 나왔습니다.");
} else if(num == 5) {
System.out.println(num + "번이 나왔습니다.");
} else {
System.out.println(num + "번이 나왔습니다.");
}
}
}
랜덤으로 돌리는 score 점수 판별
package kr.co.hanuledu.controlex;
public class IfNestedEx0 {
public static void main(String[] args) {
// 중첩 if문 : if문 안에 if문이 있는 문
// if(조건식 A) {
// if(조건식 B) { 조건 A를 만족한 상태에서 조건식 B이 참일 때 실행되는 문장;}
// }
int score = (int)(Math.random() * 100);
if(score >= 0 && score <= 100) {
if(score >= 90) {
System.out.println("점수는 " + score +"이고, A학점입니다.");
} else if (score >= 80) {
System.out.println("점수는 " + score +"이고, B학점입니다.");
} else if (score >= 70) {
System.out.println("점수는 " + score +"이고, C학점입니다.");
} else if (score >= 60) {
System.out.println("점수는 " + score +"이고, D학점입니다.");
} else {
System.out.println("점수는 " + score +"이고, F학점입니다.");
}
}
}
}
package kr.co.hanuledu.controlex;
public class IfNestedEx0 {
public static void main(String[] args) {
// 중첩 if문 : if문 안에 if문이 있는 문
// if(조건식 A) {
// if(조건식 B) { 조건 A를 만족한 상태에서 조건식 B이 참일 때 실행되는 문장;}
// }
int score = (int)(Math.random() * 100);
if (score >= 0 && score <= 100) {
if (score >= 90) {
System.out.println("점수는 " + score + "이고, A학점입니다.");
} else if (score >= 80) {
System.out.println("점수는 " + score + "이고, B학점입니다.");
} else if (score >= 70) {
System.out.println("점수는 " + score + "이고, C학점입니다.");
} else if (score >= 60) {
System.out.println("점수는 " + score + "이고, D학점입니다.");
} else {
System.out.println("점수는 " + score + "이고, F학점입니다.");
}
} else {
System.out.println("0 ~ 100까지 숫자만 입력해 주세요");
}
}
}
package kr.co.hanuledu.controlex;
public class IfNestedEx0 {
public static void main(String[] args) {
// 점수가 80 ~ 100 사이의 점수를 성적 판별
// A+, A, B+, B
int score = (int)(Math.random()*21) + 80;
if(score >= 95 && score <= 100) {
System.out.println("A+");
System.out.println(score +"입니다.");
} else if(score >=90 && score < 95) {
System.out.println("A");
System.out.println(score +"입니다.");
} else if(score >=85 && score < 90) {
System.out.println("B+");
System.out.println(score +"입니다.");
} else if(score >=80 && score < 85) {
System.out.println("B");
System.out.println(score +"입니다.");
}
}
}
package kr.co.hanuledu.controlex;
public class IfNestedEx0 {
public static void main(String[] args) {
// 점수가 80 ~ 100 사이의 점수를 성적 판별
// A+, A, B+, B
int score = (int)(Math.random()*21) + 80;
String grade;
if(score >= 90) {
if(score >= 95) {
grade = "A+";
} else {
grade = "A";
}
} else {
if(score >= 85) {
grade = "B+";
}
grade = "B";
}
System.out.println("당신의 학점은 " + grade +"입니다.");
}
}
Switch문
package kr.co.hanuledu.controlex;
public class SwitchEX0 {
public static void main(String[] args) {
/*
* switch(기준값) {
* case 값1 :
* 값1이 참일 때 실행되는 문장;
* break;
* case 값2 :
* 값2이 참일 때 실행되는 문장;
* break;
* case 값3 :
* 값3이 참일 때 실행되는 문장;
* break;
* default :
* 거짓일 때 실행되는 문장;
* }
*/
int score = 78;
switch(score / 10) {
case 10:
case 9:
System.out.println("A학점");
break;
case 8:
System.out.println("B학점");
break;
case 7:
System.out.println("C학점");
break;
case 6:
System.out.println("D학점");
break;
default:
System.out.println("F학점");
}
}
}
package kr.co.hanuledu.controlex;
import java.util.Scanner;
public class SwitchEX02 {
public static void main(String[] args) {
// 어떤 수를 입력을 받은 후에 switch~case문을 이용해서 '홀수', '짝수'를
// 판별하는 기능을 작성하시오.
Scanner scanner = new Scanner(System.in);
System.out.print("숫자를 입력해주세요 : ");
int num = scanner.nextInt();
switch(num % 2)
{
case 0:
System.out.println("입력하신 숫자는 " + num + "짝수입니다.");
break;
case 1:
System.out.println("입력하신 숫자는 " + num + "홀수입니다.");
}
}
}
package kr.co.hanuledu.controlex;
//import java.util.Scanner;
public class SwitchEX02 {
public static void main(String[] args) {
// if문에서 사용한 주사위 코드를 switch~case로 변경
int num = (int)(Math.random() * 6) + 1;
switch(num) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
}
System.out.println(num +"입니다.");
}
}
package kr.co.hanuledu.controlex;
//import java.util.Scanner;
public class SwitchEX02 {
public static void main(String[] args) {
// 8 ~ 11까지의 랜덤한 정수를 생성하는 작업
int time = (int)(Math.random() * 4) + 8;
System.out.println("[현재 시간 : " + time + "시]");
switch(time) {
case 8:
System.out.println("출급합니다.");
break;
case 9:
System.out.println("회의를 합니다.");
break;
case 10:
System.out.println("업무를 봅니다.");
break;
case 11:
System.out.println("외근을 나갑니다.");
break;
}
}
}
package kr.co.hanuledu.controlex;
public class SwitchExpressionEx {
public static void main(String[] args) {
String day= "SUNDAY";
switch(day) {
case "MONDAY":
case "FRIDAY":
case "SUNDAY":
System.out.println(6);
break;
case "TUESDAY" :
System.out.println(7);
break;
case "THURSDAY":
case "SATURDAY":
System.out.println(8);
break;
case "WEDNESDAY" :
System.out.println(9);
break;
}
}
}
package kr.co.hanuledu.controlex;
public class SwitchExpressionEx {
public static void main(String[] args) {
String day= "SUNDAY";
// Java 12부터 사용 가능
int num = switch(day) {
case "MONDAY", "FRIDAY", "SUNDAY" -> System.out.println(6);
case "TUESDAY" -> System.out.println(7);
case "THURSDAY","SATURDAY"-> System.out.println(8);
case "WEDNESDAY" -> System.out.println(9);
}
}
}
'BackEnd > Backend 공부 정리' 카테고리의 다른 글
| sqldeveloper-5 (0) | 2024.08.09 |
|---|---|
| Java-5.3 (0) | 2024.08.08 |
| Java-5.1 (0) | 2024.08.08 |
| sqldeveloper-4 (0) | 2024.08.02 |
| Java-3 (0) | 2024.08.02 |