자바에서 최대값 구하는 3가지 예제입니다. 참고하세요
import java.io.*;
import java.util.*;
class Max1 {
public static void main(String[] args) throws IOException {
int max = -2147483648;
int[] array = new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("콤마(,)로 구분하여 숫자들을 입력하세요...");
String digits = br.readLine();
StringTokenizer st = new StringTokenizer(digits, ",");
for(int i=0; st.hasMoreElements(); i++) {
array[i] = Integer.parseInt(st.nextToken());
}
import java.util.*;
class Max1 {
public static void main(String[] args) throws IOException {
int max = -2147483648;
int[] array = new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("콤마(,)로 구분하여 숫자들을 입력하세요...");
String digits = br.readLine();
StringTokenizer st = new StringTokenizer(digits, ",");
for(int i=0; st.hasMoreElements(); i++) {
array[i] = Integer.parseInt(st.nextToken());
}
for(int i=0; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
System.out.println("최대값은 : " + max);
}
}
if (array[i] > max) {
max = array[i];
}
}
System.out.println("최대값은 : " + max);
}
}
import java.io.*;
import java.util.*;
class Max2 {
public static void main(String[] args) throws IOException {
int[] array = new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("콤마(,)로 구분하여 숫자들을 입력하세요...");
String digits = br.readLine();
StringTokenizer st = new StringTokenizer(digits, ",");
for(int i=0; st.hasMoreElements(); i++) {
array[i] = Integer.parseInt(st.nextToken());
}
int tmp=0;
for(int i=0; i < array.length-1; i++) {
if (array[i] > array[i+1]) {
tmp = array[i];
array[i] = array[i+1];
array[i+1] = tmp;
}
}
System.out.println("최대값은 : " + array[array.length-1]);
}
}
import java.util.*;
class Max2 {
public static void main(String[] args) throws IOException {
int[] array = new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("콤마(,)로 구분하여 숫자들을 입력하세요...");
String digits = br.readLine();
StringTokenizer st = new StringTokenizer(digits, ",");
for(int i=0; st.hasMoreElements(); i++) {
array[i] = Integer.parseInt(st.nextToken());
}
int tmp=0;
for(int i=0; i < array.length-1; i++) {
if (array[i] > array[i+1]) {
tmp = array[i];
array[i] = array[i+1];
array[i+1] = tmp;
}
}
System.out.println("최대값은 : " + array[array.length-1]);
}
}
3.
import java.io.*;
import java.util.*;
class Max3 {
public static void main(String[] args) throws IOException {
int[] array = new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("콤마(,)로 구분하여 숫자들을 입력하세요...");
String digits = br.readLine();
StringTokenizer st = new StringTokenizer(digits, ",");
for(int i=0; st.hasMoreElements(); i++) {
array[i] = Integer.parseInt(st.nextToken());
}
int [] newArray = new int[array.length];
System.arraycopy(array, 0, newArray, 0, array.length);
import java.util.*;
class Max3 {
public static void main(String[] args) throws IOException {
int[] array = new int[20];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("콤마(,)로 구분하여 숫자들을 입력하세요...");
String digits = br.readLine();
StringTokenizer st = new StringTokenizer(digits, ",");
for(int i=0; st.hasMoreElements(); i++) {
array[i] = Integer.parseInt(st.nextToken());
}
int [] newArray = new int[array.length];
System.arraycopy(array, 0, newArray, 0, array.length);
int tmp=0;
for(int i=0; i < newArray.length-1; i++) {
if (newArray[i] > newArray[i+1]) {
tmp = newArray[i];
newArray[i] = newArray[i+1];
newArray[i+1] = tmp;
}
}
System.out.println("최대값은 : " + newArray[array.length-1]);
}
}
for(int i=0; i < newArray.length-1; i++) {
if (newArray[i] > newArray[i+1]) {
tmp = newArray[i];
newArray[i] = newArray[i+1];
newArray[i+1] = tmp;
}
}
System.out.println("최대값은 : " + newArray[array.length-1]);
}
}
[개강확정강좌]오라클자바커뮤니티에서 운영하는 개발자 전문교육 ,개인80%환급(www.onjprogramming.co.kr)
[주말]
[11/2]C#,ASP.NET마스터
[11/2]Spring3.X, MyBatis, Hibernate실무과정
[11/2]JAVA&WEB프레임워크실무과정
[평일야간]
[10/29]C#,ASP.NET마스터
[10/31]JAVA&WEB프레임워크실무과정
[11/1]Spring3.X, MyBatis, Hibernate실무과정
[주간]
[11/4]Spring3.X, MyBatis, Hibernate실무과정
[주말]
[11/2]C#,ASP.NET마스터
[11/2]Spring3.X, MyBatis, Hibernate실무과정
[11/2]JAVA&WEB프레임워크실무과정
[평일야간]
[10/29]C#,ASP.NET마스터
[10/31]JAVA&WEB프레임워크실무과정
[11/1]Spring3.X, MyBatis, Hibernate실무과정
[주간]
[11/4]Spring3.X, MyBatis, Hibernate실무과정
[기타 다른 강좌는 아래 해당 카테고리를 클릭해주세요]
댓글 없음:
댓글 쓰기