프로그래밍/JAVA 자바

do-while문 / for문 / 별찍기 / 가위바위보

Heidong 2021. 7. 9. 00:45
반응형

do - while 문

package ex0708;

public class Ex01_do_while {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int s,n;
		
		s=n=0;
		
		do {
			n++;
			s+=n;
		} while(n<100);
		System.out.println(n + ","+ s);

	}

}


// while과 다른건 조건이 밑에있다.
// while -> while이 참일 동안 괄호 안의 내용을 반복해라
// do while -> do의 괄호 내용을 반복해라 while이 참일 동안
package ex0708;

public class Ex02_do_while {

	public static void main(String[] args) {
		// while 과 do while의 차이
		
		int a = 10;
		
		
		while(a<10) {
			a++;
		}
		System.out.println("a:"+a);
		
		
		
		
		int b = 10;
		do {
			b++;
		}while(b<10); //세미콜론 붙임
		System.out.println("b:"+b);
	}

}

 

do while문 이용해서

2~9사이의 수를 입력 받아 입력 받은 수의 구구단 구하기
2~9외의 수는 다시 입력하기

 

package ex0708;

import java.util.Scanner;

public class Ex03_do_while {

	public static void main(String[] args) {
		// 2~9사이의 수를 입력 받아 입력 받은 수의 구구단 구하기
		// 2~9외의 수는 다시 입력하기
		
		
		Scanner sc = new Scanner(System.in);
		
		int dan;
		int n;
		
		/*
		System.out.println("단 입력 : ");
		dan = sc.nextInt();
		
		while(dan<2 || dan>9) {
			System.out.println("단 입력 : ");
			dan = sc.nextInt();
		}
		*/
		
		//while은 조건이 만족하지 않으면 한번도 실행하지 않음
		//do while은 한번 실행하고 조건을 봄
		
		do {
			System.out.println("단 입력 : ");
			dan = sc.nextInt();
		} while(dan<2 || dan>9);
		
		n = 0;
		while(n<9) {
			n++;
			System.out.printf("%d * %d = %d\n", dan, n, dan*n);
		}
		
		
		sc.close();

	}

}

 

 

 

 

do while문은 아니지만

while문으로 구구단 가로로 출력 해보기

 

package ex0708;

public class Testttttt {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int dan,n;
		n=0;
		
		while(n<9) {
			n++;
			dan=2;
			while(dan<=9) {
				System.out.printf("%d * %d = %2d\t",dan,n,dan*n);
				dan++;
			}
			System.out.println();
		}

	}

}

 

 

난수를 발생시켜 보자

 

(int)(Math.random()*100+1); //1~100까지 숫자를 랜덤으로 생성하는 내장 함수

 

package ex0708;

import java.util.Scanner;

public class Ex06_Q6 {

	public static void main(String[] args) {
		// 1~100사이 난수를 발생시켜 몇번에 맞추는지 출력
		
		
		Scanner sc = new Scanner(System.in);
		
		int com, user, cnt;
		
		com = (int)(Math.random()*100+1);
		cnt=0;
		
		while(true) {
			System.out.print("수 ? ");
			user = sc.nextInt();
			cnt++;
			if(com==user) {
				System.out.println(cnt + "번만에 성공");
				break;
			}else if(com > user) {
				System.out.println("입력된 수보다 큼");
			}else {
				System.out.println("입력된 수보다 적음");
			}
		}
		
		sc.close();

	}

}

 

 

 

for문의 기본 골자

package ex0708;

public class Ex09_for {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		/*
		int n,s;
		for(n=1, s=0; n<=10; s+=n, n++);//초기화 및 증감식은 ,콤마로 여러문장 기술 가능
		System.out.println(n+","+s);
		*/
		
		
		int n,s;
		n=1;
		s=0;
		
		for(; ;) { //증가식에 아무것도 없으면 무한 루프
			s+=n;
			n++;
			if(n>10) break;
		}
		System.out.println(n+","+s);

	}

}

 

정수 입력 받아 1에서 입력 받은 수까지 합 구함

package ex0708;

import java.util.Scanner;

public class Ex12_for {

	public static void main(String[] args) {
		// 정수 입력 받아 1에서 입력 받은 수까지 합 구함
		
		Scanner sc = new Scanner(System.in);
		
		int num,s;
		
		
		do {
			System.out.println("수 : ");
			num = sc.nextInt();
		} while(num<1 || num >1000);
		
		
		s=0;
		for(int n=1; n<=num; n++) {
			s+=n;
		}
		
		System.out.printf("1~%d까지 합 : %d\n", num, s);
		
		
		sc.close();

	}

}

 

 

1~9 사이의 정수를 입력받아 입력 받은 수의 구구단 출력

package ex0708;

import java.util.Scanner;

public class Ex13_for {

	public static void main(String[] args) {
		// 1~9 사이의 정수를 입력받아 입력 받은 수의 구구단 출력
		
		Scanner sc = new Scanner(System.in);
		
		int dan;
		
		do {
			System.out.println("정수 입력: ");
			dan = sc.nextInt();
		}
		while(dan<1 || dan>9);
		
		for(int n = 1; n<=9; n++) {
			System.out.printf("%d * %d = %d\n",dan,n,dan*n);
		}
		
		sc.close();

	}

}

 

 

10개의 정수를 입력 받아 입력 받은 정수 중 짝수 갯수와 홀수 갯수를 출력

package ex0708;

import java.util.Scanner;

public class Ex14_for {

	public static void main(String[] args) {
		// 10개의 정수를 입력 받아 입력 받은 정수 중 짝수 갯수와 홀수 갯수를 출력

		Scanner sc = new Scanner(System.in);
		
		int input;
		int even, odd;
		even = odd = 0;
		
		System.out.println("10개의 정수 입력 : ");
		for(int n=1; n<=10; n++) {
			input = sc.nextInt();
			if(input%2==0) {
				even++;
			}else {
				odd++;
			}
		}
		System.out.printf("짝수의 개수 : %d, 홀수의 개수 : %d\n", even,odd);
		
		sc.close();
	}

}

 

 

 

10개의 정수를 입력 받아 입력 받은 정수 중 카장 큰 값을 출력해라

package ex0708;

import java.util.Scanner;

public class Ex15_for {

	public static void main(String[] args) {
		// 10개의 정수를 입력 받아 입력 받은 정수 중 카장 큰 값을 출력해라
		
		Scanner sc = new Scanner(System.in);
		
		int input;
		int max=0;
		
		System.out.println("10개의 정수 입력 : ");
		for(int n=1; n<=10; n++) {
			input = sc.nextInt();
			if(n==1) { // 처음 입력 받은 경우 최대치방에 초기화함
				max = input;
			} else if(max < input) {
				max = input;
			}
			
		}
		System.out.println(max);
		
		
		sc.close();
	}

}

 

 

 

10개의 정수를 입력 받아 입력 받은 정수 중 가장 작은 값을 출력해라

package ex0708;

import java.util.Scanner;

public class Ex16_for {

	public static void main(String[] args) {
		// 10개의 정수를 입력 받아 입력 받은 정수 중 가장 작은 값을 출력해라
		
		Scanner sc = new Scanner(System.in);
		
		int input;
		int min=0;
		
		System.out.println("10개의 정수 입력 : ");
		for(int n=1; n<=10; n++) {
			input = sc.nextInt();
			if(n==1) { // 처음 입력 받은 경우 최대치방에 초기화함
				min = input;
			} else if(min > input) {
				min = input;
			}
			
		}
		System.out.println(min);
		
		
		sc.close();
	}

}

 

 

 

두 개의 정수를 입력받아 입력 받은 수중 적은 수에서 큰수 사이에 3의 배수이거나 5의 배수인 수들의 합과 평균을 구하라

package ex0708;

import java.util.Scanner;

public class Ex17_for {

	public static void main(String[] args) {
		// 두 개의 정수를 입력받아 입력 받은 수중 적은 수에서 큰수 사이에 3의 배수이거나 5의 배수인 수들의 합과 평균을 구하라

		Scanner sc = new Scanner(System.in);
		
		int a,b,t;
		int tot, cnt;
		
		System.out.println("두 수 입력 : ");
		a = sc.nextInt();
		b = sc.nextInt();
		
		if(a>b) {
			t=a; a=b; b=t;
		}
		
		
		tot = cnt = 0;
		for(int n=a; n<=b; n++) {
			if(n%3==0 || n%5==0) {
				tot+=n;
				cnt++;
			}
		}
		System.out.println("합 : "+ tot + "평균 : "+ (tot/cnt));
		
		sc.close();
		
		
	}

}

 

 

 

다중 for문 구구단 출력

package ex0708;

public class Ex19_for {

	public static void main(String[] args) {
		// 다중 for문 구구단 출력

		
		for(int i=2; i<=9; i++) {
			System.out.println("** "+i+"단 **");
			for(int j=1; j<=9; j++) {
				System.out.printf("%d*%d=%d\n",i,j,i*j);
			}
			System.out.println("------------------");
		}
	}

}

i = 2단 3단 4단 ...

j = 1~9

 

 

 

5개의 정수를 입력받아 합 구하기
단 입력 받은수가 0이하이면 재입력

package ex0708;

import java.util.Scanner;

public class Ex21_for {

	public static void main(String[] args) {
		// 5개의 정수를 입력받아 합 구하기
		// 단 입력 받은수가 0이하이면 재입력

		
		Scanner sc = new Scanner(System.in);
		
		int input=0;
		int s;
		
		/*
		s = 0;
		System.out.println("5개 정수 입력");
		for(int n=1; n<=5; n++) {
			do {
				System.out.print(n+"번째수 : ");
				input = sc.nextInt();
			}while(input <= 0);
			
			s+=input;
		}
		System.out.println("결과 : " + s);
		*/
		
		
		s = 0;
		System.out.println("5개 정수 입력");
		for(int n=1; n<=5; n++) {
			System.out.print(n+"번째수 : ");
			input = sc.nextInt();
			if(input <= 0) {
				n--;
			}else {
				s+=input;
			}
			
		}
		System.out.println("결과 : " + s);
		
		
		
		sc.close();
	}

}

 

 

 

별 찍기

package ex0708;

public class Ex22_for {

	public static void main(String[] args) {
		//별 찍기
		/*
		for(int i=1; i<=5; i++) {
			for(int j=1; j<=i; j++) {
				System.out.printf("*");
			}
			System.out.println();
		}
		*/
		
		/*
		for(int i=5; i>=1; i--) {
			for(int j=1; j<=i; j++) {
				System.out.printf("*");
			}
			System.out.println();
		}
		*/
		/*
		for(int i=1; i<=5; i++) {
			for(int j=1; j<=5-i; j++) {
				System.out.print(" ");
			}
			for(int j=1; j<=i; j++) {
				System.out.printf("*");
			}
			System.out.println();
		}
		*/
		
		for(int i=1; i<=5; i++) {
			for(int j=1; j<=5-i; j++) {
				System.out.print(" ");
			}
			for(int j=1; j<=i*2-1; j++) {
				System.out.printf("*");
			}
			System.out.println();
		}

	}

}

주석 처리 안된     코드의 결과

첫번째 for문으로 공백을 주고

두번째 for문으로 별을 출력

 

공백을 첫줄에 4칸 두번째 줄 3칸 이런식으로 공백의 횟수가 늘어남

 

별은 그 공백 뒤에 하나씩 출력하는거임

 

공백4칸 + 별 한개

공백3칸 + 별 두개

공백2칸 + 별 세개

 

이런식으로 내려간다.

 

 

두 주사위의 눈의 합이 9가 되는 경우를 모두 출력

package ex0708;

public class Ex23_for {

	public static void main(String[] args) {
		//두 주사위의 눈의 합이 9가 되는 경우를 모두 출력
		
		for(int i = 1; i<=6; i++) {
			for(int j = 1; j<=6; j++) {
				if(i+j==9) {
					System.out.println(i+" "+j);
				}
			}
			
		}
	}

}

 

 

1~10까지합, 11~20까지합, ... 91~100까지 합

package ex0708;

public class Ex24_for {

	public static void main(String[] args) {
		// 1~10까지합, 11~20까지합, ... 91~100까지 합
		int s;
	
		for(int i = 1; i<=100; i+=10) {
			//System.out.println(i+ "," + (i+9));
			s = 0;
			for(int j=i; j<=i+9; j++) {
				s+=j;
			}
			System.out.printf("%d~%d까지 합 = %d\n", i, i+9, s);
			
		}

	}

}

 

 

 

1~100까지 수중 4와 6의 공배수를 출력

package ex0708;

public class Ex25_for {

	public static void main(String[] args) {
		// 1~100까지 수중 4와 6의 공배수를 출력
		
		 for(int i=1; i<=100; i++) {
			 if(i%4==0 && i%6==0) {
				 System.out.print(i+"    ");
			 }
		 }
		 System.out.println();

	}

}

 

 

 

5개의 정수 입력받아 7에 가장 가까운 수 출력
겹치면 먼저 입력한 수 먼저

 

package ex0708;

import java.util.Scanner;

public class Ex26_for {

	public static void main(String[] args) {
		// 5개의 정수 입력받아 7에 가장 가까운 수 출력
		// 겹치면 먼저 입력한 수 먼저
		
		Scanner sc = new Scanner(System.in);
		int n, diff, min=0, result=0;
		
		System.out.println("5개 정수 입력");
		for(int i=1; i<=5; i++) {
			n = sc.nextInt();
			diff = n > 7 ? n - 7 : 7 - n;
			if(i==1 || min > diff) {
				min = diff;
				result = n;
			}

		}System.out.println(result);
		
		sc.close();

	}
	
	

}

 

 

 

 

방정식 3x+6y=27의 모든 해를 구하라 
x,y는 정수이고 0이상 10이하임

package ex0708;

public class Ex27_for {

	public static void main(String[] args) {
		// 방정식 3x+6y=27의 모든 해를 구하라 
		// x,y는 정수이고 0이상 10이하임
		
		for(int x=1; x<=10; x++) {
			for(int y=1; y<=10; y++) {
				if(3*x + 6*y == 27) {
					System.out.println(x+","+y);
				}
			}
		}

	}

}

 

 

a+b+c=25 를 만족하는 한자리 자연수 abc를 모두 구해라

package ex0708;

public class Ex28_for {

	public static void main(String[] args) {
		// a+b+c=25 를 만족하는 한자리 자연수 abc를 모두 구해라
		
		
		for(int a=1; a<10; a++) {
			for(int b=1; b<10; b++) {
				for(int c=1; c<10; c++)
					if(a+b+c == 25) {
						System.out.println(a+"+"+b+"+"+c+"=25");
					}
			}
		}

	}

}

 

 

 

1-2+3-4+5-...+9-10 의 결과를 출력
앞에 수식도 나오게 해야함.

package ex0708;

public class Ex29_for {

	public static void main(String[] args) {
		// 1-2+3-4+5-...+9-10 의 결과를 출력
		// 앞에 수식도 나오게 해야함.
		
		
		int s=0;
		for(int i =1; i<=10; i++) {
			if(i%2==0) {
				System.out.print(i+(i==10 ? " = " : " + "));
				s-=i;
			}else {
				System.out.print(i+" - ");
				s+=i;
			}
		}
		System.out.println(s);

	}

}

결과만 나오게 하는거면 삼항 연산자로도 풀 수 있음.

 

 

 

정수를 입력 받아 소수인지 판별
소수는 1과 자기 자신의 수로만 나누어 떨어지는 수이고, 1은 소수가 아님
소수는 2를 제외하고 모두 홀수이다. (2,3,5,7,11,13,17,19...)

 

package ex0708;

import java.util.Scanner;

public class Ex30_for {

	public static void main(String[] args) {
		// 정수를 입력 받아 소수인지 판별
		// 소수는 1과 자기 자신의 수로만 나누어 떨어지는 수이고, 1은 소수가 아님
		// 소수는 2를 제외하고 모두 홀수이다. (2,3,5,7,11,13,17,19...)
		
		Scanner sc = new Scanner(System.in);
		
		int num;
		System.out.println("정수");
		num = sc.nextInt();
		
		int n=2;
		boolean b = true;
		
		while(n<num) {
			if(num%n==0) {
				b=false;
				break;
			}
			n++;
		}
		
		if(b&&num!=1) {
			System.out.println(num + "는 소수");
		}else {
			System.out.println(num + "는 소수아님");
		}
		
		
		sc.close();

	}

}

 

 

 

 

난수를 이용해서 가위바위보 하기


컴퓨터는 1~3 사이의 난수 발생 1=가위 / 2=바위 / 3=보
유저는 1~4사이의 수를 입력하고 그 외의 수는 다시 입력
1=가위 / 2=바위 / 3=보 / 4=게임종료
게임은 게임 종료 누를때까지 계속 진행

 

package ex0708;

import java.util.Scanner;

public class Ex31_forQ1 {

	public static void main(String[] args) {
		// 난수를 이용해서 가위바위보 하기
		// 컴퓨터는 1~3 사이의 난수 발생 1=가위 / 2=바위 / 3=보
		// 유저는 1~4사이의 수를 입력하고 그 외의 수는 다시 입력
		// 1=가위 / 2=바위 / 3=보 / 4=게임종료
		// 게임은 게임 종료 누를때까지 계속 진행

		
		Scanner sc = new Scanner(System.in);
		
		int com,user;
		
		while(true) {
			do {
				System.out.print("1.가위 2.바위 3.보 4.게임종료 => ");
				user = sc.nextInt();
				if(user<1 || user>4) {
					System.out.println("다시 입력");
				}
			} while(user<1 || user>4);
				
		
			if(user==4) {
				System.out.println("게임이 종료 되었습니다.");
				break;
			}
			
			com = (int)(Math.random()*3)+1;
			
			System.out.println("유저 : " + (user==1 ? "가위" : user==2 ? "바위" : "보"));
			System.out.println("컴 : " + (com==1 ? "가위" : com==2 ? "바위" : "보"));
		
			
			if(user-com == -2 || user-com == 1) {
				System.out.println("당신이 이겼습니다.");
			}
			else if(com==user) {
				System.out.println("비겼습니다.");
			}
			else {
				System.out.println("컴퓨터 승");
			}
				
		}
			
		sc.close();
	}

}

 

 

반응형