Java

배열의 요소를 반대로 나열하는 메서드 작성 import java.util.Scanner; public class ReverseArray1 { // -- 배열의 요소 a[idx1]와 a[idx2]를 교환 -- // static void swap(int[] a, int idx1, int idx2) { int t = a[idx1]; a[idx1] = a[idx2]; a[idx2] = t; } // -- 배열 a의 요소를 반대로 나열(오류) -- // static void reverse (int[] a) { try { for(int i=0; i
Throwable 클래스의 직접 하위 클래스인 Exception 클래스와 RuntimeException 클래스도 Throwable과 같은 형식의 생성자를 가진다. - Java31 참고 import java.util.Scanner; public class ThrowAndCatch { // -- sw값에 따라 예외 발생 -- // static void check(int sw) throws Exception{ switch(sw) { case 1: throw new Exception("검사 예외 발생!"); case 2: throw new Exception("비검사 예외 발생!"); } } // -- check를 호출 -- // static void test(int sw) throws Exception{ che..
import java.util.Scanner; public class MulDiv1 { public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); System.out.print("x값: "); int x = stdIn.nextInt(); System.out.print("y값: "); int y = stdIn.nextInt(); System.out.println("x * y = "+ (x*y)); System.out.println("x / y = "+ (x/y)); } } 런타임 오류와 try구문 위의 프로그램에서 오류가 날 수 있는 경우는 다음과 같다. 변수 y에 "ABC"가 입력된 경우. java.util.InputM..
String.equals 인수에 지정한 문자열과 같은지 확인하는 메서드 s1 과 s2 는 컴파일 이후에 각각의 입력을 통해 생성된 변수이므로 서로 다른 인스턴스를 참조한다. 문자열만을 비교했을 때 같으면 true, 틀리면 false를 리턴한다. import java.util.Scanner; Scanner stdIn = new Scanner(System.in); System.out.print("문자열 s1:"); String s1 = stdIn.next(); System.out.print("문자열 s2:"); String s2 = stdIn.next(); if(s1 == s2) System.out.println("s1 == s2 입니다."); else System.out.println("s1 != s2 입..
Codezoy
'Java' 태그의 글 목록