본문 바로가기
자바 프로그래밍/쉽게 배우는 자바 프로그래밍

3장 프로그래밍 문제

by 녤 2022. 9. 1.

 

do~while vs while

 

public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int i=10;
		
		do {
			i++;
		}while(i<5);
		
		System.out.println("do~while문 실행 후 :"+i);
		
		i=10;
		
		while(i<5) {
			i++;
		}
		System.out.println("while문 실행 후:"+i);
	}

}
 

do~while은 어쨌거나 실행문이 한번은 실행되기 때문에 출력시 11이 나오고

while은 조건문이 거짓이면 실행하지 않기 때문에 출력시에 10이 나옴

 

for문에서 초기식이나 증감식이 두개 이상이면 ,(쉼표)를 사용해서 사용

 

*Switch는 나중에 다시!! 꼭 보기

 

117p.~119p.

 

public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);

		int result=1;
		int n;
		
		System.out.print("팩토리얼을 구할 정수:");
		 n=in.nextInt();
		 
		 for(int i=1; i<=n; i++) {
			 result= result * i;
		 }
	
		 System.out.print(result);
		
	}
 
public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);

		int result=1;
		int n;
		
		System.out.print("팩토리얼을 구할 정수:");
		 n=in.nextInt();
		 
		while(n>0) {
			result=result*n;
			n--;}
		
		 System.out.print(result);
		
	}
 
public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int result;
		int n;
		
		Scanner in= new Scanner(System.in);
		
		System.out.print("팩토리얼 값을 구할 함수:");
		n=in.nextInt();
		
		result=factorial(n);
		
		System.out.print(result);
	}
	
	static int factorial (int x) {
		
		int r=1;
		
		while(x>0) {
			
			r=r*x;
			x--;
		}
		
		return r;
	}
 
public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		// System.out.println(factorial(5));
		System.out.println(factorial(1,5));
		System.out.println(factorial(3,5));
		System.out.println(factorial(10,5));	
		
	}
	
	static int factorial (int x, int y) {
		
		int r=1;
		
		for(int i=x; i<=y;i++) {
			r=r*i;
		}
		
		return r;
	}
 

 

 

 

프로그래밍 문제 (홀수번만)

 

01. 키보드로 입력한 정수가 19 이상이면 '성년', 아니면 '미성년'을 출력하는 프로그램을 if~else문을 사용해 작성하라.

 

public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Scanner in = new Scanner(System.in);
		
		System.out.print("나이를 입력하시오:");
		int age=in.nextInt();
		
		if (age>=19) 
			System.out.print("성년");
		
		else 
			System.out.print("미성년");
		
		in.close();

	}
 

03. 키보드로 입력된 양의 정수 중에서 짝수만 덧셈해서 출력하는 코드를 do~while문을 사용하여 작성하라. 단, 입력된 정수가 양수가 아니라면 입력을 종료한다.

 

public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int sum=0;
		int num;
		
		
		Scanner in= new Scanner(System.in);
		
		do {
			System.out.print("양의 정수를 입력하시오:");
			num=in.nextInt();
			
			if(num %2 ==0) {
				sum=sum+num;
			}
			
		} while(num >0);
		
		System.out.print("입력한 양의 정수 중에서 짝수의 합은:"+sum);
		
		in.close();
	}
 

04. 다음 실행 결과를 출력하는 프로그램을 for 문을 사용해 작성하라.

 

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int x,y;
		
		for(x=1;x<=5;x++) {
			for(y=0;y<x;y++) { //y가 x-1만큼 반복하니까...찍히는 거겟죠. 
				System.out.printf("*");
			}
			
			System.out.println();
		}
	}
 

05. 각 변의 길이 합이 20 이하이며 각 변의 길이가 정수인 직각 삼각형의 모든 변을 구하라.

	public static void main(String [] args) {
		
		int a,b,c;
		
		for(a=1;a<=19;a++) {
			for(b=1; b<=19; b++) {
				for(c=1;c<=19;c++)
					if((a*a+b*b==c*c) && (a+b+c<=20)) {
						System.out.printf("%d %d %d\n", a, b, c);
					}
			}
		}
	}
 

06. 철수와 영희가 가위(s),바위(r), 보(p) 게임을 한다. 다음 실행 결과와 같이 r,p,s 중 하나를 입력해 승자 또는 무승부를 출력하는 프로그램ㅇ을 작성하라.

https://blog.naver.com/wndgndi/222661858365

이미지 썸네일 삭제
[자바] == 연산자, equals( )

객체 비교와 equals( ) 메소드 프로그램을 작성하다보면 두 객체가 같은 것인지를 비교해야 할 경우가 더러...

blog.naver.com

import java.util.Scanner;

public class Chap3 {
	public static void main(String [] args) {
		Scanner in =new Scanner(System.in);
		
		String c,y;
		int result;
		
		System.out.print("철수:");
		c=in.next();
		
		System.out.print("영희:");
		y=in.next();
		
		if((c.equals("s")&& y.equals("p")) || (c.equals("r") && y.equals("s")) || (c.equals("p") && y.equals("r"))){
			System.out.print("철수 승");
		}
		
		else 
			System.out.print("영희 승");

}
} 
 

07. 6에서 프롬포트와 r,p,s를 입력하는 부분, 입력된 데이터에 따라 승자를 출력하는 부분을 각가 메서드로 작성하라. main() 메서드는 다음과 같다.

 

import java.util.Scanner;

public class Chap3 {
	public static void main(String [] args) {
		String c=input("철수");
		String y=input("영희");
		whosWin(c,y);	

}
	
	public static String input(String name) {
		
		String x;
		Scanner in =new Scanner(System.in);
		
		System.out.printf("%s:", name);
		x= in.next();
		
		return x;
	}
	
	public static void whosWin(String x, String y) {
		
		if((x.equals("s")&& y.equals("p")) || (x.equals("r") && y.equals("s")) || (x.equals("p") && y.equals("r")))
			System.out.print("철수 승");
		
		
		else 
			System.out.print("영희 승"); 
	}
		
		
	}
 

09. 다음은 foo() 메서드가 빠진 프로그램 일부와 실행 결과이다. foo()메서드를 완성하라.

 

public class Chap3 { //메서드 오버로딩 
	public static void main(String [] args) {
		
		foo("안녕",1);
		foo("안녕하세요" , 1, 2);
		foo("잘 있어")

}
	
	public static void foo (String x, int y) {
		
		System.out.printf("%s %d", x, y);
	}

	public static void foo (String x, int y, int z) {
		
		System.out.printf("%s %d %d", x, y,z);
	}
	

	public static void foo (String x) {
		
		System.out.printf("%s", x);
	}
}