JavaStudy day4
주제
031 ‘a’의 유니코드 값 구하기
유니코드와 char, int와의 관계를 이해한다. 문자 관련 랩퍼 클래스인 Character의 사용 방법을 익힌다.
- IntToChar.java(**)
package kr.co.infopub.chap031;
public class IntToChar
{
public static void main(String[] args)
{
for(int i=21;i<=60;i++){
//System.out.printf("[%c] ",i);
System.out.print("["+(char)i+"] ");
// (char)i --> int를 char로 casting
if(i%10==0){
System.out.println();//한칸을 내려라
}
}
System.out.println('a');
//System.out.println('\u0041');
System.out.println('A');
System.out.println("알파벳 : "+('z'-'a'+1)+"자");
char cc='한';
System.out.println(cc);//한
System.out.println((int)cc);//int
char zChar='0';//Unicode 48
int count=(int)zChar;
System.out.println(count);
for(int i=count;i<=count+9;i++){
System.out.print("["+(char)i+"] ");
}
System.out.println();//한칸을 내려라
System.out.println('\u0001'); //
System.out.println('\1'); //
System.out.println('\u0015'); //16진법
System.out.println('\25'); //8진법
for(int i=0;i<=39;i++){
System.out.printf("[%c] ",('\25'+i));
if((i+1)%10==0){
System.out.printf("%n");//한칸을 내려라
}
}
}
}
- UsingCharacterMain.java(*)
package kr.co.infopub.chap031;
public class UsingCharacterMain {
public static void main(String[] args) {
Character chr1=new Character('a');
Character chr2=new Character('9');
if(Character.isLetter(chr1.charValue())){
System.out.println("1 : "+chr1.charValue());
}
if(Character.isDigit(chr2.charValue())){
System.out.println("2 : "+(int)chr2.charValue());
}
System.out.println("3 : "+Character.getType('a'));//소문자 2
System.out.println("4 : "+Character.getType('A'));//대문자 1
System.out.println("5 : "+Character.getType('3'));//숫자 9
System.out.println("6 : "+Character.getType('&'));//특수문자 24
//OTHER_PUNCTUATION 24
System.out.println("7 : "+Character.getType('\n'));//white 15
System.out.println("8 : "+(int)'\b');//8
//9~13 28~31 white space
System.out.println("9 : "+(int)'\t');//9 //white space
System.out.println("10: "+(int)'\n');//10
System.out.println("11: "+(int)'\f');//12
System.out.println("12: "+(int)'\r');//13
System.out.println("13: "+Character.getNumericValue('9'));//숫자
//System.out.println("13: "+Character.getNumericValue(chr2.charValue()));//숫자
System.out.println("14: "+Character.isWhitespace(' '));
System.out.println("15: "+Character.isWhitespace('\n'));
System.out.println("16: "+Character.isWhitespace('\r'));
System.out.println("17: "+Character.isWhitespace('\b'));//false
System.out.println("18: "+Character.isLetterOrDigit('9'));
System.out.println("19: "+Character.isLowerCase('A'));
System.out.println("20: "+Character.isUpperCase('A'));
//radix 기수 0~9가 기본
System.out.println("21: "+Character.digit('8',9));
//SPACE_SEPARATOR 12 '\f'
//LINE_SEPARATOR 13 '\r'
//PARAGRAPH_SEPARATOR 14
//SPACE 32
System.out.println("22: "+(int)' ');//32
System.out.println("23: "+Character.isSpaceChar(' '));
System.out.println("24: "+Character.isSpaceChar('\n'));
}
}
032 점수에 따라 등급 매기기(중첩 if 문)
if~else if 이용법을 익힌다.
- IfTest1Chap32.java(*)
package kr.co.infopub.chap032;
public class IfTest1Chapt32 {
public static void main(String[] args)
{
int valuA=90;
int valuB=80;
int valuC=70;
int valuD=60;
int a=78;
if(a>=valuA){
System.out.println("A");
}else if(a>=valuB){
System.out.println("B");
}else if(a>=valuC){
System.out.println("C");
}else if(a>=valuD){
System.out.println("D");
}else{
System.out.println("F");
}
int b=78;
if(b>=valuA){
System.out.println("A");
}else{
if(b>=valuB){
System.out.println("B");
}else{
if(b>=valuC){
System.out.println("C");
}else{
if(b>=valuD){
System.out.println("D");
}else{
System.out.println("F");
}
}
}
}
}//main
}
033 #의 개수를 늘려 출력하기 (중첩 for 문)
for 안에 for를 포함하는 중첩 for 문을 익힌다.
- NestedForStarChapt33.java(*)
package kr.co.infopub.chap033;
public class NestedForStarChapt33 {
public static void main(String[] args) {
//중첩for
for(int i=0;i<10;i++){
for(int j=0;j<=i;j++){
System.out.print("#");
}
System.out.println();
}
for(int i=0;i<11;i++){
for(int j=0;j<=10-i;j++){
System.out.print("@");
}
System.out.println();
}
}
}
034 조건 선택문 (switch~case~default)과 Enumerations(enum)
조건 선택문의 사용법을 배운다.
- SwitchTestChap34.java(**)
package kr.co.infopub.chap034;
public class SwitchTestChap34 {
final static int valuA=1;
final static int valuB=2;
final static int valuC=3;
final static int valuD=4;
final static int valuE=5;
public static void main(String[] args)
{
int a=valuC;
int b=valuD;
long c=5L; //switch case presision 에러
switch(a){ //byte, shor, int, char 가능
case 1 : System.out.println("1");break;
case 2 : System.out.println("2");break;
case 3 : System.out.println("3");break;
case 4 : System.out.println("4");break;
default : System.out.println("5");break;
}
switch(b){ //byte, shor, int, char 가능
case valuA : System.out.println("1");
case valuB : System.out.println("2");
case valuC : System.out.println("3");
case valuD : System.out.println("4");
case valuE : System.out.println("5");break;
default : System.out.println("default");break;
}
}//main
}
- Season.java (**)
package kr.co.infopub.chap034;
//JAVA 5~7
public enum Season
{
SPRING,SUMMER,AUTUMN,WINTER
}
- SwitchEnumCapt34.java (**)
package kr.co.infopub.chap034;
public class SwitchEnumCapt34
{
//JAVA 5~7
public static void main(String[] args)
{
Season thisSeason=Season.SPRING;
switch(thisSeason){//case에서 Season.SPRING 하면 에러
case SPRING: System.out.println("SPRING");break;
case SUMMER: System.out.println("SUMMER");break;
case AUTUMN: System.out.println("AUTUMN");break;
case WINTER: System.out.println("WINTER");break;
}
//Season 안에 있는 모든 것
Season [ ] thisSeasons=Season.values();
for(int i=0;i<thisSeasons.length;i++){
System.out.println(i+" "+thisSeasons[i]);
}
//String을 enum으로
Season thisSeason1=Season.valueOf("AUTUMN");
System.out.println(thisSeason1);
}
}
035 1부터 10까지 출력하기 (while 문)
while 문의 사용법을 배운다.
- WhileTest1Chapt35.java (*)
package kr.co.infopub.chap035;
public class WhileTest1Chapt35
{
public static void main(String[] args)
{
int i=0;
while( i<10 ){
System.out.print("["+(i++)+"] ");
}
System.out.println();
}
}
- WhileTest2Chapt35.java (*)
package kr.co.infopub.chap035;
public class WhileTest2Chapt35
{
public static void main(String[] args)
{
int m=150;
int n=330;
while(m!=n){
System.out.print("["+m+","+n+"] ");
if(m>n)m-=n;
else if(n>m)n-=m;
}
System.out.println();
System.out.println("최대공약수 :"+m);
}
}
036 16부터 20까지 출력하기 (do while)
적어도 한 번은 실행하는 do~while 문의 사용법을 배운다.
- DoWhileTest1Chapt36.java (**)
package kr.co.infopub.chap036;
public class DoWhileTest1Chapt36
{
public static void main(String[] args)
{
int i=15;
do{
i++;
System.out.println("i 값? "+i);
}while(i<20);
}
}
- DoWhileTest2Chapt36.java (*)
package kr.co.infopub.chap036;
public class DoWhileTest2Chapt36
{
public static void main(String[] args)
{
int m=150;
int n=330;
do{
System.out.print("["+m+","+n+"] ");
if(m>n)m-=n;
else if(n>m)n-=m;
}while(m!=n);
System.out.println();
System.out.println("최대공약수 :"+m);
}
}
037 1부터 10까지의 짝수와 홀수 구분하기 (break, continue)
break, continue와 순환문의 관계라 이해한다.
- WhileBreakChapt37.java (**)
package kr.co.infopub.chap037;
public class WhileBreakChapt37
{
public static void main(String[] args)
{
int i=0;
while(true){
i++;
if(i%2==0){
System.out.print("["+i+"] ");
}else {
System.out.print("("+i+") ");
}
if(i==10){
break;
}
}
}
}
- DoWhileBreakChapter37.java (**)
package kr.co.infopub.chap037;
public class DoWhileBreakChapt37
{
public static void main(String[] args)
{
int i=0;
do{
i++;
if(i%2==0){//|
System.out.print("["+i+"] ");
}else {
System.out.print("("+i+") ");
}
if(i==10){
break;
}
}while(true);
}
}
- ContinueTest1Chapt37.java (**)
package kr.co.infopub.chap037;
public class ContinueTest1Chapt37
{
public static void main(String[] args)
{
for( int i=0; i<10;i++){
if(i%2==0){
continue; //다음 스텝 i
}
System.out.print(" [ "+i+" ] ");
}//
}
}
- BreakTest1Chapt37.java (**)
package kr.co.infopub.chap037;
public class BreakTest1Chapt37
{
public static void main(String[] args)
{
for(int i=0;i<10;i++){
aa:{ //가능하면 사용을 줄이자
System.out.println("i ==>"+i);
for(int j=0;j<2;j++){
if(i%5==1 || i%5==2){
System.out.println("i :"+i+" j:"+j);
}else break aa;
}// for
}// aa:
}// for
}
}
038 예외처리(try~catch)
try~catch를 이용해서 예외를 처리하는 방법을 익힌다.
- TryCatch1Chapt38.java (***)
package kr.co.infopub.chap038;
public class TryCatch1Chapt38
{
public static void main(String[] args)
{
String sNum="123";
String nNum="h";
try{
int a=Integer.parseInt(sNum);
System.out.println(a);
}catch(NumberFormatException ee){
System.out.println("int인지 확인해 봐!!!!");
//System.err.println(ee);
//ee.printStackTrace();
System.err.println(ee.getMessage());
}catch(Exception ee){
System.out.println("야 잘 좀 넣어");
//System.err.println(ee.getMessage());
//System.err.println(ee);
//ee.printStackTrace();
}finally{
System.out.println("난 수행되어야 만 해!!!!");
}
}
}
- TryCatch2Chapt38.java (***)
package kr.co.infopub.chap038;
public class TryCatch2Chapt38 {
public static void main(String[] args) {
String sNum="123";
String nNum="h";
try{
int a=Integer.parseInt(nNum);
System.out.println(a);
}catch(Exception ee){
System.out.println("==> 에러 !!!!");
System.err.println(ee+"<==");
}finally{
System.out.println("난 수행되어야 만 해!!!!");
}
try {
// Divide by zero.
int x = 5;
int y = 20 / (5 - x);
} catch (ArithmeticException e) {//run time
System.out.println("==> 0으로 나눴나 확인해 봐!!!!");
e.printStackTrace();
}
}
}
039 키보드로 문자 입력받기 (args, Scanner)
main의 args 사용 방법을 익힌다. Java 5부터 추가된 Scanner를 이해한다.
- ArgsInputChapt39.java (***)
package kr.co.infopub.chap039;
public class ArgsInputChapt39 {
//java kr.co.infopub.chap039.ArgsInputChapt39 hello java200
public static void main(String[] args) {
if(args.length<=0){
System.out.println("아규먼트가 없다.");
}else if(args.length==1){
System.out.println("아규먼트 1개: "+args[0]);
}else if(args.length==2){
System.out.printf("아규먼트 2개: %s %s",args[0],args[1]);
}else{
System.out.printf("Too many!!");
}
}
}
- ScannerChapt39.java (***)
package kr.co.infopub.chap039;
import java.util.Scanner;
public class ScannerChapt39 {
public static void main(String[] args) {
System.out.println("정수를 입력하세요.");
Scanner input=new Scanner(System.in);
int n=input.nextInt();
System.out.println(n);
}
}
- Scanner2Chapt39.java (***)
package kr.co.infopub.chap039;
import java.util.Scanner;
public class Scanner2Chapt39 {
public static void main(String[] args) {
System.out.println("문자를 입력하세요.");
String str=readString();//문자 읽기
char [] charStr=str.toCharArray();
int count=charStr.length;
System.out.println("글자수: "+count);
for(int i=0;i<count;i++){
System.out.print(charStr[i]+" : ");
}
System.out.println("\n정수를 입력하세요.");
int num=readInt(); //숫자를 입력
System.out.println("입력된 수 : "+num);
}
public static int readInt(){
Scanner input=new Scanner(System.in);
return input.nextInt();
}
public static double readDouble(){
Scanner input=new Scanner(System.in);
return input.nextDouble();
}
public static String readString(){
Scanner input=new Scanner(System.in);
return input.nextLine();
}
}
040 문자열을 숫로 바꾸기 (Integer.parseInt)
원하는 타입으로 변환하는 방법을 익힌다.
- TypeConversion1Chapt40.java (**)
package kr.co.infopub.chap040;
public class TypeConversion1Chapt40 {
public static void main(String[] args) {
String strNum1="12345";
int tempNum1=2345;
//String--> int
int intNum1=Integer.parseInt(strNum1);
double douNum1=Double.parseDouble(strNum1);
//int--> String
String strNum2=Integer.toString(tempNum1);
String strNum3=tempNum1+"";
//String <--> char
char [] charStr={'H','e','l','l','o'};
String strChar=new String(charStr);
char [] charStr2=strChar.toCharArray();
char charChar=strChar.charAt(4);//'0'
String sv=String.valueOf(charStr2);
System.out.println(sv);
//String Concatenation
String s="Get ";
s+="java ";
s+="this time.";
String st="";
st.concat("java ").concat("this time.");
System.out.println("=받지않음=>"+st);//immutable
st=st.concat("java ").concat("this time.");
System.out.println("=받기<="+st);
String stss="안녕하세요.";
//byte<-->String
byte[] byteStr=stss.getBytes();//S-->b
for(int i=0;i<byteStr.length;i++){
System.out.print(byteStr[i]+" ");
}
System.out.println();
String strByte=new String(byteStr);//b-->s
System.out.println(strByte);
}
}
댓글 없음:
댓글 쓰기