Scanner sc = new Scanner(System.in);
//변수의 선언
int number[] = null;
int count;
int updown;
// 변수의 초기화
count = 0;
updown = 0;
//////////////////////////////////////////////////////input
//정렬하고 싶은 숫자의 갯수
System.out.print("정렬하고 싶은 수의 갯수 = ");
count = sc.nextInt();
//배열에 그 갯수에 맞게 할당
number = new int[count];
//숫자들을 입력
for (int i = 0; i < count; i++) {
System.out.print((i + 1) + "번째 수 = ");
number[i] = sc.nextInt();
}
//오름(1)/내림(2)
System.out.print("오름(1)/내림(2) = ");
updown = sc.nextInt();
///////////////////////////////////////////////////////
//////////////////////////////////////////////////////sorting 처리
int temp;
for(int i = 0; i < number.length - 1; i++) {
for(int j = i + 1; j < number.length; j++) {
if(updown == 1 ) {//오름
if(number[i] > number[j]) {
//temp = number[i];
//number[i] = number[j];
//number[j] = temp;
swap(number, i, j);
}
}else { //내림
if(number[i] < number[j]) {
//temp = number[i];
//number[i] = number[j];
//number[j] = temp;
swap(number, i, j);
}
}
}
////////////////////////////////////////////////////////결과출력
if (updown == 1) {
System.out.println("오름차순정렬입니다");
}else {
System.out.println("내림차순정렬입니다");
}
for (int k = 0; k < number.length; k++) {
System.out.println("number[" + k + "] = " + number[k]);
}
}
}
static void swap(int number[], int i, int j) {
int temp;
temp = number[i];
number[i] = number[j];
number[j] = temp;
}
func();
func('A');
func(0);
func('b', 55);
func(55, 'b');
//가변인수
int s = sum(1, 2, 3);
System.out.println("합계 : " + s);
s= sum(100, 90, 80, 100, 70);
System.out.println("합계 : " + s);
}
////////////////////////////////////////////////////////함수
//아래 함수들 모두 함수명은 같으나 다른 함수로 인식
static void func() {
System.out.println("func() 호출");
}
static void func(char c) {
System.out.println("func(char c) 호출");
}
static void func(int i) {
System.out.println("func(int i) 호출");
}
static void func(char c, int i) {
System.out.println("func(char c, int i) 호출");
}
static void func(int i, char c) {
System.out.println("func(int i, char c) 호출");
}
// 가변인수
static int sum(int...number) {
int s = 0;
for(int i = 0; i< number.length; i++) {
s = s + number[i];
}
return s;
}
/*
형식 :
try{
//exception이 발생할 가능성이 있는 코드
}catch(예외클래스1 e){
메시지 출력
}catch(예외클래스2 e){
메시지 출력
}catch(예외클래스3 e){
메시지 출력
}finally{
무조건 실행
(복구 코드) - undo/rollback
}
static void func() throws 예외클래스 {
exception이 발생할 가능성이 있는 함수
}
*/
int array[] = {1, 2, 3};
System.out.println("start ---");
try {
for (int i = 0; i < 4; i++) {
System.out.println(array[i]);
}
System.out.println("process ---");
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("배열범위 초과");
//e.printStackTrace();
//System.out.println(e.getMessage());
//return;
}catch(Exception e) {
e.printStackTrace();
}finally { //무조건 실행
System.out.println("finally +++");
}
System.out.println("end ---");
//func();
//NullPointerException -> 문자가 들어있지 않을 때
String str = null; //<- 비어 있다
try {
System.out.println(str.length());
}catch (NullPointerException e) {
System.out.println("str이 null입니다");
}
//ArrayIndexOutOfBoundsException -> 배열이 초과되었을 때
int arr[] = {1, 2, 3};
try {
arr[3] = 4;
}catch(ArrayIndexOutOfBoundsException e) {
System.out.println("배열 범위 초과");
}
//FileNotFoundException -> 파일이 없을 때
File f = new File("d:\\xxxxxx");
FileInputStream is;
try {
is = new FileInputStream(f);
}catch(FileNotFoundException e) {
System.out.println("파일을 찾을 수 없습니다");
}
//StringIndexOutOfBoundsException -> 문자가 그 자리에 없을 때
String str1 = "java";
try {
str1.charAt(4);
}catch (StringIndexOutOfBoundsException e) {
System.out.println("글자가 없는 공간입니다");
}
//NumberFormatException -> 숫자대신 문자가 들어있을 때
try {
int i = Integer.parseInt("12a3");
}catch (NumberFormatException e) {
System.out.println("숫자가 아닌 글자가 있습니다");
}
Scanner sc = new Scanner(System.in);
int number = 0;
while(true) {
System.out.print("숫자입력 = ");
String strNum = sc.next();
try {
number = Integer.parseInt(strNum);
}catch (Exception e) {
System.out.println("숫자가 아닌 글자가 있습니다");
continue;
}
break;
}
System.out.println("number = " + number);
}
static void func() throws IndexOutOfBoundsException {
int num[] = {1, 2, 3};
for (int i = 0; i < 4; i++) {
System.out.println(num[i]);
}
}
File file = new File("/Users/ksy");
//파일을 조사
String filelist[] = file.list();
for (int i = 0; i < filelist.length; i++) {
System.out.println(filelist[i]);
}
//파일과 폴더를 구분해서 조사
File filelist[] = file.listFiles();
for (int i = 0; i < filelist.length; i++) {
if(filelist[i].isFile()) {
System.out.println("[파일]" + filelist[i].getName());
}
else if(filelist[i].isDirectory()) {
System.out.println("[폴더]" + filelist[i].getName());
}
else {
System.out.println("[?]" + filelist[i].getName());
}
}
//파일을 생성
File newfile = new File("/Users/ksy/tmp//newfile.txt");
try {
if(newfile.createNewFile()) {
System.out.println("파일 생성 성공!");
}else {
System.out.println("파일 생성 실패");
}
}catch(IOException e) {
e.printStackTrace();
}
//파일의 존재 여부
if(newfile.exists()) {
System.out.println("파일이 존재합니다");
}else {
System.out.println("파일이 존재하지 않습니다");
}
//읽기전용
newfile.setReadOnly();
//파일삭제
newfile.delete();
//(문자열)쓰기
File f = new File("/Users/ksy/tmp//iofile.txt");
try {
//파일이 없으면 생성한다
//쓰기
FileWriter fwriter = new FileWriter(f);
fwriter.write("안녕하세요");
fwriter.write("Hello");
fwriter.close();
//추가쓰기
FileWriter fwriter = new FileWriter(f, true);
fwriter.write("반갑습니다");
fwriter.close();
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f)));
pw.println("hi");
pw.print("헬로우");
pw.println("world");
pw.close();
//(문자열)읽기
//한글자씩 읽기
FileReader fr = new FileReader(f);
int ch = fr.read();
while(ch != -1) {
System.out.print((char)ch);
ch = fr.read();
}
//문장으로 읽기
BufferedReader br = new BufferedReader(new FileReader(f));
String str = "";
while((str = br.readLine()) != null) {
System.out.println(str);
}
br.close(); //반드시 닫아줘야 함
} catch (IOException e) {
e.printStackTrace();
}
}
Window에서는 ("c:\program\)로 작성, Mac OS에서는 ("/User/ksy")와 같이 슬래시 방향이 다름