KH 8일차
새로운 한주를 시작하면서, 8일차 Start!
오늘은 For, While, DoWhile, Return, break, Continue문에 대해서 공부하고 관련 예제들을 학습했다.
오늘 선생님께서 제시해주신 여러 문제가 생각보다 어렵고 풀리지 않았다.
반복, 반복, 반복많이 살길이라고 계속 강조하는거보니 정말로 열심히 반복해야겠다.
class While_1
{
public static void main(String[] args)
{
int cnt = 0;
int sum = 0;
int even = 0;
int odd = 0;
// int even = 0, odd = 0;
while(cnt < 100)
{
cnt ++;
if (cnt % 2 == 0)
{
even += cnt;
}
else
{
odd += cnt;
}
System.out.println(" cnt = " + cnt);
sum += cnt;
// sum += cnt++;
}
System.out.println(" sum = " + sum);
System.out.println(" 짝수의 합은 = " + even);
System.out.println(" 홀수의 합은 = " + odd);
}
}
While문 while(조건식) 으로 이루어져 있고, 조건식이 거짓이 될 때까지 반복한다. 중복 While문을 사용할 수 있으며 While 문 속에 if문을 결합할 수 있다.
class DoWhile_1
{
public static void main(String[] args)
{
int cnt = 1, sum = 0, even=0, odd=0;
do
{
sum += cnt;
if(cnt % 2 == 0){
even += cnt;
}
else{
odd += cnt;
}
cnt ++;
}
while (cnt <= 100);
System.out.println(" 1 + 2 + 3 + 4 + 5 ~ 100 = " + sum);
System.out.println(" 2 + 4 + 6 ~ 100 = " + even);
System.out.println(" 1 + 3 + 5 ~ 99 = " + odd);
}
}
DoWhile문은 While문과 다르게 조건식이 거짓이라도 do블럭에 있는 코드들은 무조건 한번은 실행이 된다는 특징을 가지고있다.
class Return_1
{
public static void main(String[] args)
{
// 메소드는 2개가 있다. return 받는 메소드(static int)와 return 받지 않는 메소드(static void)가 있다.
// retrun 받지 않는 메소드(static void)는 return 값이 필요없다.
// void 메소드는 return 받지 않는다.
sum(15, 27); //덧셈
sub(33, 27); //뺄셈
mul(23, 7); //곱셈
div(35, 4); // 나눗셈
/*
System.out.println(" 15 + 27 = " + result1);
System.out.println(" 33 - 27 = " + result2);
System.out.println(" 23 * 7 = " + result3);
System.out.printf(" 35 /4 = %7.3f \n" , result4);
*/
}
//변수 뒤에 괄호가 있는것을 메소드라고 한다.
//void는 return 이 없음 !
static void sum(int x, int y)
{
System.out.println(" 15 + 27 = " + (x + y));
}
static void sub(int x, int y)
{
System.out.println(" 33 - 27 = " + (x - y));
}
static void mul(int x, int y)
{
System.out.println(" 23 * 7 = " + (x * y));
}
static void div(int x, int y)
{
System.out.printf(" 35 /4 = %7.3f \n" , (x / (double)y));
}
}
자바에는 두 종류의 메소드가 존재한다. static void로 시작하여 return값을 필수로 받지 않는 메소드와, static int 등의 변수 형태를 받고 return값을 필수로 받는 메소드 두 종류이다.
class Break_1
{
public static void main(String[] args)
{
int sum = 0;
int i;
int cnt = 0;
// 1 ~ 100 사이의 3의 배수의 합이 200을
// 넘는 순간의 3의 배수의 값은?
for (i = 1;i <= 100 ;i++ )
{
if (i % 3 == 0)// 3의 배수
{
cnt ++;
sum += i;
System.out.print(i + "\t");
if(cnt % 3 ==0){
System.out.println();
}
if (sum > 200)
{
//가장 가까운 for문을 벗어난다는 뜻 if문을 벗어난다는 것이 아님 !
break;
}
}
}
System.out.println("\n 합 :" + sum);
System.out.println("\n 3의 배수 :" + i);
}
}
break문은 가장 가까운 for문을 벗어난다는 의미를 가지고 있다. **if문**을 벗어나는것이 아니기 때문에 유념해야한다!!
class Continue_1
{
public static void main(String[] args)
{
int sum = 0;
for (int i =0;i<10 ;i++ )
{
if (i % 2 ==0)
{
//continue가 있으면 반복문의 끝으로 감 => 증감값으로 바로 =>
continue;
}
sum += i;
System.out.print("\t" + i);
}
System.out.println("\n 1 ~ 10 사이의 홀수의 합 :" + sum);
}
}
Continue을 만나면 코드는 반복문에 끝으로 바로 이동한다. 끝으로 이동한 코드는 바로 증감값으로 이동하게 되고 다시 if문을 거쳐 적합하게 되는 순간 반복문의 끝으로 보내기 때문에 if문에 적합하지 않은 경우 즉 1~10까지의 홀수의 합들만 출력이 되는 구조이다.
과제 1. While, DoWhile, For문을 활용하여 구구단을 만드시오.
public class Test_01 {
public static void main(String[] args) {
for (int i = 2; i < 10; i++) {
for (int j = 1; j < 10; j++) {
System.out.println(i + " * " + j + " = " + (i*j));
if(j >=9) {
System.out.println();
}
}
}
}
}
public class Test_01 {
public static void main(String[] args) {
int i = 2;
int j = 1;
while(i<10)
{
while(j<10)
{
System.out.println(i + "*" + j + "=" + (i*j));
j++;
if(j >= 10) {
System.out.println();
}
}
i++;
j = 1;
}
}
}
public class Test_01 {
public static void main(String[] args) {
int i = 2;
int j = 1;
do {
do {
System.out.println(i + "*" + j + "=" + (i*j));
j++;
if(j >=10) {
System.out.println();
}
} while (j<10);
i++;
j = 1;
} while (i<10);
}
}
과제 2. While, DoWhile, For문을 활용하여 구구단을 만드시오.(Scanner을 활용해서 2개의 숫자를 받고 받은 2개의 숫자로 이뤄진 구구단을 만드시오.
import java.util.Scanner;
public class Test_01 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("첫 번째 단");
int first = sc.nextInt();
System.out.println("두 번째 단");
int second = sc.nextInt();
if(first > second) {
int temp = first;
first = second;
second = temp;
}
for (int i = first; i <= second; i++) {
for (int j = 1; j < 10; j++) {
System.out.println(i + "*" + j + "=" + (i*j));
if(j >= 9) {
System.out.println();
}
}
}
}
}
import java.util.Scanner;
public class Test_01 {
public static void main(String[] args) {
int j = 1;
Scanner sc = new Scanner(System.in);
System.out.println("첫 번째 단");
int first = sc.nextInt();
System.out.println("두 번째 단");
int second = sc.nextInt();
if(first > second) {
int temp = first;
first = second;
second = temp;
}
while(first<=second)
{
while(j < 10) {
System.out.println(first + "*" + j + "=" + (first*j));
j++;
if(j >= 10) {
System.out.println();
}
}
first ++;
j = 1;
}
}
}
public class Test_01 {
public static void main(String[] args) {
int j = 1;
Scanner sc = new Scanner(System.in);
System.out.println("첫 번째 단");
int first = sc.nextInt();
System.out.println("두 번째 단");
int second = sc.nextInt();
if(first > second) {
int temp = first;
first = second;
second = temp;
}
do {
do {
System.out.println(first + "*" + j + "=" + (first*j));
j++;
if(j>9) {
System.out.println();
}
} while (j < 10);
first++;
j = 1;
} while (first < second+1);
}
}
과제 3. While문으로 해결하시오!
// 13m 우물속에 달팽이가 살고 있습니다.
// 이 달팽이는 하루에 3m를 오르고 밤에는 1m 미끄러집니다.
// 이 달팽이가 우물에서 탈출하는 날짜는 며칠입니까.
import java.util.Scanner;
public class Test_01 {
public static void main(String[] args) {
int i = 0;
int j = 0;
while(i>=0)
{
i += 3; // 3 // 5
j++;
if(i>13)
{
break;
}
i--; // 2
System.out.println("달팽이는 " + j +"일째에 탈출합니다.");
}
}
}