개발일지/Java + Spring (86) 썸네일형 리스트형 sc.close() 닫아주는 이유 입력을 사용할 때 키보드를 통해서 입력 받는 경우도 있지만 파일 등을 통해서 입력 받는 경우도 있다. 파일 작업의 순서는 사용하고자 하는 파일을 열고, 사용, 닫기순인데요. 파일을 열어두고 닫지 않을 때 파일 손상이 될 수 있다. 기본적으로 파일 작업은 열고/닫고 해주어야 한다. 스캐너 생성시 파라미터로 값을 넘기는데 (System.in)에서 in이 키보드 입력을 가리킨다. close() 해주지 않아도 상관 없지만 리소스 사용하는 경우에는 되도록 close() 해주는 습관을 들이는 게 좋다고 함 클래스 이용해서 학점 계산하기_진짜_최종.java package Test; public class Grade{ int tot; double avg; String grade; void printS(String name, int k, int e, int m) { System.out.println("당신의 이름은 " + name + "입니다."); System.out.println("국어 성적 : " + k ); System.out.println("영어 성적 : " + e ); System.out.println("수학 성적 : " + m ); } void tot(int kor, int eng, int math) { tot = kor + eng + math; System.out.println("총점 : " + tot); } void avg() { avg = t.. 예외처리(try-catch) Try 8 : 작업을 하다가 문제가 생기면 캐치문으로 가라 모든 수를 0으로 나누는 경우 .. 0이 나오면 자동으로 19번으로 간다. 9 : args 창에서 입력 받음 catch 13 : 배열에 관한 문제 16 : (인자)데이터타입 문제 19 : 0으로 나눈 문제가 생겼을 때 대소문자 제대로 써줘야함 인터페이스 클래스 원래 자바에서는 클래스 두 개 상속 받는 게 금기시 된다. 추상클래스도 안 됨 근데 인터페이스 클래스는 가능함 유일하게 가능하다. interface Interface1{ int interVar = 10; void interface1Method(); } class Interface1Impl implements Interface1{ @Override public void interface1Method() { System.out.println("interface1Method 구현"); } } public class InterfaceTest1 { public static void main(String[] args) { Interface1Impl in1Impl = new Interface1Impl(); Syste.. 데이터베이스 자바에서 연결하기 import java.io.*; import java.sql.*; public class Main { public static void main(String[] args) { Connection conn; Statement stmt = null; try { Class.forName("com.mysql.cj.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/" + "Application?useSSL=false&serverTimezone=Asia/Seoul", "root", "1234"); //JDBC 연결 System.out.println("DB 연결 완료"); stmt = conn.createStatement();.. [Java] File 만들기/읽기 import java.io.*; import java.util.Scanner; public class PhoneWriterEx { public static void main(String[] args) { FileWriter fw = null; //초기값 File f = new File("c:\\temp\\phone.txt"); //저 장치 밑에 저장해 줌 try { fw = new FileWriter(f); Scanner sc = new Scanner(System.in); System.out.println("전화번호 입력 프로그램입니다."); while(true) { System.out.print("이름 전화번호 >> "); String line = sc.nextLine(); //한 줄을 읽는다. if(.. [Java] 거스름 돈 출력 - 예제 import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] coin = { 1000, 500, 100, 50, 10 }; int sum = 0; //지불금액 System.out.print("물건 값을 입력하세요 : "); int mulG = sc.nextInt(); System.out.print("지불한 돈의 액수를 입력하세요 : "); int inputM = sc.nextInt(); //출력 sum = inputM-mulG; if(inputM>mulG) { System.out.println("거스름 돈 : " + sum + "원".. [Java] 추상클래스 abstract class AbstractClass{ //변수 선언 int age; //구현된 메소드 void generalMethod() { System.out.println("일반 메소드"); } //추상 메소드 abstract void abstractMethod(); //추상 메소드는 {} 실행부분이 있으면 안 됨 } class AbstractChildClass extends AbstractClass{ //상속 @Override void abstractMethod() { System.out.println("추상 메소드 구현"); } } public class AbstractTest1 { public static void main(String[] args) { //추상 클래스로 객체 생성 //Abst.. 이전 1 ··· 5 6 7 8 9 10 11 다음