자바 알고리즘 기본 -입력을 받는 방법-

1. 최댓값 구하기 프로그램

 

3개의 정수를 입력받아 최댓값을 구하는 프로그램

 

package chap01;

import java.util.Scanner;

public class Max3 {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		System.out.println("세 정수의 최댓값을 구합니다.");
		System.out.println("a의 값: "); int a = stdIn.nextInt();
		System.out.println("b의 값: "); int b = stdIn.nextInt();
		System.out.println("c의 값: "); int c = stdIn.nextInt();
		
		//a,b,c의 최댓값을 구해서 max에 대입
		int max = a;
		if (b > max) max = b;
		if (c > max) max = c;
		
		System.out.println("최댓값은 " + max + "입니다. ");
	}

}

세 정수의 최댓값을 구합니다.
a의 값: 
1
b의 값: 
3
c의 값: 
2
최댓값은 3입니다.

 

 

2. 입력을 받는 방법

 

키보드로 정숫값을 입력하고 읽어들이는 방법

 

java.util.Scanner을 import

 

Scanner stdIn = new Scanner(System.in);

 

여기서 System.in은 키보드와 연결된 표준 입력 스트림이고 

 

stdIn.nextInt()는 키보드로 입력한 int형 정수를 읽어들인다

 

범위는 -2147483648~2147483647이고 알파벳이나 기호는 입력할 수 없다고함

 

package chap01;

import java.util.Scanner;

public class A {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		
		//정수를 입력받는다
		//int형 범위 -2147483648~2147483647
		stdIn.nextInt();
	}
}

 

3. 입력을 받는 방법2

 

자바에서는 입력받는 값의 자료형에 따라 다른 메소드를 사용해야한다....

 

대부분 next + 자료형()으로 생겼음

 

근데 걍 next()랑 nextLine()쓰면 안되나

 

메소드 자료형 입력값의 범위
nextBoolean() boolean true, false
nextByte() byte -128~+127
nextShort() short -32768~+32767
nextInt() int -2147483648~+2147483647
nextLong() long -9223372036854775808~+9223372036854775807
nextFloat() float +-3.40282347E+38~+-1.40239846E-45
nextDouble() double +-1.79769313486231507E+378~+-4.94065645841246544E-324
next() String 문자열(공백, 줄바꿈 문자로 구분)
nextLine() String 문자열 1줄

 

 

4. 메소드를 이용한 처리

 

특정 행위를 인자값에 따라 다르게 여러번 반복해서 구해야할때, 메소드를 만들어서 처리하면 편하다

 

package chap01;

import java.util.Scanner;

public class Max3Method {
	//a,b,c의 최댓값을 구해 반환
	static int max3(int a, int b, int c) {
		int max = a;
		if(b>max) {
			max = b;
		}
		if(c>max) {
			max = c;
		}
		
		return max;
	}
	
	public static void main(String[] args) {
		
		for(int i=0;i<13;i++) {
			
			Scanner stdIn = new Scanner(System.in);
			
			int a = stdIn.nextInt();
			int b = stdIn.nextInt();
			int c = stdIn.nextInt();
			
			System.out.println("max3(a,b,c): "+max3(a,b,c));
		}
	}
}

3
2
1
max3(a,b,c): 3
3
2
2
max3(a,b,c): 3
3
1
2
max3(a,b,c): 3
3
2
3
max3(a,b,c): 3
2
1
3
max3(a,b,c): 3
3
3
2
max3(a,b,c): 3
3
3
3
max3(a,b,c): 3
2
2
3
max3(a,b,c): 3
2
3
1
max3(a,b,c): 3
2
3
2
max3(a,b,c): 3
1
3
2
max3(a,b,c): 3
2
3
3
max3(a,b,c): 3
1
2
3
max3(a,b,c): 3

 

 

5. 알고리즘

 

"어떤 문제를 해결하기 위한 절차로, 명확하게 정의되고 순서가 있는 유한 개의 규칙으로 이루어진 집합"

 

명확하게 정의하더라도 변수 값에 따라 결과가 맞기도 하고 틀리기도 한다면 올바른 알고리즘이 아니다

 

 

6. 연습문제

 

1) 3개의 값의 최솟값을 구하는 프로그램

 

 

package chap01;

import java.util.Scanner;

public class Min3Method {
	
	static int min3(int a, int b, int c) {
		
		int min = a;
		
		if (b < min) {
			min = b;
		}
		
		if (c < min) {
			min = c;
		}
		
		return min;
	}
	
	public static void main(String[] args) {
		
		Scanner stdIn = new Scanner(System.in);
		
		int a = stdIn.nextInt();
		int b = stdIn.nextInt();
		int c = stdIn.nextInt();
		
		System.out.println("min3(a,b,c): " + min3(a,b,c));
	}

}

5
9
2
min3(a,b,c): 2
TAGS.

Comments