밑변과 높이 정보를 저장할 수 있는 Triangle 클래스 정의
main 클래스에 생성과 동시에 초기화가 가능한 생성자 정의
밑변과 높이 정보를 변경시킬 수 있는 메소드를 정의하여 삼각형의 넓이를 계산해서 반환하는 메소드 객체를 구현

 

 

public class TriangleMain {
	public static void main(String[] args) {
		//생성과 동시에 초기화가 가능한 생성자도 정의
		Triangle ta0 = new Triangle(3,5);
		Triangle ta1 = new Triangle(4,2);
		Triangle ta2 = new Triangle(6,10);//
		
		ta0.res();
		ta1.res();
		ta2.res();
		
	}
}
public class Triangle {
	private int base;
	private int height;


	public Triangle(int base, int height) {
		this.base = base;
		this.height = height;//정보를 변경시킬수있는 메소드정의
	}

	void res() {
		System.out.printf("삼각형의 넓이는 %d이다.\n", base * height / 2);//넓이를 계산해서 반환
	}

}

ex1

public class Star {
	public static void printStar(int num) {
		// 세로 행
		for(int i=0;i<num;i++) {
			// 가로 열
			for(int j=0;j<=i;j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(true) {
			System.out.println("별찍기 숫자 입력");
			int num = sc.nextInt();
			if(num == -1)
				break;
			printStar(num);
			System.out.println();
		}
		
		sc.close();
	}
}
별찍기 숫자 입력
5
*
**
***
****
*****

 

 

 

ex2

public class StarDiamond {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		while (true) {
			System.out.println("다이아몬드 숫자 입력");
			int num = sc.nextInt();
			if (num == -1) {
				System.out.println("종료합니다~");
				break;
			}
			int middlePt = num / 2; // 중앙위치
			// 세로행만큼 아래로 개행
			for (int i = 0; i < num; i++) {
				// 가로열의 이동
				for (int j = 0; j < num; j++) {
					if (i <= middlePt) {
						if (j >= middlePt - i && j <= middlePt + i)
							System.out.print("*");
						else
							System.out.print(" ");
					} else { // 세로행이 중앙보다 초과
						if (j >= middlePt - (num - i - 1) && j <= middlePt + (num - i - 1))
							System.out.print("*");
						else
							System.out.print(" ");
					}
				}
				System.out.println();
			}
		}
		sc.close();
	}
}
다이아몬드 숫자 입력
7
   *   
  ***  
 ***** 
*******
 ***** 
  ***  
   *   
for (int b = 1; b <= 9; b++) {//a와 b의 순서를 바꾸는것이 포인트
			for (int a = 2; a <= 9; a++) {
				System.out.print(a + "x" + b + "=" + a * b+"\t");
                //Tab(\t) 만큼 벌리기// 여기에서 nx1=합 첫째줄이 출력 ↓내려가서 줄바꿈
			}System.out.println();//줄바꾸기->다시 첫for문으로 올라가서 둘째쭐 nx2=합 출력 반복
2x1=2	3x1=3	4x1=4	5x1=5	6x1=6	7x1=7	8x1=8	9x1=9	
2x2=4	3x2=6	4x2=8	5x2=10	6x2=12	7x2=14	8x2=16	9x2=18	
2x3=6	3x3=9	4x3=12	5x3=15	6x3=18	7x3=21	8x3=24	9x3=27	
2x4=8	3x4=12	4x4=16	5x4=20	6x4=24	7x4=28	8x4=32	9x4=36	
2x5=10	3x5=15	4x5=20	5x5=25	6x5=30	7x5=35	8x5=40	9x5=45	
2x6=12	3x6=18	4x6=24	5x6=30	6x6=36	7x6=42	8x6=48	9x6=54	
2x7=14	3x7=21	4x7=28	5x7=35	6x7=42	7x7=49	8x7=56	9x7=63	
2x8=16	3x8=24	4x8=32	5x8=40	6x8=48	7x8=56	8x8=64	9x8=72	
2x9=18	3x9=27	4x9=36	5x9=45	6x9=54	7x9=63	8x9=72	9x9=81	

'java > java exercise' 카테고리의 다른 글

삼각형의 넓이  (0) 2020.02.10
별찍기  (0) 2020.01.28
Identify x and y are in the rectangle zone  (0) 2020.01.25
It will add everything until you're satisfied.  (0) 2020.01.25
Generate multiplication table of a given number  (0) 2020.01.25
Scanner sc = new Scanner(System.in);
		// 정수 x와 y를 입력받고
		System.out.println("정수 x를 입력해주세요.");
		int x = sc.nextInt();
		System.out.println("정수 y를 입력해주세요.");
		int y = sc.nextInt();

		/*
		 * (50, 50)과 (100, 100)의 두 점으로 이루어진 사각형 점(x, y)가 이 직사각형 안에 있는지 밖에 있는지 판별
		 */
		if ((50 <= x && x <= 100) && (50 <= y && y <= 100))
			System.out.println(x + ", " + y + "는 직사각형 안에 있습니다.");
		else
			System.out.println(x + ", " + y + "는 직사각형 밖에 있습니다.");

		sc.close();

사용자로부터 입력 받은 정수를 계속 더합니다.
사용자가 0을 입력하면 합을 출력합니다.
프로그램을 종료합니다.

Scanner sc = new Scanner(System.in);
		int result = 0;

		while (true) { //반복문//
			System.out.println("정수를 입력해주세요(0을 입력시 프로그램 종료): ");
			int num = sc.nextInt();
			result += num; //변수를 하나더 선언해준뒤 그값(result)에 입력받은 num을 계속 합산//

			if (num == 0)//num이 0일경우 break로 while문을 빠져나가 ↓아래에 리절트값을 출력//
				break;
		}
		
		System.out.println("총 합산은 " + result + " 입니다.");
		sc.close();

입력받은 수의 구구단 출력하기

Scanner sc = new Scanner (System.in);
		System.out.println("숫자입력");
		int num = sc.nextInt();
		
		for(int i = 1 ; i <= 9; i++) { //for(정의;조건식;증감식)
			System.out.printf("%dx%d=%d\t", num, i, num*i);
		}//\t=띄어쓰기
		sc.close();
	Scanner sc = new Scanner(System.in);
		System.out.println("type number");
		int num = sc.nextInt();
		int i = 0; //정의
		
		do {
			System.out.println("Thank you");
			i++;//증감식
		} while (i<num);//조건식
		
		sc.close();

no thank you, next

'java > java exercise' 카테고리의 다른 글

It will add everything until you're satisfied.  (0) 2020.01.25
Generate multiplication table of a given number  (0) 2020.01.25
Calculate and display Student Grades  (0) 2020.01.25
Print multiplication table  (0) 2020.01.25
Dice Game  (0) 2020.01.25

ex1 Using "switch-case"

System.out.println("학점을 입력해주세요");
			int score = sc.nextInt();
			int yuk = score/10;
			
			switch(yuk) {
			case 10 :
			case 9 :
				System.out.println(score + "점은 A 입니다.");
				break;
			case 8 :
				System.out.println(score + "점은 B 입니다.");
				break;
			case 7 :
				System.out.println(score + "점은 C 입니다.");
				break;
			case 6 :
				System.out.println(score + "점은 D 입니다.");
				break;
			default :
				System.out.println("press F to pay respect");
				break;
			}

 

ex2 using "if-else if"

	System.out.println("학점을 입력해주세요");
			int score = sc.nextInt();
			
    if (score>=90)
				System.out.println(score + "점은 A 입니다.");
			else if (score>=80)
				System.out.println(score + "점은 B 입니다.");
			else if (score>=70)
				System.out.println(score + "점은 C 입니다.");
			else if (score>=60)
				System.out.println(score + "점은 D 입니다.");
			else 
				System.out.println(score + "점은 F 입니다.");

'java > java exercise' 카테고리의 다른 글

Generate multiplication table of a given number  (0) 2020.01.25
It will thank you until you're satisfied.  (0) 2020.01.25
Print multiplication table  (0) 2020.01.25
Dice Game  (0) 2020.01.25
Changing Letter case  (0) 2020.01.25