- 조건문 - if, switch
제어문
- 반복문 - while, do while, for
* 조건문
1. if
1. if(조건식) - 참
문장;
조건식이 참이면 문장을 실행한다. (if의 기본적인 실행방법)
Ascii(American Standard Code for Information Interchange) 컴터에서 인식 할 수 있는 코드값
class AsciiTest
{
public static void main(String[] args)
{
char ch = 97;
System.out.println(ch);
int i = 'B';
System.out.println(i);
for(char ch1 = 'a'; ch1< 'z'; ch1++)
System.out.print(ch1+" : " +(int)ch1+"\t"); // 문자와 그 에 해당하는 Ascii값을 출력
}
}
기본적인 ifTest
class ifTest
{
public static void main(String[] args) throws java.io.IOException
{
int i;
System.out.print("숫자 하나 입력: ");
i = System.in.read() - 48;
if( i % 2 == 0)
System.out.println("짝수입니다.");
else
System.out.println("홀수입니다.");
}
}
#키보드 입력
키보드 입력 하나의 문자만 입력 받는다.. 여러문자를 하려면 추후 IO 클래스를 배워야지만 가능..
class InputTest
{
public static void main(String[] args) throws java.io.IOException
{
char ch;
int i;
System.out.print("숫자 입력: ");
i = System.in.read() - 48;
System.out.println("내가 입력한 숫자: " + i);
System.in.skip(2); // 문자열 입력 버퍼를 건너띰
System.out.print("문자 입력: ");
ch = (char)System.in.read();
System.out.println("내가 입력한 문자: " + ch);
System.in.skip(4);
System.out.print("숫자 입력: ");
i = System.in.read() - 48;
System.out.println("내가 입력한 숫자: " + i);
}
}
버퍼 입력
입력 버퍼는 주기억 장치…
컴터로 전송되기 전에 입력 버퍼가 있어서 잠시 저장 그 후 프로그램으로 넘겨 출력..
a |
\n |
|
|
|
\n 다음 버퍼에 겹치기 때문에 문제가 생김—in.skip() 함수로 해결..
1. if
1. if(조건식) - 참
문장;
else(조건식) – 참
문장;
else –참 //옵션 있어도 되기 없어도 됨
문장;
조건식이 참이면 문장을 실행한다. (if의 기본적인 실행방법)
class MultiIfTest
{
public static void main(String[] args)
{
int avg = 65;
char c;
if( avg >= 90 )
c = 'A';
else if( avg >= 80)
c = 'B';
else if( avg >= 70)
c = 'C';
else if( avg >= 60)
c = 'D';
else
c = 'F';
System.out.println("학점 : "+c);
}
}
ex1
class ex1
{
public static void main(String[] args) throws java.io.IOException
{
int i;
System.out.print("숫자 하나 입력: ");
i = System.in.read() - 48;
if( i >= 1 && i <= 3 )
System.out.println("정답");
else
System.out.println("오답");
}
}
ex2 예제
class ex2
{
public static void main(String[] args) throws java.io.IOException
{
char ch;
System.out.print("문자 입력: ");
ch = (char)System.in.read();
if( (ch >='A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))
System.out.println("문자입니다.");
else
System.out.println("알수 없는 데이터");
}
}
연산자 뿐만이라 아니라 값도 입력 가능하게끔 코딩
lass ex3
{
public static void main(String[] args) throws java.io.IOException
{
int a = 2, b = 3, c = 0;
System.out.print("숫자 입력: ");
a = System.in.read() - 48;
System.in.skip(2);
System.out.print("숫자 입력: ");
b = System.in.read() - 48;
System.in.skip(2);
char ch;
System.out.print("+, -, *, / 중에 입력: ");
ch = (char)System.in.read();
if( ch == '+' )
c = a + b;
else if( ch == '-' )
c = a - b;
else if( ch == '/' )
c = a / b;
else if( ch == '*' )
c = a * b;
else
System.out.println("+, -, *, / 중에 입력하세요..");
System.out.println("결과 : "+c);
}
}
ex4 소문자, 대문자,
class ex4
{
public static void main(String[] args) throws java.io.IOException
{
char ch;
System.out.print("데이터 입력: ");
ch = (char)System.in.read();
if(ch >=65 && ch <= 90)
System.out.println("대문자입니다.");
else if(ch >= 97 && ch <= 122)
System.out.println("소문자입니다.");
else if(ch >= 48 && ch <= 57)
System.out.println("숫자입니다.");
else
System.out.println("알수 없는 데이터");
}
}
if는 다중 if문 가능…
if(조건식) {
if(조건식)
문장:
}
else
{
if(조건식)
문장:
}
switch(변수나 수식){
case 값:
문장
break: // break가 있어야지 끝남 없으면 밑의 case까지 계속 실행..
case 값:
문장
break;
default: // 옵션: 있어도 되고 없으면 그만..
문장
* 반복문
while, do while, for
초기화, 조건, 카운터 <- 반복횟수
while(조건){
문장
}
while에 대한 문제.. 1, 2, 3, 4 문제 통합 소스
class ex5
{
public static void main(String[] args)
{
int i = 1;
int sum = 0;
while(i<=10){
sum +=i;
i++;
}
System.out.println("1에서 10까지의 합: "+sum); //문제1
i = 1;
sum = 1;
while(i<=10){
sum *= 2;
i++;
}
System.out.println("2^10 :"+sum); //문제2
i = 1;
int cnt = 0;
while(i<=10){
if(i%2 == 0) cnt++;
i++;
}
System.out.println("짝수의 개수: "+cnt); //문제3
i = 1;
int sum1 = 0;
int sum2 = 0;
while(i<=10){
if(i%2 == 0) sum1 += i;
else sum2 += i;
i++;
}
System.out.println("짝수의 합값: " +sum1);
System.out.println("홀수의 합값: " +sum2); //문제4
}
}
* for 무한 반복문..
for( 초기값, 조건, 증가){
문장:
}
class forTest
{
public static void main(String[] args)
{
int i;
for( i = 0; i<13; i++){
i += i; //15 출력..
}
System.out.println(i);
}
}
ChatGPT, 블록체인, 자바, 맥북, 인터넷, 컴퓨터 정보를 공유합니다.
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!