본문 바로가기
Example 2018

제어문연습프로젝트 - 다중 if문 : 정수 중 큰수, 학점, 문자구별

by ZEROMI 2018. 4. 9.
728x90

package logic.testif;


import java.util.Scanner;


public class IfElseIfSample {

/* 다중(중첩) if 문 : else 뒤에 다시 if(조건식) 사용

* if(조건식1){

*   조건식1이 참일 때 실행할 내용1

* }else if(조건식2){

*   조건식1이 거짓이고, 조건식2가 참일 때 실행할 내용2

* }else if(조건식3){

*   조건식1,2가 거짓이고, 조건식3이 참일 때 실행내용3

* }else{

*    위의 조건들이 모두 거짓일 때 실행할 내용

* }

*/

public void testThreeMax(){

/* 3개의 정수를 입력받아

* 3수중 가장 큰 수를 알아내어 출력

*/

Scanner sc = new Scanner(System.in);

System.out.print("첫번째 정수 : ");

int first = sc.nextInt();

System.out.print("두번째 정수 : ");

int second = sc.nextInt();

System.out.print("세번째 정수 : ");

int third = sc.nextInt();

int max;  //가장 큰 값 기록할 변수

if(first > second && first > third)

max = first;

else if(second > third)

max = second;

else

max = third;

System.out.println("가장 큰 값 : " + max);

}

/* 키보드로 점수를 입력받아, 정수변수에 저장

   단, 점수는 반드시 0 이상의 값이여야 함.

      다중 if문으로 점수가 90 이상이면 문자변수에 'A' 대입

80 이상 90 미만 'B'

70 이상 80 미만 'C'

60 이상 70 미만 'D'

60 미만 'F' 대입함

      점수와 학점 출력 확인

      점수가 0미만이면 "잘못 입력하셨습니다." 출력

*/

public void testScoreGrade(){

System.out.print("점수 : ");

int score = new Scanner(System.in).nextInt();

char grade;

if(score >= 0){

if(score >= 90) grade = 'A';

else if(score >= 80) grade = 'B';

else if(score >= 70) grade = 'C';

else if(score >= 60) grade = 'D';

else  grade = 'F';

System.out.println(score + " => "

+ grade);

}else{

System.out.println("잘못 입력하셨습니다.");

}

}

/* 문자를 하나 입력받아, 

* 영문대문자이면 "Upper" 라고 출력하고,

* 영문소문자이면 "Lower" 라고 출력하고,

* 숫자문자이면 "Number" 라고 출력하고,

* 그 외의 문자이면 "Others" 라고 출력

*/

public void testCharacter(){

System.out.print("문자 하나 입력 : ");

char ch = new Scanner(System.in).next().charAt(0);

if(ch >= 'A' && ch <= 'Z')

System.out.println("Upper");

else if(ch >= 'a' && ch <= 'z')

System.out.println("Lower");

else if(ch >= '0' && ch <= '9')

System.out.println("Number");

else

System.out.println("Others");

}

/* 두 개의 정수를 입력받아, 두 수를 비교하여

* "A가 B보다 크다."

* "A와 B는 같다."

* "B가 A보다 크다." 중 하나가 출력되게 구현

*/

public void testTwoMaxEqual(){

Scanner sc = new Scanner(System.in);

System.out.print("첫번째 정수 : ");

int first = sc.nextInt();

System.out.print("두번째 정수 : ");

int second = sc.nextInt();

if(first > second)

System.out.println(first + "가 " + 

second + "보다 크다.");

else if(first == second)

System.out.println(first + "와 " + 

second + "는 같다.");

else 

System.out.println(second + "가 " + 

first + "보다 크다.");

}

}







728x90