목차

1) 랜덤한 숫자 출력 : (new Random()).nextInt(100);

2) for문

3) for문 예제 1 ~ 6


1. 랜덤한 숫자 출력

package PACK01;
import java.util.Random;

public class Hello01 {

	public static void main(String[] args) {
		/*랜덤 숫자 출력*/
		
		//100 미만의 숫자 중에서 랜덤으로 출력
		int a = (new Random()).nextInt(100);
		System.out.println(a);
		
		int rn = (new Random()).nextInt();
	}
}

2. for문 

형태

package PACK01;

public class Hello01 {

	public static void main(String[] args) {
		/*for문*/
		for(int i = 0; i<10; i++) {
			System.out.println("호랑이"+i);
		}
		/*출력 결과
		호랑이0
		호랑이1
		호랑이2
		호랑이3
		호랑이4
		호랑이5
		호랑이6
		호랑이7
		호랑이8
		호랑이9
		*/
	}
}

예제 1 : 1부터 10까지 더하기

package PACK01;

public class Hello01 {

	public static void main(String[] args) {
		/*for문*/
		
		/*for문 자동완성 단축키
		for + ctrl + space
		*/
		
		int s = 0;
		for (int i = 1; i < 10+1; i++) {
			s = s+i;
			System.out.println(s);
		}
		
		System.out.println("총 합: "+s);
		
		/*출력 결과
		1
		3
		6
		10
		15
		21
		28
		36
		45
		55
		총 합: 55
		*/
	}
}

 


예제2 : 100미만의 랜덤한 숫자 10개의 합

package PACK01;
import java.util.Random;

public class Hello01 {

	public static void main(String[] args) {
		/*랜덤 숫자 출력*/
		
		//100미만의 랜덤한 숫자 10개의 합  
		int sum = 0;
		for (int i = 0; i<10; i++) {
			int rn = (new Random()).nextInt(100);
			System.out.println(rn);
			sum = sum + rn;
		}
		System.out.println("10개의 합: "+sum);
		
		/*출력 결과
		13
		34
		37
		87
		93
		5
		87
		52
		7
		1
		10개의 합: 416 */
	}
}

예제 3 : 구구단 5단 출력

package PACK01;
import java.util.Random;

public class Hello01 {

	public static void main(String[] args) {
		//구구단 5단 출력
		for (int i = 1; i < 9+1; i++) {
			int n = 5;
			System.out.println(n+"*"+i+":"+(n*i));
		}
		
		/*출력결과
		5*1:5
		5*2:10
		5*3:15
		5*4:20
		5*5:25
		5*6:30
		5*7:35
		5*8:40
		5*9:45*/
	}
}

예제 4 : 1부터 100까지 합산하는 프로그램

package PACK01;
import java.util.Random;

public class Hello01 {

	public static void main(String[] args) {
		//1부터 100까지 합산하는 프로그램
		int sum = 0;
		for (int i=1; i<=100; i++) {
			sum = sum + i;
		}
		System.out.println("총합: "+sum);
		
		/*출력결과
		 총합: 5050
		*/
	}
}

예제 5 : 1부터 10까지 숫자 중 다섯개 랜덤하게 골라 합산

package PACK01;
import java.util.Random;

public class Hello01 {

	public static void main(String[] args) {
		//1부터 10까지 숫자 중 다섯개 랜덤하게 골라 합산
		int sum = 0;
		for (int i=0; i<5; i++) {
			int rn = (new Random()).nextInt(10);
			System.out.println("랜덤숫자: "+rn);
			sum = sum + rn;
		}
		System.out.println("총합: "+sum);
		
		/*출력결과
		랜덤숫자: 4
		랜덤숫자: 6
		랜덤숫자: 4
		랜덤숫자: 9
		랜덤숫자: 3
		총합: 26
		*/
	}
}

 

예제 6 : 0부터 100사이의 숫자 중 10개를 랜덤으로 추출해 각 자리수의 합을 구하시오.

package PACK01;
import java.util.Random;

public class Hello01 {

	public static void main(String[] args) {
		//0부터 100사이의 숫자 중 10개를 추출해 각 자리수의 합을 구하시오
		for (int i = 0; i <10; i++) {
			int rn = (new Random()).nextInt(100);
			
			int q = rn /10; //십의 자리
			int n = rn%10; //일의 자리

			int sum = q + n;
			System.out.println("자리수의 합 : "+sum);
		}
		
		/*출력결과
		자리수의 합 : 7
		자리수의 합 : 3
		자리수의 합 : 2
		자리수의 합 : 4
		자리수의 합 : 10
		자리수의 합 : 11
		자리수의 합 : 1
		자리수의 합 : 15
		자리수의 합 : 4
		자리수의 합 : 13
		*/
	}
}

+ Recent posts