Presentation is loading. Please wait.

Presentation is loading. Please wait.

5장 조건과 반복 ①.

Similar presentations


Presentation on theme: "5장 조건과 반복 ①."— Presentation transcript:

1 5장 조건과 반복 ①

2 Contents boolean expression If 문 실습예제 1 If 문 실습예제 2 If 문 실습예제 3
일정관리 [Cascading if] 가위,바위,보 게임[Switch문]

3 5.1 boolean expression[부울 식]
아래 조건문을 Java syntax에 맞게 표현하시오. 1. x > y > z 2. x와 y는 모두 0보다 작다. 3. x와 y 둘 다 0보다 작지 않다. 4. x는 y와 같고 z와는 같지 않다.

4 If문 실습예제1 Salary. java // *************************************************************** // Salary.java // 직원의 raise와 new salary를 계산한다. import java.util.Scanner; public class Salary { public static void main (String[] args) double currentSalary; // 현재 연봉 double rating; // 평가 등급(1 = excellent, 2 = good, 3 = poor) double raise; // dollar amount of the raise Scanner scan = new Scanner(System.in); // Get the current salary and performance rating System.out.print ("Enter the current salary: "); currentSalary = scan.nextDouble(); System.out.print ("Enter the performance rating: "); rating = scan.nextDouble(); // if … else… 문을 이용하여raise를 계산 하시오 // Print the results System.out.println ("Amount of your raise: $" + raise); System.out.println ("Your new salary: $" + currentSalary + raise); }

5 If문 실습예제1 (Cont.) 주석부분에 If문을 추가하여 “Salary. java”에서 직원의 new salary 와raise를 구하는 부분을 완성하시오. 조건: 직원의 Current annual salary와 그룹별 rating을 input으로 받아 해당 rating에 따라 상응한 raise를 계산해준다. 1=excellent (raise=6%), 2=good (raise=4%), 3=poor (raise=1.5%)

6 If문 실습예제 2 Salary1. java // *************************************************************** // Salary1.java // 직원의 raise와 new salary를 계산한다. 현재 salary 와 performance // rating은 다음과 같다[a String: “Excellent”, “Good”, “Poor”을 input으로 // 받는다.] // // Computes the amount of a raise and the new // salary for an employee. The current salary // and a performance rating (a String: "Excellent", // "Good" or "Poor") are input. import java.util.Scanner; import java.text.NumberFormat; public class Salary1 { public static void main (String[] args) double currentSalary; // 직원의 현재 salary double raise; // amount of the raise double newSalary; // new salary for the employee String rating; // performance rating Scanner scan = new Scanner(System.in); System.out.print ("Enter the current salary: "); currentSalary = scan.nextDouble(); System.out.print ("Enter the performance rating (Excellent, Good, or Poor): "); rating = scan.nextLine(); // raise 를 if ...문을 이용하여 계산하시오 newSalary = currentSalary + raise; // Print the results NumberFormat money = NumberFormat. getCurrencyInstance(); System.out.println(); System.out.println("Current Salary: " + money.format(currentSalary)); System.out.println("Amount of your raise: " + money.format(raise)); System.out.println("Your new salary: " + money.format(newSalary)); }

7 If문 실습예제 2(Cont.) 조건: 주의 할 점:
“Salary1.java”에서 current salary, rating을 입력 값으로 받아서 new salary를 구하는 프로그램을 작성하시오. 주의 할 점: Rating을 string으로 받아야 한다. [“Excellent”, “Good”, “Poor”]

8 If문 실습예제 3 신용카드회사의 외상거래 명세서를 작성하는 프로그램을 완성하시오. 조건:
1.계좌내의 잔액[balance]과 전체 추가 비용을 입력 받는다. 2. 매달의 interest, 새로운 잔액[total new balance], 최소 지출 금액[minimum payment due] 를 계산해야 한다. 새로운 잔액 = 예전 잔액 + 전체 추가 비용 + interest Interest를 이렇게 가정한다. 즉, 예전 잔액이 0이면 interest도 0, 예전 잔액이 0보다 크면 interest는 total owd [예전 잔액 + 추가 비용]의 2%이다. minimum payment due는 다음과 같이 가정한다. [예를 들면, 새 잔액이$ > 반드시 $38.00을 전체 다 지불, 새 잔액이 $128 -> $50를 지불, 새 잔액이 $350 -> $70(20% of 350)를 지불 해야 한다.] New balance for a new balance less than $50 $ for a new balance between $50 and $300 (inclusive) 20% of the new balance for a enw balance over $300

9 If문 실습예제 3 (Cont.) 3. NumberFormat class 를 이용하여 아래 그림과 같이 출력하시오.
CS CARD International Statement ========================== Previous Balance: $ Additional Charges: $ Interest: $ New Balance: $ Minimum Payment: $

10 Cascading if 날씨변화에 따른 나의 활동목록 만들기
2. 아래와 같이 프로그램을 수정하시오. 조건: Boolean operator를 사용하여 temp>=95, or temp<20일 때, “Visit our shops!”를 출력하고 기타 날씨일 경우, 위와 같은 활동을 한다고 출력 날씨 활동 temp >= 80 swimming 60<= temp <80 tennis 40 <= temp <60 golf Temp <40 skiing

11 가위, 바위, 보 게임[switch문] // **************************************************************** // Rock. java // // Play Rock, Paper, Scissors with the user import java.util.Scanner; import java.util.Random; public class Rock { public static void main(String[] args) String personPlay; //User's play -- "R", "P", or "S" String computerPlay; //Computer's play -- "R", "P", or "S" int computerInt; // 사용자 play를 랜덤하게 발생시킨다. Scanner scan = new Scanner(System.in); Random generator = new Random(); //Get player's play – string으로 저장 // 비교하기 쉽게 하기 위해 Make player's play uppercase 로 한다. //Generate computer's play (0,1,2) //Translate computer's randomly generated play to string switch (computerInt) { } // computer's play를 출력한다. // nested ifs instead of &&를 사용하여 누가 이겼는지 //확인한다. if (personPlay.equals(computerPlay)) System.out.println("It's a tie!"); else if (personPlay.equals("R")) if (computerPlay.equals("S")) System.out.println("Rock crushes scissors. You win!!"); else //... 나머지 코드 부분을 채워넣으세요.

12 가위, 바위, 보 게임[Cont.] “Rock. java”에서 주석으로 처리되어 있는 부분의 프로그램을 완성하여 아래와 같은 게임을 만드시오. $ java Rock Enter your play: R, P, or S r Computer play is S Rock crushes scissors, you win! 주의 할 점: 사용자한테서 게임 시작 명령을 받는다. 대소문자 구별 없이 r, p, s사용해야 한다. Switch statement를 사용하여 컴퓨터가 랜덤 하게 값을 받아야 한다.


Download ppt "5장 조건과 반복 ①."

Similar presentations


Ads by Google