package day008;
public class Day00801반복문 {
public static void main(String[] args) {
//변수명(index :: 순서)
int i = 1;//초기식
while(i <= 5) //조건식
{
System.out.println(i);
i = i + 1; //증감식
}
// 증감식의 종류
// 1.i = i + 1;
// 2.i += 1
// 3. i++;
// 4. ++i;
}
}
package day008;
public class Day00802반복문기본 {
public static void main(String[] args) {
// 문제 1. 1~10까지 반복해 5~9출력
// 답. 5, 6, 7, 8, 9
int i = 1;
while(i <= 10) {
if(i >= 5 && i <= 9) {
System.out.print(i + " ");
}
i=i+1;
}
System.out.println(" ");
System.out.println(" ");
System.out.println("mission 2");
//문제 2. 10 ~ 1까지 반복해서 6 ~ 3 거꾸로 출력
// 6, 5, 4, 3
i = 10;
while(i >= 1) {
if(i <= 6 && i >=3) {
System.out.print(i + " ");
}
i = i-1;
}
System.out.println(" ");
System.out.println(" ");
System.out.println("mission 3 ");
//mission3. 1~ 10 까지 짝수 출력
//답 2, 4, 6, 8, 10
i = 1;
while(i <= 10) {
if(i % 2 == 0) {
System.out.println(i);
}
i = i+1;
}
}
}
package day008;
public class Day00803반복문기본2 {
public static void main(String[] args) {
//문제 1 1~5까지의 합 출력
//정답 1. 15
int i = 1;
int total = 0;
while(i <= 5) {
total = total + i;
i = i + 1;
}
System.out.println(total);
//문제 2 . 1~ 10 까지 반복해 3미만 7이상만 출력
// 답 1, 2, 7 8 9 10
i = 1;
while(i<=10) {
if(i < 3|| i >= 7) {
System.out.println(i);
}i = i + 1;
}
//문제 3 문제2의 조건에 맞는 수들의 합
// 답 37
i = 1;
total = 0;
while(i<=10) {
if(i < 3|| i >= 7) {
total = total + i;
}i = i + 1;
}
System.out.println(total);
// 문제4 문제2의 조건에 맞는 수들의 개수
// 6개
i = 1;
int count = 0;
while(i<=10) {
if(i < 3|| i >= 7) {
count = count + i;
}i = i + 1;
}
System.out.println(count);
}
}
package day008;
public class Day00804반복문복습 {
public static void main(String[] args) {
//문제 1. 0~10 중에 홀수만 출력
// 1, 3, 5, 7, 9
System.out.println("mission 1");
System.out.println();
int i = 0;
while(i <= 10) {
if(i % 2 == 1) {
System.out.print(i + ", ");
}
i = i + 1;
}
System.out.println();
System.out.println("mission 2");
System.out.println();
//문제 2. 0~20 중에 3의 배수이면서 4의 배수인 수만 출력
// 2. 12
i = 0;
while(i <= 20) {
if(i != 0 && i % 3 == 0 && i % 4 == 0){
System.out.print(i);
}
i = i +1;
}
System.out.println();
System.out.println("mission 3");
System.out.println();
//3. 0 ~ 10 중에 2의 배수의 합 출력
// 30
i = 0;
int total = 0;
while(i <= 10) {
if(i % 2 == 0) {
total = total + i;
}
i = i + 1;
}
System.out.print(total);
System.out.println();
System.out.println("mission 4");
System.out.println();
//4. 0~10 중에 3의 배수의 개수출력
// 3
i = 0;
int count = 0;
while(i <= 10) {
if(i % 3 == 0 && i != 0) {
count = count + 1;
}
i = i + 1;
}
System.out.print(count);
}
}
package day008;
import java.util.Random;
import java.util.Scanner;
public class Day00805구구단 {
public static void main(String[] args) {
/*
* 구구단 3
* 1. 구구단을 5회 반복한다.
* 2. 정답을 맞추면 20점이다.
* 3. 게임 종료 후, 성적을 출력한다.
*/
Scanner scan = new Scanner(System.in);
Random ran = new Random();
int cnt = 0;
int i = 1;
while(i<=5) {
int x = ran.nextInt(8)+2;
int y = ran.nextInt(9)+1;
System.out.println(x + "x" + y + " = ?? ");
int ans = x * y;
int my =scan.nextInt();
if(my == ans) {
System.out.println("정답");
cnt++;
}
else {
System.out.println("오답");
}
i++;
}
int score = cnt * 20;
System.out.println(score +"점 입니다.");
}
}
package day008;
import java.util.Random;
import java.util.Scanner;
public class Day00805구구단 {
public static void main(String[] args) {
/*
* 구구단 3
* 1. 구구단을 5회 반복한다.
* 2. 정답을 맞추면 20점이다.
* 3. 게임 종료 후, 성적을 출력한다.
*/
Scanner scan = new Scanner(System.in);
Random ran = new Random();
int cnt = 0;
int i = 1;
while(i<=5) {
int x = ran.nextInt(8)+2;
int y = ran.nextInt(9)+1;
System.out.println(x + "x" + y + " = ?? ");
int ans = x * y;
int my =scan.nextInt();
if(my == ans) {
System.out.println("정답");
cnt++;
}
else {
System.out.println("오답");
}
i++;
}
int score = cnt * 20;
System.out.println(score +"점 입니다.");
}
}