목록IT (131)
이야기앱 세상
대문자를 소문자로 소문자를 대문자로 바꾸기(대소문자 변환) String str = "abcMDye-4W?EWzz"; //String의 메서드 활용하기String result = ""; for(int i=0;i=65 && c=97 && c
여러개의 숫자 중 최대값, 최소값 구하기 ArrayList와 Collections.max() 최대값, Collections.min() 최소값 ArrayList list = new ArrayList();list.add(12);list.add(39);list.add(8);list.add(10); //최대값Integer i = Collections.max(list);System.out.println(i); //39 출력 //최소값Integer i2 = Collections.min(list);System.out.println(i2); //8 출력
배열을 ArrayList로 변환 String[] city = {"서울","부산","대구","광주"};ArrayList al = new ArrayList();Collections.addAll(al, city); --------------------------------------------String[] country = {"한국","미국","중국","일본"};List al2 = new ArrayList();al2 = Arrays.asList(country); ============================================ArrayList 를 배열로 변환 ArrayList al3 = new ArrayList(); al3.add(1); al3.add(2); al3.add(3); al3.add..
double num = 123.623656; //반올림System.out.println(Math.round(num)); //반올림String pattern = "0.###";DecimalFormat df = new DecimalFormat(pattern); System.out.println(df.format(num)); //반올림BigDecimal bd = new BigDecimal(String.valueOf(num));BigDecimal result = null; result = bd.setScale(2, BigDecimal.ROUND_DOWN); //내림System.out.println(result); result = bd.setScale(3, BigDecimal.ROUND_HALF_UP); //반올..
재귀호출(recursive call)는 메서드 내부에서 자기 자신 메서드를 호출하는 것이다. 팩토리얼(factorial)은 한 숫자가 1일 될 때까지 1씩 감소시켜가면서 계속 곱하는 것을 의미한다. f(n) = n * f(n-1) 이며 f(1) = 1 이 된다. 재귀호출을 이용하면 쉽게 팩토리얼을 구할 수 있다. -------------------------------------------------------------------------- class DoFactorial{ public static long makeFact(int n){ long result = 0; if(n == 1){ result = 1; }else{ result = n * makeFact(n-1); } return result;..
Vector를 이용한 로또 예제--------------------------------import java.util.Collections; import java.util.Random; import java.util.Vector;public class VectorLotto { public static void main(String[] args){ Vector vc = new Vector(); Random ran = new Random(); Integer ir = null; while(vc.size()
HashSet를 이용한 로또 만들기 ------------------------------- package dr03.random; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Random; import java.util.Set; public class LottoHashSet { Set vc; public LottoHashSet(){ vc = new HashSet(); this.doLotto(); this.printLotto(); } public void doLotto(){ Random ra = new Random(); Intege..
배열을 이용한 로또 프로그램 만들기 ----------------------------------- package dr03.random; import java.util.Arrays; public class LottoArray { int[] lotto = new int[6]; public LottoArray(){ this.doLotto(); this.printLotto(); } // 로또 숫자 만들기 public void doLotto(){ for(int i=0;i