반복문
반복문 while문
반복문 while문에 대한 문법 설명 1)
public class While01 {
public static void main(String[] args) {
int num=1;
//반복의 시작 시점 필수.
//While문은 ()조건을 비교하고 참이면 {}실행하고 다시 ()조건을 확인하고
// 거짓일시 {}실행하지 않고 빠져나온다.
while(num<3){
//()반복 조건 필수.
System.out.println("java");
num++;
//반복을 파기할 조건 필수.
}
System.out.println("끝");
}
}
반복문 while문에 대한 문법 설명 2)
public class While02 {
public static void main(String[] args) {
//안녕하세요를 10번 출력.
int i=0;
int count=0;
while (i<10){
System.out.println("안녕하세요");
i++;
count++;
}
System.out.println("출력된 갯수 : "+count);
}
}
반복문 while문에 대한 문법 설명 3)
public class While03 {
public static void main(String[] args) {
// 0부터 5까지 출력
int i=0;
while (i<6){
System.out.println(i);
i++;
}
System.out.println("출력된 갯수 : "+i);
}
}
반복문 while문에 대한 문법 설명 4)
public class While04 {
public static void main(String[] args) {
//2부터 10까지 짝수만 출력.
int i=0,count=0;
while (i<10){
i+=2;
System.out.println(i);
count++;
}
System.out.println("출력된 갯수 : "+count);
}
}
반복문 while문에 대한 문법 설명 5)
public class While05 {
public static void main(String[] args) {
int i=10; //i를 정수로 선언하고 값 10으로 초기화
//i가 10부터 0과 같아질때까지 반복
while (i>=0){
System.out.println(i);
i--;
}
}
}
반복문 while문에 대한 문법 설명 6)
import java.util.Scanner;
public class While06 {
public static void main(String[] args) {
//0부터 5까지의 합을 출력
Scanner sc=new Scanner(System.in);
int s, t;
int sum=0;
System.out.print("시작할 수를 입력하세요 : ");
s=sc.nextInt();
System.out.print("마지막 수를 입력하세요 : ");
t=sc.nextInt();
//시작할 수 s에 대해서 입력한 마지막수 t의 값까지 반복
while(s<=t){
sum+=s; //반복할때마다 sum의 값에 s값을 더함
s++;
}
System.out.println("모든정수의 합은 : "+sum);
}
}
==========================================================================================
반복문 for문
반복문 for문에 대한 문법 설명 1)
public class For01 {
public static void main(String[] args) {
for(int i = 1; i<3; i++){
System.out.println("java");
}
//위에 문법에서 int i=1(선언 한번만 해석) -> i<3 -> 출력문 -> i++ ->
// -> 반복 조건이 맞을때까지(조건문(i<3)-> 출력문-> i++)
}
}
반복문 for문에 대한 문법 설명 2)
public class For02 {
public static void main(String[] args) {
//i의 값이 0부터 10까지 출력
int count=0;
for (int i=0; i<11; i++, count++) {
System.out.println(i);
}
System.out.println("출력된총갯수는 : " + count);
}
}
반복문 for문에 대한 문법 설명 3)
public class For03 {
public static void main(String[] args) {
//i가 0에서 10까지 반복할 때 sum값에 해당 i값을 반복하여 더한다.
int sum=0;
for (int i = 0; i < 11; i += 1) {
sum += i;
}
System.out.println(sum);
}
}
반복문 for문에 대한 문법 설명 4)
import java.util.Scanner;
public class For04 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x, result=0;
System.out.print("알고싶은 구구단의 단수를 입력하세요 : ");
x=sc.nextInt();
//i가 1부터 9까지 반복할 때 알고싶은 구구단의 단수 x에 i값을 곱하여 result값에 대입.
for (int i=1; i<10; i++){
result=x*i;
System.out.println(x+" * "+i+" = "+result);
}
}
}
==========================================================================================
반복문에 대한 무한loop
반복문 while과 for문의 무한루프 문법 설명)
public class InfiniteLoop01 {
public static void main(String[] args) {
//while문의 무한루프
//while (true){
}
//for문의 무한루프
//for(; ; ){
}
반복문 while의 무한루프 문법 예제)
public class Infinite02 {
public static void main(String[] args) {
//점수를 입력받는데 점수가 0~100 사이의 수를 받을 동안
//계속해서 점수를 입력 받는 코드
Scanner sc=new Scanner(System.in);
int score;
while (true){
System.out.print("점수를 입력하세요 : ");
score=sc.nextInt();
if(score<=100&&score>=0){
break;
}
}
}
}
==========================================================================================
예제
예제 1-1)
//System.out.print("*");을 사용하여 별을 다섯 개 출력해보자. 단 출력문은 한번만 사용해야한다.
//while문을 사용하여 풀어보아라.
public class Ex1_1 {
public static void main(String[] args) {
int i=0;
String x="*";
while(i<5){
System.out.println(x);
i++;
}
}
}
예제 1-2)
//Ex1_1의 문제를 for문으로 사용하여 풀어보아라.
public class Ex1_2 {
public static void main(String[] args) {
for(int i=0; i<5; i++){
System.out.println("*");
}
}
}
예제 1-3)
//출력문을 한번만 사용하여 0~5까지 출력하되, while문을 사용하여라.
public class Ex1_3 {
public static void main(String[] args) {
int i=0;
while(i<6){
System.out.println(i);
i++;
}
}
}
예제 1-4)
//Ex1_3문제를 for문을 사용하여 풀어보아라
public class Ex1_4 {
public static void main(String[] args) {
for(int i=0; i<6; i++){
System.out.println(i);
}
}
}
예제 1-5)
//출력문을 한번만 사용하여 '2 4 6 8 10'을 출력하되 , while 문을 사용하여라.
public class Ex1_5 {
public static void main(String[] args) {
int i=0;
while(i<5){
i++;
System.out.println(i*2);
}
}
}
예제 1-6)
//Ex1_5문제를 for문을 사용하여 풀어보아라
public class Ex1_6 {
public static void main(String[] args) {
for (int i=1; i<6; i++){
System.out.println(i*2);
}
}
}
예제 1-7)
//출력문을 한번만 사용하여 10~0까지 출력하되, while문을 사용하여라
public class Ex1_7 {
public static void main(String[] args) {
int i=10;
while(i>=0){
System.out.println(i);
i--;
}
}
}
예제 1-8)
//Ex1_7문제를 for문을 사용하여 풀어보아라
public class Ex1_8 {
public static void main(String[] args) {
for (int i=10; i>=0; i--){
System.out.println(i);
}
}
}
예제 1-9)
//while문을 사용하여 0~10까지의 합을 구하여라.
public class Ex1_9 {
public static void main(String[] args) {
int i=0;
int sum=0;
while(i<11){
sum+=i;
i++;
}
System.out.println(sum);
}
}
예제 1-10)
//Ex1_9문제를 for문을 사용하여 풀어보아라
public class Ex1_10 {
public static void main(String[] args) {
int sum=0;
for (int i=0; i<11; i++){
sum+=i;
}
System.out.println(sum);
}
}
예제 1-11)
//1~100까지의 숫자 중 3의 배수인 수의 개수를 while문을 사용하여 구하여라.
//1~100중 3의 배수의 개수 출력
//1.1~100까지 3의 배수인지를 검사한다.
//2.만약 3의 배수라면 3의 배수의 개수를 1증가시킨다.
import java.util.Scanner;
public class Ex1_11 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i;
int x=1;
int d;
int count=0;
System.out.print("원하는 숫자를 입력하세요 : ");
i=sc.nextInt();
System.out.print("찾고자하는 배수를 입력하세요 : ");
d=sc.nextInt();
while(i>=x){
if(x%d==0){
x++;
count++;
}
else {
x++;
}
}
System.out.println("총 배수의 개수는 : "+count);
}
}
예제 1-12)
//Ex1_11문제를 for문을 사용하여 풀어보아라
import java.util.Scanner;
public class Ex1_12 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i;
int d;
int count=0;
System.out.print("원하는 숫자를 입력하세요 : ");
i=sc.nextInt();
System.out.print("찾고자하는 배수를 입력하세요 : ");
d=sc.nextInt();
for(int x=1; x<=i; x++){
if(x%d==0){
count++;
}
}
System.out.println("1~"+i+" 의 "+d+" 의 배수의 총 개수는 "+count);
}
}
예제 1-13)
//1~100까지의 숫자 중 5의 배수인 수를 모두 출력하고, 5의 배수인 수의 개수도 출력하되, while문을 사용하여라.
import java.util.Scanner;
public class Ex1_13 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=1, count=0;
int d, i;
System.out.print("원하는 숫자를 입력하세요 : ");
i=sc.nextInt();
System.out.print("찾고자하는 배수를 입력하세요 : ");
d=sc.nextInt();
while(i>=x){
if(x%d==0){
System.out.println(x);
count++;
x++;
}
else {
x++;
}
}
System.out.println("1~"+i+" 까지의 "+d+" 의 배수의 총 개수는 "+count);
}
}
예제 1-14)
//Ex1_13문제를 for문을 사용하여 풀어보아라
import java.util.Scanner;
public class Ex1_14 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=0;
int d, i;
System.out.print("원하는 숫자를 입력하세요 : ");
i=sc.nextInt();
System.out.print("찾고자하는 배수를 입력하세요 : ");
d=sc.nextInt();
for(int x=1; x<=i; x++){
if(x%d==0){
System.out.println(x);
count++;
}
}
System.out.println("1~"+i+" 까지의 "+d+" 의 배수의 총 개수는 "+count);
}
}
예제 1-15)
//임의의 정수를 입력받아 0부터 입력받은 수까지 중 짝수의 개수를 구하여라. while문 사용
import java.util.Scanner;
public class Ex1_15 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i, j=1, count=0;
System.out.print("원하는 정수를 입력하세요 : ");
i=sc.nextInt();
while(i>=j){
if(j%2==0){
count++;
j++;
}
else {
j++;
}
}
System.out.println("1~"+i+" 까지의 짝수의 총 개수는 "+count);
}
}
예제 1-16)
//Ex1_15문제를 for문을 사용하여 풀어보아라
import java.util.Scanner;
public class Ex1_16 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i, count=0;
System.out.print("원하는 정수를 입력하세요 : ");
i=sc.nextInt();
for(int j=1; j<=i; j++){
if(j%2==0){
count++;
}
}
System.out.println("1~"+i+" 까지의 짝수의 총 개수는 "+count);
}
}
예제 2-1)
//1이상 100미만의 정수 중에서 7의 배수와 9의 배수를 출력하는 프로그램을 작성하여라.
// 단, 7의 배수이면서 동시에 9의 배수인 수는 한번만 출력하여야 한다.
import java.util.Scanner;
public class Ex2_4 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i, j=0;
int d1, d2, d3=0;
System.out.print("원하는 정수를 입력하세요 : ");
i=sc.nextInt();
System.out.print("원하는 첫번째 배수를 입력하세요 : ");
d1=sc.nextInt();
System.out.print("원하는 두번째 배수를 입력하세요 : ");
d2=sc.nextInt();
for(j=1; j<=i; j++){
if(j%d1==0){
System.out.println(j);
}
else if(j%d2==0){
System.out.println(j);
}
}
}
}
예제 2-2)
//키보드로 두 정수를 입력받아 입력받은 두 정수 사이의 모든 정수의 합을 출력하여라.
import java.util.Scanner;
public class Ex2_5 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int i, j, x;
int sum=0;
System.out.print("첫번째 원하는 정수를 입력하세요 : ");
i=sc.nextInt();
System.out.print("두번째 원하는 정수를 입력하세요 : ");
j=sc.nextInt();
for(x=i; x<=j; x++){
sum+=x;
}
System.out.println(i+" 부터 "+j+" 정수의 사이의 모든 정수의 합은 "+sum);
}
}
예제 2-3)
//1부터 누적해서 합을 구하다 300이 최초로 넘었을때
//for문을 멈추고 그때까지의 합과 마지막으로 더해진 값을 각각 출력하여라
import java.util.Scanner;
public class Ex2_6 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int sum=0;
int i; //입력받을 값
int j=1;
System.out.print("숫자를 입력하세요 : ");
i=sc.nextInt();
for(j=1; j<i; j++){
sum+=j;
if(sum>i){
break;
}
}
System.out.println(" 합 "+sum+" 마지막으로 더해진 값 "+j);
}
}
23.01.19일 4일차 끝!
'코딩 > JAVA' 카테고리의 다른 글
23.01.25 (이차원배열, switch문, 문자열비교) (2) | 2023.01.29 |
---|---|
23.01.20 (배열) (0) | 2023.01.29 |
23.01.18 (조건문if, 키보드입력문) (0) | 2023.01.27 |
23.01.17 (기본 자료형, 연산자, 상수) (0) | 2023.01.27 |
23.01.16 (출력문, 변수) (0) | 2023.01.27 |
댓글