본문 바로가기

자바 DB 연동/입출력 스트림

System.in.read()

public class test {

public static void main(String[] args) {

int ch;

try {

System.out.print("입력하고 싶은 단어 입력[Ctrl+Z:종료]");

while((ch=System.in.read())!=-1){ // 아스키코드 -1은 Ctrl+z

System.out.print(ch); //ASCII 코드

System.out.print(":"+((char)ch));

System.out.println();

}

}catch(Exception e){

}

}

}


===================================================================================


public class test {

public static void main(String[] args) {

int ch;

int n=0, s=0;

System.out.print("수?");

try{

while((ch=System.in.read())!=13){ // 아스키코드 13은 케리지리턴

n=n*10 + (ch-48);

}

for(int i=1; i<=n; i++)

s+=i;

System.out.println("1~"+n+"까지 합 : " + s);

}catch(Exception e){

}

}

}

====================================================================================


public class fffff {

public static void main(String[] args) {

int ch;

try {

System.out.print("입력하고 싶은 단어 입력[Ctrl+Z:종료]");

while((ch=System.in.read())!=-1){

char c=(char)ch;

// System.out.write(c); 한 바이트로 출력(한글안깨짐)

System.out.print(c); // 한글 깨짐

}

}catch(Exception e){

}

}

}