programming/java
[java] 자바 기초 문법과 클래스의 구성
몽구스_
2020. 12. 31. 23:38
728x90
반응형
이클립스에서 새로운 자바 클래스를 생성한다.
이 클래스는 메인 함수를 갖고 있는 클래스이며,
메인 함수 안에 변수를 설정하고 데이터를 출력한다.
Math.random : 랜덤값 반환 (0부터 시작)
isDigit : 숫자인지 아닌지 (boolean형)
trim : 공백 제거
toUpperCase : 대문자로 변환
.length : 길이
charAt : 인덱스 넣으면 그에 해당하는 데이터 반환 (0부터 시작)
public class test2 {
//클래스 구성요소
//멤버 변수+기능(메소드함수)
public static void main(String[] args) {
// TODO Auto-generated method stub
int num = (int)(Math.random() * 100 + 1); //1~100
int num2 = (int)(Math.random() * 100); //0~99
System.out.println(num);
/*숫자인지 아닌지 확인*/
char ch = 'a';
System.out.printf("%b", ch >= '0' && ch <= '9'); //직접구현
System.out.printf("%b", Character.isDigit(ch)); //내장함수사용
/*트림*/
System.out.println(" Hello world! ".trim()); //양쪽공백없앰
/*널예외 없애기*/
String msg = "Hello Java";
int idx = 9; //인텍스는 0부터
if(msg != null && (idx >= 0 && idx < msg.length())) {
System.out.println(msg.toUpperCase().charAt(idx));
}
else {
System.out.println("유효하지 않음");
}
System.out.println(msg.length()); //10
}
}
728x90
반응형