본문 바로가기

JAVA/기초 프로그래밍

(55)
객체//tv프로그램 main public class mainClass_tv2 { public static void main(String args[]) { MyTv2 t = new MyTv2(); t.setChannel(10); System.out.println("CH:"+t.getChannel()); t.setChannel(20); System.out.println("CH:"+t.getChannel()); /////////////////////이전채널 돌아가기 t.gotoPrevChannel(); System.out.println("CH:"+t.getChannel()); t.gotoPrevChannel(); System.out.println("CH:"+t.getChannel()); } } ** main의 이전채널 돌아가기는..
객체//모래게임 main public static void main(String[] args) { //모래게임 MyGametea game = new MyGametea(); game.init(); game.gamePlay(); game.result(); } class public class MyGametea { int rand_num; int user_num; boolean gameOver; public void init() { gameOver = false; setRandom(); } public void setRandom() { rand_num = (int)(Math.random() * 71) + 30; System.out.println("rand_num = " + rand_num); } public void user..
함수// 학생 성적 입출력 프로그램 1. static int getSearchIndex(String student[][], String name) { //2개이상 중복되는 코드는 함수로 빼주는 것이 좋음 int findIndex = -1; for (int i = 0; i < student.length; i++) { String n = student[i][0]; if(n.equals(name)) { findIndex = i; break; } } return findIndex; } 를 getSearchIndex로 함수로 빼주고 return 값을 줘서 빈 인덱스 자리를 찾아 입력하게 될 때마다 int findIndex = getSearchIndex(student, name); 로 호출하여 사용함 2. static void allPrint(Str..
학생 성적관리 프로그램 (미완성) public static void main(String[] args) { /* 학생 성적 관리 String student[][]; 메뉴 --- 1. 학생 정보 추가insert(예: 홍길동, 나이, 영어, 수학) 2. 학생 정보 삭제delete 3. 학생 정보 검색search 4. 학생 정보 수정update 5. 학생 정보 모두 출력 6. 과목의 총점-> 1. 영어2.수학 7. 과목의 평균 8. 성적순으로 정렬 9. 데이터 저장 */ Scanner sc = new Scanner(System.in); String student[][] = new String [20][4]; int choice; for (int i = 0; i < student.length; i++) { for (int j = 0; j < s..
파일//함수// 배열 파일을 읽어오기 File file = new File("d:\\tmp\\test.txt"); String arrStr[] = { "Hello World", "안녕하세요", "Happy day", "Nice Day", }; String arrayStr[] = dataLoad(file); for (int i = 0; i < arrayStr.length; i++) { System.out.println(arrayStr[i]); } static String[] dataLoad(File f) { String str[] = null; try { FileReader fr = new FileReader(f); // 데이터를 카운터 (몇개?) int count = 0; String s; BufferedReader br = new Buff..
파일//함수// 배열을 파일에 저장하기 배열 : String arrAtr[] = { "Hello", "안녕하세요", "Hi" }; File file = new File("d:\\tmp\\test.txt"); // 문자열 String arrStr[] = { "Hello World", "안녕하세요", "Happy day", "Nice Day", }; boolean b = dataSave(file, arrStr); if(b) { System.out.println("성공적으로 파일에 데이터가 저장되었습니다"); }else { System.out.println("파일에 데이터가 저장되지 않았습니다"); } } static boolean dataSave(File f, String datas[]) { try { FileWriter fWriter = new..
함수// 야구게임 /* 1. random 2. userinput 3. finding 4. message 5. result */ int r_num[] = new int[3]; int u_num[] = new int[3]; random(r_num); int w = 0; userInput(u_num); boolean b = finding(u_num, r_num); if(b == true) { clear = true; break; } w++; } resultPrint(clear); } static void random(int r_num[]) { boolean swit[] = new boolean[10]; for (int i = 0; i < swit.length; i++) { swit[i] = false;// 00000 00000..
함수// 입력받은 배열 sorting swap (답안) 1. 입력(숫자들, 오름/내림) 2. 정렬처리 swap() 3. 출력 주석 없앤 버전 int number[] = null; boolean updown[] = new boolean[1];// address 에 의한 할당 number = userInput(updown); sorting(number, updown[0]); // 결과 result(number, updown[0]); // prototype static int[] userInput(boolean ud[]) {//입력값 배열 리턴값 배열 Scanner sc = new Scanner(System.in); // 정렬할 갯수? System.out.print("정렬할 갯수는 = "); int count = sc.nextInt(); // 정렬할 숫자를 동적할..