오늘의 코드
package edu.java.if06;
import java.util.Scanner;
public class IfMain06 {
public static void main(String[] args) {
System.out.println("if 중첩문 연습");
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
sc.close();
// 1) 입력된 정수에 대해 짝수와 홀수로 나눔
// 2) 만약 짝수인 경우,
// ㄴ 입력된 정수가 4의 배수면 -> 4의 배수 출력
// ㄴ 입력된 정수가 4의 배수가 아니면 -> 4의 배수가 아닌 짝수 출력
// 만약 홀수인 경우,
// ㄴ 아무것도 출력 안함
if (num % 2 == 0) { // 짝수일 때
System.out.println("짝수");
if (num % 4 == 0) { // 4의 배수일 때
System.out.println("4의 배수");
} else { // 4의 배수가 아닐 때
System.out.println("4의 배수가 아닌 짝수");
}
} else { // 홀수일 때
System.out.println("홀수");
}
} // end main()
} // end IfMain06
package edu.java.if07;
import java.util.Scanner;
public class IfMain07 {
public static void main(String[] args) {
// Ctrl + Shift + f : 코드 자동 정렬
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
sc.close();
// 입력된 3 개의 숫자 중 가장 큰 수를 출력하시오.
if(a >= b) { // a가 b보다 크거나 같은가?
if(a >= c) {
System.out.println(a);
} else {
System.out.println(c);
}
} else {
System.out.println(b);
}
// 너무 긴 코드
if(a>=b) { // a가 b보다 큰경우
if(a>=c) { // a가 c보다 큰경우
System.out.println("가장큰수 : "+a);
}
} else if(b>=a) { // b가 z보다 큰경우
if(b>=c) { // b가 c보다 큰경우
System.out.println("가장큰수 : "+b);
}
} else if(c>=a) { // c가 a보다 큰경우
if(c>=b) { // c가 b보다 큰경우
System.out.println("가장큰수 : "+c);
}
}
// 3항으로 작성시
System.out.println(a>=b ? (a>=c ? "가장큰수 : " + a: "가장큰수 : " + c) : "가장큰수 : " + b);
} // end main()
} // end IfMain07
코드업 사이트에서 문제 풀이
package edu.java.codeup;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int c = sc.nextInt();
if (a <= b && a <= c) {
if (b <= c) {
System.out.println(a + " " + b + " " + c);
} else {
System.out.println(a + " " + c + " " + b);
}
}else if (b <= a && b <= c) {
if (a <= c) {
System.out.println(b + " " + a + " " + c);
} else {
System.out.println(b + " " + c + " " + a);
}
}else if (c <= b && c <= a) {
if (b <= a) {
System.out.println(c + " " + b + " " + a);
} else {
System.out.println(c + " " + a + " " + b);
}
}
// 다른 문제 풀이 법
int first = 0;
int second = 0;
int third = 0;
if (a <= b && a <= c) {
if (b <= c) {
first = a;
second = b;
third = c;
} else {
first = a;
second = c;
third = b;
}
}else if (b <= a && b <= c) {
if (a <= c) {
first = b;
second = a;
third = c;
} else {
first = b;
second = c;
third = a;
}
}else if (c <= b && c <= a) {
if (b <= a) {
first = c;
second = b;
third = a;
} else {
first = c;
second = a;
third = b;
}
}
System.out.println(first + " " + second + " " + third);
} // end main()
} // end main
ckage edu.java.for01;
// for 반복문
// - 반복 수행을 하기 위한 키워드
// - for(초기식; 조건식; 증감식) {
// (본문)
// }
// - 초기값을 증감시켜서 조건식이 false가 될 때까지 본문을 반복
// - 실행 순서
// 1) 초기식. 2) 조건식. 3) 본문. 4) 증감식
public class ForMain01 {
public static void main(String[] args) {
System.out.println("for 반복문");
// 1 ~ 10까지 출력
for(int x = 1; x <= 10; x++) {
System.out.println(x);
}
System.out.println("=========");
// 1, 3, 5, 7, 9를 출력
for(int x = 1; x <= 9; x = x + 2) {
System.out.println(x);
}
// 15, 14, 13, 12, ..., 1을 출력
for(int x = 15; x >= 1; x--) {
System.out.println(x);
}
} // end main()
} // end ForMain01
package edu.java.for02;
public class ForMain02 {
public static void main(String[] args) {
// println() : 맨 마지막 줄바꿈(\n)이 포함된 출력 함수
// print() : 문자열을 나열하여 출력
// 0 2 4 6 8 10
for(int x = 0; x <= 10; x += 2) {
System.out.print(x + " ");
}
System.out.println();
// 2*0 2*1 2*2 2*3 2*4 2*5
// 2 * (0 1 2 3 4 5 )
for(int x = 0; x <= 5; x++) {
System.out.print(2 * x + " ");
}
System.out.println();
// 0 1 2 3 4 5 6 7 8 9 10
for(int x = 0; x <= 10; x++) {
if(x % 2 == 0) {
System.out.print(x + " ");
}
}
} // end main()
} // end ForMain02
package edu.java.for03;
public class ForMain03 {
public static void main(String[] args) {
System.out.println("for문 연습");
// 1 ~ 100까지 정수 중에서 10의 배수들만 출력
for(int x = 1; x <= 100; x++) {
if(x % 10 == 0) {
System.out.print(x + " ");
}
}
System.out.println();
// 1 2 3 4 5 6 7 8 9 10
// 11 12 13 14 15 16 17 18 19 20
// 21 22 23 24 25 26 27 28 29 30
// 31 32 33 34 35 36 37 38 39 40
// 41 42 43 44 45 46 47 48 49 50
// 51 52 53 54 55 56 57 58 59 60
// 61 62 63 64 65 66 67 68 69 70
// 71 72 73 74 75 76 77 78 79 80
// 81 82 83 84 85 86 87 88 89 90
// 91 92 93 94 95 96 97 98 99 100
for (int x = 1; x <= 100; x++) {
System.out.print(x + " ");
if(x % 10 == 0) {
System.out.println();
}
}
} // end main()
} // end ForMain03
package edu.java.for04;
public class ForMain04 {
public static void main(String[] args) {
System.out.println("for문 연습");
int sum1 = 0;
// 1. 1 ~ 10까지 더하시오.
for (int i = 1; i <= 10; i++) {
sum1 = sum1 + i;
}
System.out.println(sum1);
// 1 ~ 100까지의 숫자 중 짝수들의 합을 출력
// 2 + 4 + 6 + 8 + ... +96 + 98 + 100
// 1 ~ 100까지 반복한다.
// 그 중 짝수만 선택한다.
// 선택한 수를 합친다.
int sum2 = 0;
for (int i = 1; i <= 100; i++) {
if (i % 2 == 0) {
sum2 += i;
}
}
System.out.println(sum2);
/*
* 1꼬마 2꼬마 3꼬마 인디언 4꼬마 5꼬마 6꼬마 인디언 7꼬마 8꼬마 9꼬마 인디언 10꼬마 인디언 보이~
*/
for (int i = 1; i < 16; i++) {
if (i <= 10) {
System.out.print(i + "꼬마");
if (i % 4 == 0) {
System.out.print("인디언");
System.out.println();
}
} else if (i == 14) {
System.out.print("인디언");
} else if (i == 15) {
System.out.print("보이~");
}
}
} // end main()
} // end ForMain04
package edu.java.switch01;
import java.util.Scanner;
// switch - case 문
// - if문과 비슷하지만 비교 값을 정형화하는 방식
// - 변수와 일치하는 값에 따라 해당 case 문이 실행됨
// switch(변수) {
// case 값1 : ...
// break;
// case 값2 : ...
// break;
// default : ...
// break;
// }
public class SwitchMain01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
System.out.println("switch문");
// switch문
switch (a) {
case 1:
System.out.println("1번 선택");
break;
case 2:
System.out.println("2번 선택");
break;
default:
System.out.println("그 외 선택");
break;
}
} // end main()
} // end SwitchMain01
package edu.java.while01;
// while 문
// - for문과 같은 반복문
// - 선언 형태
// while(조건) {
// (본문)
// }
public class WhileMain01 {
public static void main(String[] args) {
System.out.println("while 반복문");
// 0 ~ 10까지 출력
int x = 0;
while (x <= 10) {
System.out.print(x + " ");
x++;
}
System.out.println();
// while문을 사용하여 10, 9, 8, ... 2, 1까지 출력하기
x = 10;
while (x >= 1) {
System.out.print(x + " ");
x--;
}
} // end main()
} // end WhileMain01
package edu.java.while02;
import java.util.Scanner;
public class WhileMain02 {
public static void main(String[] args) {
System.out.println("구구단 연습");
Scanner sc = new Scanner(System.in);
// int dan = sc.nextInt();
// int i = 1;
// while (i <= 9) {
// System.out.println(dan + " X " + i + " = " + dan * i);
// i++;
// }
// 2 ~ 9까지 반복
int dan = 2;
while (dan <= 9) {
System.out.println(dan + "단");
int i = 1;
while (i <= 9) {
System.out.println(dan + " X " + i + " = " + dan * i);
i++;
}
dan++;
}
// for문으로 구구단 출력하기
// 단 : 4 ~ 8
// 곱하는 수 : 2 ~ 7
for (int x = 4; x <= 8; x++) {
System.out.println(x + "단");
for (int y = 2; y <= 7; y++) {
System.out.println(x + "X" + y + "=" + (x * y));
}
}
int n = sc.nextInt();
// for (int i = 0; i < n; i++) {
// for (int j = 0; j < n; j++) {
// if (i >= j) {
// System.out.print("*");
// }
// }
// System.out.println();
// }
for (int i = 0; i < n / 2 + 1; i++) {
for (int j = 0; j < n; j++) {
if((i + j) < n / 2 || (j - i) >= n / 2 + 1) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
} // end main()
} // end WhileMain02
package edu.java.while03;
// do - while
// - 무조건 한 번 본문을 실행 한 후
// 반복문의 조건에 따라 반복 실행
// - 선언 형태
// do { 본문 }
// while (조건)
public class WhileMain03 {
public static void main(String[] args) {
int count = 0;
while(count > 0) {
System.out.println(count);
count--;
}
do {
System.out.println(count);
count--;
} while(count > 0);
} // end main()
} // end WhileMail03