728x90
320x100
배열요소의 최댓값 구하기
package chap02;
import java.util.Scanner;
public class MaxOfArray {
static int maxOf(int[] a){
int max = a[0];
for (int i = 0; i < a.length; i++){
if (a[i] > max)
max = a[i];
}
return max;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("키의 최댓값을 구합니다.");
System.out.print("사람 수 : ");
int num = scan.nextInt();
int[] height = new int[num];
for(int i = 0; i < num; i++){
System.out.print("height[" + i + "]:");
height[i] = scan.nextInt();
}
System.out.println("최댓값은 " + maxOf(height) + " 입니다.");
}
}
난수(키)를 생성하여 배열의 최댓값 구하기
package chap02;
import java.util.Random;
import java.util.Scanner;
public class MaxOfArrayRand {
static int maxOf(int[] a){
int max = a[0];
for (int i = 0; i < a.length; i++){
if (a[i] > max)
max = a[i];
}
return max;
}
public static void main(String[] args) {
Random ran = new Random(); //랜덤함수
Scanner scan = new Scanner(System.in);
System.out.println("키의 최댓값을 구합니다.");
System.out.print("사람 수 : ");
int num = scan.nextInt();
int[] height = new int[num];
for(int i = 0; i < num; i++){
height[i] = 100 + ran.nextInt(90); // 0~89까지.
System.out.println("height[" + i + "]: " + height[i]);
}
System.out.println("최댓값은 " + maxOf(height) + " 입니다.");
}
}
*** ran.nextInt(90) -> 0~89까지의 난수를 생성함.
난수(사람수, 키)를 생성하여 배열 최댓값 구하기
package chap02;
import java.util.Random;
import java.util.Scanner;
public class Practice01 {
static int maxOf(int[] a){
int max = a[0];
for (int i = 0; i < a.length; i++){
if (a[i] > max)
max = a[i];
}
return max;
}
public static void main(String[] args) {
Random ran = new Random(); //랜덤함수
Scanner scan = new Scanner(System.in);
System.out.println("키의 최댓값을 구합니다.");
int num = 1 + ran.nextInt(9);
System.out.println("사람 수 : " + num);
int[] height = new int[num];
for(int i = 0; i < num; i++){
height[i] = 100 + ran.nextInt(90); // 0~89까지.
System.out.println("height[" + i + "]: " + height[i]);
}
System.out.println("최댓값은 " + maxOf(height) + " 입니다.");
}
}
*** 선형합동법 : 보편적으로 사용하는 의사 난수 생성기.
*** 의사 난수 : 컴퓨터 과학에서는 보통 특정 입력값이나 컴퓨터 환경에 따라 무작위로 선택한 것 처럼 보이는 난수를 생성하는데, 그 입력값이나 컴퓨터 환경이 같다면 그 결괏값은 항상 같음. 결국 컴퓨터에서 생성된 모든 난수는 미리 컴퓨터가 계산해둔 의사 난수임.
728x90
320x100
'💻 하나씩 차곡차곡 > 자료구조 & 알고리즘(JAVA)' 카테고리의 다른 글
배열 요소의 합계 구하기 (chap02/Practice03) (0) | 2022.10.18 |
---|---|
배열요소를 역순으로 정리하기 (chap02/ReverseArray) (0) | 2022.10.17 |
for문을 이용하여 각종 별찍기 (chap01/TriangleLB) (2) | 2022.10.14 |
이중루프를 사용하여 구구단 곱셈표 출력하기 (chap01/Multi99Table) (1) | 2022.10.13 |
2자리 양수만 입력받기 (chap01/Twodigits) (0) | 2022.10.12 |