4. 출력 메소드
1) system 클래스 구성하는 3가지 필드
- in : 입력받기 위한 필드
- out : 콘솔로 출력하기 위한 필드
- err : 에러 메세지를 콘솔에 표시
ex) 키보드에서 문자 한 개 입력받기
import java.io.IOException;
public class tets {
public static void main(String[] args) throws IOException {
int data = 0;
data = System.in.read(); //8
System.out.println(data); //56 (유니코드로 읽어옴)
System.out.println((char)data); //8
}
}
2) 출력 메소드 종류
- println / print(내용)
- printf("형식 지정자", 내용) : 형식지정자에 맞게 내용을 출력한다.
5. 입력받기 (scanner)
1) 정수로 입력받기
ex) import java.util.Scanner;
public class tets {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int a,b,c;
System.out.println("정수 데이터 입력하시오");
a = input.nextInt();
b = input.nextInt();
c=a+b;
System.out.printf("%3d+%3d=%4d", a,b,c);
}
}
2) 실수로 입력받기
ex) import java.util.Scanner;
public class tets {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double a,b,c;
System.out.println("실수 데이터 입력하시오");
a = input.nextDouble();
b = input.nextDouble();
c=a+b;
System.out.printf("%7.2f+%7.2f=%7.2fd", a,b,c);
}
}
3) 문자열 입력받기
ex) import java.util.Scanner;
public class tets {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String greeing, name;
System.out.print("인삿말 입력하시오");
greeing = input.nextLine();
System.out.print("이름을 입력하시오");
name = input.nextLine();
System.out.printf("입력한 내용 :"+greeing+","+name);
}
}
6. 메소드 호출하기
= 객체참조변수명.메소드명(); || 객체참조변수명.메소드명(인수1, 인수2);
'자바 이야기' 카테고리의 다른 글
DB 연동방식 (0) | 2020.07.30 |
---|---|
자바 이야기 6. 메소드의 활용 (0) | 2020.05.12 |
자바 이야기 4. 배럭만들기와 한 부대씩 움직이게 하기 (0) | 2020.02.28 |
자바 이야기 2. 내 맘대로 조종하기 / 귀찮은거 반복하게 하기 (0) | 2020.02.24 |
자바 이야기 0. 유닛 타입 만들기 (0) | 2020.02.19 |