항해99 Pre-onboarding 1주차 항해일지 <4day - TIL>
📌 조건식에 따라 다른 실행문을 실행하기 위해 사용됨
(1.1) if문
<예제코드 - 성적에 해당하는 등급 출력하기>
import java.util.Scanner;
public class Self_coding_4_1 {
public static void main (String[] args){
System.out.print(" 당신의 점수를 입력하세요 : ");
Scanner scanner = new Scanner(System.in);
String score = scanner.nextLine();
int num = Integer.parseInt(score);
String grade = "";
if ( num >= 90) {
System.out.println("점수가 90보다 큽니다.");
grade = "A";
}
if ( num <= 90){
System.out.println("점수가 90보다 작습니다.");
grade = "B";
}
System.out.println("등급은 : " + grade + " 입니다.");
}
}
//if문만을 이용한 성적 출력하기코드, 원 코드에서는 점수 변수로 선언해놓지만 scanner문을 통해서 입력받은 점수에 대한 성적을 출력하도록 작성했음
(1.2) if-else문
import java.util.Scanner;
public class Self_coding_4_1 {
public static void main (String[] args){
System.out.print(" 당신의 점수를 입력하세요 : ");
Scanner scanner = new Scanner(System.in);
String score = scanner.nextLine();
int num = Integer.parseInt(score);
String grade = "";
if ( num >= 90) {
System.out.println("점수가 90보다 큽니다.");
grade = "A";
} else {
System.out.println("점수가 90보다 작습니다.");
grade = "B";
}
System.out.println("등급은 : " + grade + " 입니다.");
}
}
(1.3) if-else if-else문
import java.util.Scanner;
public class Self_coding_4_1 {
public static void main (String[] args){
System.out.print(" 당신의 점수를 입력하세요 : ");
Scanner scanner = new Scanner(System.in);
String score = scanner.nextLine();
int num = Integer.parseInt(score);
String grade = "";
if ( num >= 90) {
System.out.println("점수가 90보다 큽니다.");
grade = "A";
} else if ( num >= 80 ){
System.out.println("점수가 80보다 작습니다.");
grade = "B";
} else if ( num >= 70 ){
System.out.println("점수가 70보다 작습니다.");
grade = "C";
} else {
System.out.println("점수가 70보다 작습니다.");
grade = "D";
}
System.out.println("등급은 : " + grade + " 입니다.");
}
}
🔥 주사위 굴리기 프로그램 작성을 위한 Math.random() 메소드 알아보기
(1.4) 랜덤함수를 및 else-if문을 이용한 주사위 뽑기
public class Self_coding_4_2 {
public static void main(String[] args) {
int num = (int) (Math.random() * 6) + 1;
// 로또 번호 뽑기를 하려면 *45를 하면 된다!
if (num == 1) {
System.out.println("1번을 뽑았습니다.");
} else if (num == 2) {
System.out.println("2번을 뽑았습니다.");
} else if (num == 3) {
System.out.println("3번을 뽑았습니다.");
} else if (num == 4) {
System.out.println("4번을 뽑았습니다.");
} else if (num == 5) {
System.out.println("5번을 뽑았습니다.");
} else {
System.out.println("6번을 뽑았습니다.");
}
}
}
(1.5) Switch문
public class Self_coding_4_3 {
public static void main(String[] args) {
int num = (int) (Math.random() * 6) + 1;
// 로또 번호 뽑기를 하려면 *45를 하면 된다!
switch (num){
case 1:
System.out.println("1번");
break;
case 2:
System.out.println("2번");
break;
case 3:
System.out.println("3번");
break;
case 4:
System.out.println("4번");
break;
case 5:
System.out.println("5번");
break;
case 6:
System.out.println("6번");
break;
}
}
}
📌 어떤 작업이 반복적으로 실행되도록 할 때 사용됨
(2.1) For문
#1부터 100까지의 합 더하기
public class Self_coding_4_4 {
public static void main(String[] args){
int sum = 0;
for (int i = 1 ; i <= 100; i ++){
sum += i;
}
System.out.println("1부터 100까지의 합은:" + sum);
}
}
(2.2) 중첩 for문
#중첩 for문을 통한 구구단 만들기
public class Self_coding_4_5 {
public static void main(String[] args){
for (int i=1; i<=9; i++){
System.out.println(" **** " + i + "단 ****");
for (int j=1; j<=9; j++){
int sum = i * j ;
System.out.println(i + "x" + j + "=" + sum );
}
}
System.out.println("**** 구구단 끝! ****");
}
}
(2.3) while문
#while문을 통한 1부터 100까지 더하기
public class Self_coding_4_6 {
public static void main(String[] args){
int i = 1;
int sum = 0;
while ( i <=100 ){
sum += i;
i ++;
}
System.out.println("1에서 100까지의 합은 :" + sum );
}
}
(2.4) do-while문
# 위 while문을 do-while형태로 바꾸면 아래와 같당
public class Self_coding_4_6 {
public static void main(String[] args){
int i = 1;
int sum = 0;
do {
sum += i;
i ++;
}
while ( i <=100 );
System.out.println("1에서 100까지의 합은 :" + sum );
}
}
(2.5) break/continue문
#continue문 예시
public class Self_coding_4_7 {
public static void main(String[] args){
for (int i = 1; i <= 10; i++){
if ( i % 2 != 1){
continue;
}
System.out.println(i);
}
}
}
1️⃣ while, Math.random()메소드를 이용하여 2개의 주사위를 던졌을 때 나오는 눈을 출력하고 눈의 합이 5가 아니면 계속, 눈의 합이 5이면 실행을 멈추는 코드를 작성하기
public class exam_coding_4_1 {
public static void main(String[] args){
while(true){
int x = (int) (Math.random() * 6) + 1;
int y = (int) (Math.random() * 6) + 1;
int sum = 0;
sum = x+y;
System.out.println("두 수의 합은 : " + x + "+" + y + "=" + sum + "입니다." );
if ( sum == 5 ){
break;
}
}
}
}
2️⃣ 중첩 for문을 이용하여 방정식 4x + 5y = 60의 모든 해를 구해서 (x,y)형태로 출력
public class exam_coding_4_2 {
public static void main(String[] args){
for ( int i=1; i <=10; i++){
for (int j=0; j<=10; j++){
int sum = (4 * i) + (5 * j);
if ( sum == 60) {
System.out.println("x : " + i + " y : "+ j);
}
}
}
}
}
3️⃣ 죽음의 별찍기 (아래 그림처럼 찍으면 됨 )
public class exam_coding_4_4 {
public static void main(String[] args){
for (int i=1; i<=5; i++){
for (int j=1; j <= i; j++){
System.out.print("*");
} System.out.println("");
}
}
}
4️⃣ ATM기 만들기
import java.util.Scanner;
public class exam_coding_4_5 {
public static void main(String[] args) {
boolean run = true;
int balance = 0;
Scanner scanner = new Scanner(System.in);
while (run) {
System.out.println("==============================");
System.out.println("1. 예금 | 2. 출금 | 3.잔고 | 4. 종료");
System.out.println("==============================");
System.out.print("선택:");
int menuNum = Integer.parseInt(scanner.nextLine());
switch (menuNum) {
case 1:
System.out.println("예금액 : ");
balance += Integer.parseInt(scanner.nextLine());
break;
case 2:
System.out.println("출금액 : ");
int outpay = Integer.parseInt(scanner.nextLine());
if (outpay > balance) {
System.out.println("출금액이 부족합니다.");
break;
}
balance -= outpay;
break;
case 3:
System.out.println("잔고 : " + balance);
break;
case 4:
System.out.println("종료합니다.");
break;
}
}
}
}
문제에서 요구하는대로만 만들면 출금 요청액보다 잔액이 적어도 인출되는 현상이 있었다.(마이너스 통장처럼) 어림없지 잔액이 출금액보다 적을 경우 출금 불가 하도록 수정하였다.
📌 코드업 - python 기초 100제 진행
a = input()
n = int(a)
print('%x' % n)
// n 앞에 공백있음