๊ด€๋ฆฌ ๋ฉ”๋‰ด

gi_dor

Java (4) ์ œ์–ด๋ฌธ - ๋ฐ˜๋ณต๋ฌธ + Scanner ๋ณธ๋ฌธ

Language/Java

Java (4) ์ œ์–ด๋ฌธ - ๋ฐ˜๋ณต๋ฌธ + Scanner

๊ธฐ๋Œ 2023. 10. 26. 17:57
728x90

๐Ÿ“š ๊ตญ๋น„ 14 ์ผ์ฐจ


๋ชฉ์ฐจ

for
์ค‘์ฒฉ for
Scanner
break 
continue
์ด๋ฆ„์žˆ๋Š” ๋ฐ˜๋ณต๋ฌธ

 


๐Ÿ”ถ ์ œ์–ด๋ฌธ

  • ๋ฐ˜๋ณต๋ฌธ : for ๋ฌธ , while๋ฌธ์„ ํ™œ์šฉํ•˜๋ฉด ํŠน์ • ์ˆ˜ํ–‰๋ฌธ์„ ์—ฌ๋Ÿฌ๋ฒˆ ( ์กฐ๊ฑด์‹์ด true๋กœ ํŒ์ •๋˜๋Š” ๋™์•ˆ ) ์‹คํ–‰ ์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

  • while : ๋ฐ˜๋ณต์ฒ˜๋ฆฌ ์ง€์†์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ๊ธฐ๋Šฅ์ด ์žˆ๋Š” ๊ฒƒ๋“ค๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ  , ๋ฌดํ•œ ๋ฐ˜๋ณต ์ž‘์—… ๊ตฌํ˜„
  • do - while :  ๋ฐ˜๋“œ์‹œ ํ•œ๋ฒˆ์€ ์ˆ˜ํ–‰๋ฌธ์ด ์‹คํ–‰๋˜์–ด์•ผ ํ•˜๋Š” ๋ฐ˜๋ณต์ž‘์—…

 

๐Ÿ”ธ ๋ฐ˜๋ณต๋ฌธ

๐Ÿ”น  for ๋ฌธ

  • for  :  ๋ฐ˜๋ณตํšŸ์ˆ˜๋ฅผ ๊ฒฐ์ •ํ•ด์„œ ๋ฐ˜๋ณต์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ
  • ์ดˆ๊ธฐ์‹์—์„œ ์ƒ์„ฑํ•œ ๋ณ€์ˆ˜๋Š” ์กฐ๊ฑด์‹ , ์ฆ๊ฐ์‹ , ๋ธ”๋Ÿญ๋‚ด์˜ ์ˆ˜ํ–‰๋ฌธ์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
  • for๋ฌธ์˜ ์ดˆ๊ธฐ์‹ , ์กฐ๊ฑด์‹ , ์ฆ๊ฐ์‹์€ ์ƒ๋žต์ด ๊ฐ€๋Šฅํ•˜๋‹ค
/*
* for ๋ฌธ
* 	์ง€์ •๋œ ๋ฐ˜๋ณตํšŸ์ˆ˜ ๋งŒํผ ๋ธ”๋Ÿญ๋‚ด ์ˆ˜ํ–‰๋ฌธ์„ ๋ฐ˜๋ณต์ˆ˜ํ–‰ํ•œ๋‹ค
* 
* for (์ดˆ๊ธฐ์‹; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹;) {
*  	์ˆ˜ํ–‰๋ฌธ;
* 	์ˆ˜ํ–‰๋ฌธ;
*  }
* 
* ๋ฐ˜๋ณต ํšŸ์ˆ˜๋Š” ์ดˆ๊ธฐ์‹ , ์กฐ๊ฑด์‹ , ์ฆ๊ฐ์‹ ์„ ์ด์šฉํ•ด ์ง€์ •
* 
* 					์‹คํ–‰์ˆœ์„œ
* 
*					<----- ์ฆ๊ฐ์‹ <---
*					| 		|
* 			์ดˆ๊ธฐ์‹ --> ์กฐ๊ฑด์‹ --> ์ˆ˜ํ–‰๋ฌธ ----->
* 
*/

 


๐Ÿ”น ์ค‘์ฒฉ for ๋ฌธ

  • ์ˆ˜ํ–‰๋ฌธ 1์€ ๋ฐ”๊นฅ์ชฝ for๋ฌธ์— ๋ฐ˜๋ณตํšŸ์ˆ˜ ๋งŒํผ ์‹คํ–‰
  • ์ˆ˜ํ–‰๋ฌธ 1 ์—์„œ๋Š” ์•ˆ์ชฝ for๋ฌธ์˜ ์ดˆ๊ธฐ์‹์—์„œ ์ƒ์„ฑํ•œ ๋ณ€์ˆ˜์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์—†๋‹ค

  • ์ˆ˜ํ–‰๋ฌธ 2๋Š” ๋ฐ”๊นฅ์ชฝ for๋ฌธ์ด ํ•œ๋ฒˆ ์‹คํ–‰ ๋  ๋•Œ๋งˆ๋‹ค ์•ˆ์ชฝ for ๋ฌธ์— ๋ฐ˜๋ณตํšŸ์ˆ˜ ๋งŒํผ ์‹คํ–‰
  • ์ˆ˜ํ–‰๋ฌธ 2์—์„œ๋Š” ๋ฐ”๊นฅ for ๋ฌธ์˜ ์ดˆ๊ธฐ์‹์—์„œ ์ƒ์„ฑํ•œ ๋ณ€์ˆ˜
  • ์•ˆ์ชฝ for ๋ฌธ์˜ ์ดˆ๊ธฐ์‹์—์„œ ์ƒ์„ฑํ•œ ๋ณ€์ˆ˜์— ์ ‘๊ทผ ํ•  ์ˆ˜ ์—†๋‹ค.
* ์ค‘์ฒฉ๋œ for๋ฌธ
* 
* for( ์ดˆ๊ธฐ์‹; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹){
* 		์ˆ˜ํ–‰๋ฌธ1		
* 	for( ์ดˆ๊ธฐ์‹2; ์กฐ๊ฑด์‹2; ์ฆ๊ฐ์‹){
* 		์ˆ˜ํ–‰๋ฌธ2
* 
* 		}
* 	}

 


๐Ÿ”น Scanner

  • ๊ฐ’์„ ์ฝ์–ด์˜ค๋Š” ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค
  • java.base ๋ชจ๋“ˆ์˜ java.util ํŒจํ‚ค์ง€์— ์œ„์น˜ํ•˜๋Š” ํด๋ž˜์Šค
next() : ์ž…๋ ฅํ•œ ๊ฐ’์„ ๋ฌธ์ž์—ด๋กœ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค
nextInt() : ์ž…๋ ฅํ•œ ๊ฐ’์„ ์ •์ˆ˜๋กœ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค
nextDouble() : ์ž…๋ ฅํ•œ ๊ฐ’์„ ์‹ค์ˆ˜๋กœ ์ œ๊ณตํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค.
Scanner scanner = new Scanner(System.in);

// new Scanner(System.in);  ํ‘œ์ค€ ์ž…๋ ฅ์žฅ์น˜์ธ ํ‚ค๋ณด๋“œ์—์„œ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ ์ฝ์–ด์˜จ๋‹ค

๐Ÿ”น break 

  • break ์™€ continue๋Š” ๋ฐ˜๋ณต๋ฌธ ์•ˆ์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํ‚ค์›Œ๋“œ๋‹ค .
    โœ” break๋Š” switch์—์„œ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
  • break๋Š” ์ด ํ‚ค์›Œ๋“œ๊ฐ€ ํฌํ•จ๋œ ๊ฐ€์žฅ ์•ˆ์ชฝ ๋ฐ˜๋ณต๋ฌธ์„ ํƒˆ์ถœํ•œ๋‹ค

for(์ดˆ๊ธฐ์‹;์กฐ๊ฑด์‹;์ฆ๊ฐ์‹) {
	์ˆ˜ํ–‰๋ฌธ;
        ์ˆ˜ํ–‰๋ฌธ;
        // breack๋Š” ์ฃผ๋กœ if๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜์–ด ํŠน์ • ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์„ ๋ฒ—์–ด๋‚œ๋‹ค
      if (ํƒˆ์ถœ ์กฐ๊ฑด) {
        	์ˆ˜ํ–‰๋ฌธ;
            break;   ---
            }		|
			|
	}	<-------|

 


๐Ÿ”น continue

  • continue๋Š” ๋ฐ˜๋ณต๋ฌธ ์ง„ํ–‰ ์ค‘์— continue๋ฅผ ๋งŒ๋‚˜๋ฉด ๋ฐ˜๋ณต๋ฌธ์˜ ๋ (for๋Š” ์ฆ๊ฐ์‹ , while์€ ์กฐ๊ฑด์‹)์œผ๋กœ ์ด๋™ํ•ด์„œ 
    ๋‹ค์Œ ๋ฒˆ ๋ฐ˜๋ณต์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
// continue๋Š” ๋ฐ˜๋ณต๋ฌธ ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ
// break์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ if๋ฌธ๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ๋˜๋ฉฐ
// ๋‹ค์Œ๋ฒˆ ๋ฐ˜๋ณต์„ ํ•˜๊ธฐ ์œ„ํ•ด ์ฆ๊ฐ์‹์œผ๋กœ ์ด๋™ํ•œ๋‹ค

for(์ดˆ๊ธฐ์‹; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹){ <----
	์ˆ˜ํ–‰๋ฌธ;			|
   if (์กฐ๊ฑด์‹2) {		|
   ์ˆ˜ํ–‰๋ฌธ;			|
   continue;---------------------
	   }
        ์ˆ˜ํ–‰๋ฌธ;
        ์ˆ˜ํ–‰๋ฌธ;
}

 


๐Ÿ”น ์ด๋ฆ„ ์žˆ๋Š” ๋ฐ˜๋ณต๋ฌธ

  • ์—ฌ๋Ÿฌ ๋ฐ˜๋ณต๋ฌธ์ด ์ค‘์ฒฉ๋˜์–ด ์žˆ์„ ๋•Œ ๋ฐ˜๋ณต๋ฌธ ์•ž์—  ์ด๋ฆ„์„  ๋ถ™์ด๊ณ 
  • break ๋ฌธ๊ณผ continue๋ฌธ์—์„œ ์ด๋ฆ„์„ ์ •ํ•ด ์คŒ์œผ๋กœ์จ
  • ํ•˜๋‚˜ ์ด์ƒ์˜ ๋ฐ˜๋ณต๋ฌธ์„ ๋ช‡๋ฒˆ ๋ณด๋‚ด ๋ฐ˜๋ณต์„ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค.
Loop: for (์ดˆ๊ธฐํ™”์‹; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹ ) {
		for( ์ดˆ๊ธฐ์‹; ์กฐ๊ฑด์‹; ์ฆ๊ฐ์‹) {
        ์ˆ˜ํ–‰๋ฌธ;
        
        if( ํƒˆ์ถœ์ถœ์กฐ๊ฑด)1 {
        	 break Loop1 
        }			         
            if( ํƒˆ์ถœ์กฐ๊ฑด2) {
            break;----------------
            } // ์•ˆ์ชฝ for ๋ฌธ์˜ ๋ 
        ์ˆ˜ํ–‰๋ฌธ;             
        } // ์ด๋ฆ„์ด Loop1์ธ for๋ฌธ์˜ ๋

 


 


์‹ค์Šต

โ—ป ์‹ค์Šต - for

// ์ง€์ •๋œ ์ˆ˜ํ–‰๋ฌธ์„ 5๋ฒˆ ์‹คํ–‰ํ•˜๋Š” for๋ฌธ

for( int i = 1; i <= 5; i++) {
   System.out.println("i์˜ ๊ฐ’ " +i);
  }
// ์ƒ๋žต ver
		
int a = 1;
	for(; a<= 5; a++) {
   	 System.out.println("a์˜ ๊ฐ’ : "+a);
	}
    
int b = 1;
    for(; b <= 5;) {
        System.out.println("b์˜ ๊ฐ’ : "+b);
        b++;
    }
// for๋ฌธ์œผ๋กœ ๋ฌดํ•œ๋ฐ˜๋ณต
for (;;) {
	System.out.println("๋ฌดํ•œ๋ฐ˜๋ณต a : " + a);
	System.out.println("๋ฌดํ•œ๋ฐ˜๋ณต b : " + b);
        }

 


		// 1 ~ 100 ๊นŒ์ง€ ์ •์ˆ˜์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๊ธฐ
		
		// ์ •์ˆ˜์˜ ํ•ฉ์„ ๋‹ด์„ ๋ณ€์ˆ˜ sum ์ดˆ๊ธฐํ™” 
		int sum = 0;
		
		
			for (int i = 1; i <= 100; i++) {
				sum += i;
					}
			System.out.println("1 ~ 100 ๊นŒ์ง€์˜ ์ •์ˆ˜์˜ ํ•ฉ : " +sum);  
		
		// 1 ~ 100 ๊นŒ์ง€ ์ •์ˆ˜์ค‘์—์„œ ์ง์ˆ˜์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๊ธฐ
			
			int doubleSum = 0;
			for ( int i = 2; i <= 100; i += 2) {
				doubleSum += i;
			}
			
			System.out.println("1 ~ 100 ๊นŒ์ง€์˜ ์ •์ˆ˜์ค‘ ์ง์ˆ˜์˜ ํ•ฉ : " + doubleSum);
		
		
		// 1 ~ 100 ๊นŒ์ง€์˜ ์ •์ˆ˜ ์ค‘์—์„œ ํ™€์ˆ˜์˜ ํ•ฉ์„ ๊ณ„์‚ฐ
			
			int oddSum = 0;
			for( int i = 1; i <= 100; i++ ) {
				if(i%2 == 1) {
					oddSum += i;
				}
			}
			System.out.println("1 ~ 100 ๊นŒ์ง€์˜ ์ •์ˆ˜  ํ™€์ˆ˜์˜ ํ•ฉ : "+ oddSum);

 

// ๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅํ•˜๊ธฐ
		
		/*
		 *  3 * 1 = 3
		 *  3 * 2 = 6
		 *  3 * 3 = 9
		 *  3 * 4 = 12
		 *  .....
		 *  3 * 9 = 27 
		 * 
		 */
		
		int dan = 3;
		
		for ( int i = 1; i <= 9; i++) {
				System.out.println( dan +" * "+ i + " = " + i * dan );
				}

 

โ—ป ์‹ค์Šต - Star

/* *****
		 * *****
		 * *****
		 * *****
		 * *****
		 */
		
		System.out.println("1๋ฒˆ");

		for(int line = 1; line <= 5; line++) {	// ํ–‰์„ ์ถœ๋ ฅ  5๋ฒˆ
			for(int count = 1; count <= 5; count++) {	// ๋ณ„์„ ์ถœ๋ ฅ ์ค„๋งˆ๋‹ค 5๋ฒˆ 
				System.out.print("*");
			}
			System.out.println();
		}
		
		System.out.println("------------------------");
	
		
		
		/*
		 * 
		 * * 		1๋ฒˆ์ค„  1๊ฐœ
		 * **		2๋ฒˆ์ค„  2๊ฐœ
		 * ***		3๋ฒˆ์ค„  3๊ฐœ
		 * ****		4๋ฒˆ์ค„  4๊ฐœ
		 * *****	5๋ฒˆ์ค„  5๊ฐœ
		 * 
		 * ---------------
		 */
		
		System.out.println("2๋ฒˆ");
		
		for(int line = 1; line <= 5; line++) {
			for(int count =1; count <= line; count++) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		
		System.out.println("-----------------------");
	
		 /* *****   1๋ฒˆ์ค„ 5๊ฐœ = 6  -> 6 - i ๋ฒˆ์งธ์ค„ 5๊ฐœ 
		  * ****	2๋ฒˆ์ค„ 4๊ฐœ = 6  -> 6 - i ๋ฒˆ์งธ์ค„ 4๊ฐœ
		  * ***		3๋ฒˆ์ค„ 3๊ฐœ = 6
		  * **		4๋ฒˆ์ค„ 2๊ฐœ = 6
		  * *		5๋ฒˆ์ค„ 1๊ฐœ = 6
		  * 	
		  * 
		  * ----------------
		  */
		
		System.out.println("3๋ฒˆ");
		for (int i = 1; i <= 5; i++) {
			for(int j = 1; j <= 6 - i; j++) {   //for(int j = 6 - i; j >= 1 ; j--) {
				System.out.print("*");
			}
			System.out.println();
		}
		
		
		
		
		System.out.println("--------------");

 

		
		

//	 	  ํ–‰	 ๊ณต๋ฐฑ   ๋ณ„
/*     *  	  1ํ–‰ 	 4์นธ 	 ๋ณ„1     ํ–‰ == ๋ณ„
*     **	2ํ–‰ 	 3์นธ  	๋ณ„2     5 - ํ–‰ = ๊ณต๋ฐฑ
*    ***	3ํ–‰ 	 2์นธ 	๋ณ„3
*   ****	4ํ–‰ 	 1์นธ  	๋ณ„4
*  *****	5ํ–‰ 	 0์นธ 	๋ณ„5
*  
*  ---------------
*/  
		 
		 System.out.println("4๋ฒˆ");
		for(int line = 1; line <= 5; line++) {
			// ๊ณต๋ฐฑ
			for(int space = 1; space <= 5 - line; space++) {
				System.out.print(" ");
			}
			// ๋ณ„์ฐ๊ธฐ
			for(int star = 1; star <= line; star++ ) {
				System.out.print("*");
			}
			System.out.println();
		}
	// ๊ตฌ๊ตฌ๋‹จ ์ถœ๋ ฅ
		
/*
2 * 1 = 2	3 * 1 = 3	4 * 1 = 4	5 * 1 = 5	6 * 1 = 6	7 * 1 = 7	8 * 1 = 8	9 * 1 = 9	
2 * 2 = 4	3 * 2 = 6	4 * 2 = 8	5 * 2 = 10	6 * 2 = 12	7 * 2 = 14	8 * 2 = 16	9 * 2 = 18	
2 * 3 = 6	3 * 3 = 9	4 * 3 = 12	5 * 3 = 15	6 * 3 = 18	7 * 3 = 21	8 * 3 = 24	9 * 3 = 27	
2 * 4 = 8	3 * 4 = 12	4 * 4 = 16	5 * 4 = 20	6 * 4 = 24	7 * 4 = 28	8 * 4 = 32	9 * 4 = 36	
2 * 5 = 10	3 * 5 = 15	4 * 5 = 20	5 * 5 = 25	6 * 5 = 30	7 * 5 = 35	8 * 5 = 40	9 * 5 = 45	
2 * 6 = 12	3 * 6 = 18	4 * 6 = 24	5 * 6 = 30	6 * 6 = 36	7 * 6 = 42	8 * 6 = 48	9 * 6 = 54	
2 * 7 = 14	3 * 7 = 21	4 * 7 = 28	5 * 7 = 35	6 * 7 = 42	7 * 7 = 49	8 * 7 = 56	9 * 7 = 63	
2 * 8 = 16	3 * 8 = 24	4 * 8 = 32	5 * 8 = 40	6 * 8 = 48	7 * 8 = 56	8 * 8 = 64	9 * 8 = 72	
2 * 9 = 18	3 * 9 = 27	4 * 9 = 36	5 * 9 = 45	6 * 9 = 54	7 * 9 = 63	8 * 9 = 72	9 * 9 = 81
* 
*/
		
		// dan = 2; dan<=9; dan++ 
			for (int row = 1; row <= 9; row++) {
				for(int dan = 2; dan <= 9; dan++) {
					System.out.print(dan +" * "+ row +" = "+ dan*row + "\t");
				}
				System.out.println();
			}

 


 

โ—ป ์‹ค์Šต - ์ž…๋ ฅ๋ฐ›์•„์„œ ๋” ํฐ ์ˆ˜ ๊ตฌํ•˜๊ธฐ

		/*
		 * ๋‘ ์ˆ˜๋ฅผ ์ž…๋ ฅ๋ฐ›์•„์„œ ๋” ํฐ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ
		 * 
		 */
		
		
		
		// new Scanner (System.in);
		// ํ‘œ์ค€ ์ž…๋ ฅ์žฅ์น˜์ธ ํ‚ค๋ณด๋“œ์—์„œ ์ž…๋ ฅํ•œ ๋‚ด์šฉ์„ ์ฝ์–ด์˜จ๋‹ค
		
		Scanner scanner = new Scanner(System.in);
		// Resource leak: 'scanner' is never closed /// ์ž์›์˜ ๋ˆ„์ˆ˜๊ฐ€ ? ๋ฐœ์ƒ

		
		// ์ž…๋ ฅ๊ฐ’์„ ์ฝ์–ด์„œ ์ •์ˆ˜๊ฐ’์œผ๋กœ ๋ณ€ํ™˜
		System.out.print("์ฒซ๋ฒˆ์งธ ์ˆซ์ž ์ž…๋ ฅ: " );
		int num1 = scanner.nextInt();

		System.out.print("๋‘๋ฒˆ์งธ ์ˆซ์ž ์ž…๋ ฅ: " );              
		int num2 = scanner.nextInt();
		
		System.out.println("์ฒซ๋ฒˆ์งธ ์ •์ˆ˜ : " + num1);
		System.out.println("๋‘๋ฒˆ์งธ ์ •์ˆ˜ : " + num2);
		
		if ( num1 > num2 ) {
			System.out.println("ํฐ ์ˆ˜ : " +num1);
		} else if (num1 < num2) {
			System.out.println("ํฐ ์ˆ˜ : "+num2);
		} else if( num1 == num2) {
			System.out.println("๋‘ ์ˆซ์ž๊ฐ€ ๊ฐ™์Šต๋‹ˆ๋‹ค");
		}
		
		
		
	
		
		
		scanner.close();

 


โ—ป ์‹ค์Šต -  ์‹œ๊ฐ„ ๊ตฌํ•˜๊ธฐ ( ์–ด๋ ค์› ์Œ )

		/* 
		 * ์™„๋ฃŒ์‹œ๊ฐ„ ์ถœ๋ ฅํ•˜๊ธฐ
		 * 
		 * ์ž…๋ ฅ์œผ๋กœ ํ˜„์žฌ ์‹œ๊ฐ„์˜ ์ •๋ณด์™€ ๋ถ„ ์ •๋ณด๋ฅผ ์ž…๋ ฅ๋ฐ›๋Š๋‹ค
		 * ์ž…๋ ฅ์œผ๋กœ ์†Œ์š” ์‹œ๊ฐ„์„ ๋ถ„๋‹จ์œ„๋กœ ์ž…๋ ฅ๋ฐ›๋Š”๋‹ค
		 * 
		 * ์™„๋ฃŒ ์‹œ๊ฐ„์„ ์‹œ , ๋ถ„ ๋‹จ์œ„๋กœ ์ถœ๋ ฅํ•œ๋‹ค
		 * 
		 * ์˜ˆ์‹œ. 
		 * ์‹œ์ž‘์‹œ๊ฐ„์˜ hour : 15์‹œ 
		 * ์‹œ์ž‘ ์‹œ๊ฐ„์˜ minute : 20
		 * ์†Œ์š”์‹œ๊ฐ„ (๋ถ„) : 50
		 * 
		 * ์˜ˆ์ƒ ์™„๋ฃŒ ์‹œ๊ฐ„ ?
		 * 
		 */
		
		// 15์‹œ 20๋ถ„์ธ๋ฐ 
		// ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„์ด 50๋ถ„์ด๋‹ˆ
		// ์˜ˆ์ƒ์™„๋ฃŒ ์‹œ๊ฐ„์€ 16์‹œ 10๋ถ„ ์ด๋ ‡๊ฒŒ ๋‚˜์™€์•ผํ•œ๋‹ค
		
		Scanner scanner = new Scanner(System.in);
		
		// ==========   ์ž…๋ ฅ =====================
		// ์‹œ์ž‘์‹œ๊ฐ„ ์ž…๋ ฅ
		System.out.print("์‹œ๊ฐ„ ์ž…๋ ฅ : ");
		int startHours = scanner.nextInt();
		
		// ์‹œ์ž‘ ์‹œ๊ฐ„์˜ ๋ถ„ ์ž…๋ ฅ
		System.out.print("๋ถ„ ์ž…๋ ฅ : ");
		int startMinutes = scanner.nextInt();
		
		
		// ์†Œ์š”๋˜๋Š” ์‹œ๊ฐ„
		System.out.print("์†Œ์š” ์‹œ๊ฐ„ ์ž…๋ ฅ (๋ถ„): ");
		int runningTime = scanner.nextInt();
		
		
		
		int endHours = 0;
		int endMinutes = 0;
		
		
		


		// startMinutes + runningTime์ด 60์˜ ๋ณด๋‹ค ํด ๋–„
		// ๋‘˜์ด ๋”ํ•œ ๊ฐ’์€ ์ตœ์ข… ์ง„ํ–‰๋œ minutes์ด๋จ
		// increMinutes / 60 ํ•˜๊ณ  ๋‚˜์˜จ ๊ฐ’์œผ๋กœ startHours + increMinutes
		// 
		//
		
		// ========= ์˜ˆ์ƒ ์™„๋ฃŒ ๊ณ„์‚ฐ========================
		if(startMinutes + runningTime >= 60) {
			// ์ด ๋ถ„ ๊ตฌํ•˜๊ธฐ
			int totalMinutes = startMinutes + runningTime;
			// ์ด ๋ถ„ ๊ตฌํ•œ๋’ค์— 60์œผ๋กœ ๋‚˜๋ˆ„์–ด์„œ ์‹œ๊ฐ„ ์ฆ๊ฐ€ ์‹œํ‚ค๊ธฐ
			int increseHours = totalMinutes / 60; 
		
			// ์™„๋ฃŒ ์‹œ์ ์€ ์‹œ๊ฐ„์€ ์‹œ์ž‘์‹œ๊ฐ„ + ์ฆ๊ฐ€ํ•œ ์‹œ๊ฐ„
			endHours = startHours + increseHours;
			// ์™„๋ฃŒ ์‹œ์ ์˜ ๋ถ„์€ ์ด ๋ถ„ ๊ฐ’์—์„œ ์ฆ๊ฐ€๋œ ์‹œ๊ฐ„ ๋งŒํผ์˜ ๋ถ„๊ฐ’์„ ์ œ์™ธ
			endMinutes = totalMinutes % 60;
            // endMinutes = totalMinutes - (increseHours * 60);
			
		} else {
			endHours = startHours;
			endMinutes = startMinutes + runningTime;
			
		
		}
		
		
		System.out.println("์™„๋ฃŒ์‹œ๊ฐ„ : "+ endHours + "์‹œ " + endMinutes +"๋ถ„" );
		scanner.close();

 


		//  1  ~  100 ๊นŒ์ง€ ์ˆœ์„œ๋Œ€๋กœ ์ €์žฅํ•ด์„œ ํ•ฉ๊ณ„๋ฅผ ๊ตฌํ•  ๋–„ ํ•ฉ๊ณ„๊ฐ€ 1000์„ ๋ŒํŒŒํ•˜๋Š” ์ˆœ๊ฐ„์˜ ํ•ฉ๊ณ„์™€ ๊ทธ ์ˆœ๊ฐ„์˜ ์ˆซ์ž๊ฐ’์„ ์ถœ๋ ฅํ•˜๊ธฐ

		int sum = 0;
		int number = 0;
	
		for(int num = 1; num<= 100; num++ ) {
			sum += num;
			
			if (sum >= 1000) {
				number = num;
				break;
				}
			
		}
			System.out.println("ํ•ฉ๊ณ„ : "+sum);
			System.out.println("์ˆซ์ž : "+number);
728x90