5장 조건과 반복 ①.

Slides:



Advertisements
Similar presentations
멘토링 2 주차 장 프로그래밍을 위한 자바의 자료형  값이 변하지 않는 상수  메모리 기억공간인 변수.
Advertisements

5 장 조건과 반복 ②. Contents Counting and Looping [while 문 사용 ] Powers of 2 [while 문 사용 ] More Guessing [do 문 사용 ] Election Day [do 문 사용 ] Finding Maximum &
명품 JAVA Programming 제 3 장 반복문, 배열, 예외처리.
어서와 Java는 처음이지! 제3장선택과 반복.
프로그래밍 개론 Ⅰ 제 3장. 클래스와 객체의 사용 ①.
제 7주 2015년 1학기 강원대학교 컴퓨터학부 담당교수: 정충교
어서와 Java는 처음이지! 제2장 자바 프로그래밍 기초.
컴퓨터 응용 및 실습 Part1. OOP&Java Programming data type Review
Recursion SANGJI University KO Kwangman
IntArray[0] int length 5 intArray 객체 제 3 장 반복문, 배열, 예외처리.
어서와 Java는 처음이지! 제2장 자바 프로그래밍 기초.
7장 배열 ②.
어서와 Java는 처음이지! 제4장 배열.
Java Seminar 6.
Java Presentation 중간 시험2 풀이
제 4장 문 장 배정문 혼합문 제어문 표준 입출력.
제6장 제어(Control) 6.1 구조적 프로그래밍(Structured Programming)
10장 예외 Lab 10-1.
자바란 무엇인가? JDK의 다운로드 및 설치 방법 Hello, Java 프로그램의 작성 자바 프로그램의 작동 원리
윤 홍 란 제3장 클래스와 객체의 사용-1 윤 홍 란
2주 실습강의 Java의 기본문법(1) 인공지능연구실.
Chapter 02 자바 기본구조 자바 프로그래밍의 기초적인 문법을 소개
[INA470] Java Programming Youn-Hee Han
명품 JAVA Essential.
제7장 제어구조 I – 식과 문장.
명품 JAVA Essential.
명품 JAVA Essential.
명품 JAVA Programming 제 4 장 클래스와 객체.
Power Java 제4장 자바 프로그래밍 기초.
7장 배열 ②.
Power Java 제10장 배열.
Choi, Namseok Java 기초 (Java의 제어문과 배열) Choi, Namseok
Power Java 제15장 예외 처리 (Exception Handling).
프로그래밍 개론 Ⅰ 제 3장. 클래스와 객체의 사용 ②.
명품 JAVA Essential.
명품 Java Programming.
10장 다중 스레드 10.1 스레드 개요 10.2 Thread 클래스 10.3 스레드 생성
2장 자바환경과 자바 프로그램 2.1 자바 개발 환경 2.2 자바 통합환경 2.3 자바 응용 프로그램과 애플릿 프로그램
DataScience Lab. 박사과정 김희찬 (월)
5장 조건과 반복 ①.
제2장 데이터 및 수식.
주소록 프로그램.
7장 배열 ①.
제 4주 – 클래스 설계 제 4주 목표 클래스를 구현하는 법을 배운다. 변수 선언 메소드 구현 구성자 객체지향프로그래밍
6장 객체-지향 설계 ①.
제 4주 2014년 1학기 강원대학교 컴퓨터학부 담당교수: 정충교
Chapter6 : JVM과 메모리 6.1 JVM의 구조와 메모리 모델 6.2 프로그램 실행과 메모리 6.3 객체생성과 메모리
23강 API - II - 약방의 감초 Random 클래스 - Scanner 및 Sysout - Wrapper 클래스의 이해
5장 조건과 반복 ②.
제2장 데이터 및 수식.
6장 객체-지향 설계 ①.
어서와 Java는 처음이지! 제4장 배열 IT응용시스템공학과 김형진 교수.
03. 안드로이드를 위한 Java 문법 제목. 03. 안드로이드를 위한 Java 문법 제목.
WAP Java Seminar
Ch.1 Iterator Pattern <<interface>> Aggregate +iterator
제 2장 어휘구조와 자료형 토 큰 리 터 럴 주 석 자 료 형 배 열 형.
[INA470] Java Programming Youn-Hee Han
자바 5.0 프로그래밍.
작성일 참고서적 – Programing Game AI by Example
Java 3장. 자바의 기본 구조 I : 변수, 자료형, 연산자 public class SumTest {
제 4 장 클래스 작성 Lab 4- 2.
JVM의 구조와 메모리 모델 JVM의 내부 구조 클래스 파일 클래스 로더 메소드(method) 영역 힙(heap) 영역
컴퓨터 프로그래밍: 실습 1 제 1장 . 서론.
Chapter 4 클래스 작성.
Java 5장. 객체지향 개념 public class SumTest {
경영학의 상황학파에 대해서… 경제학과 3학년 최준용 회계학과 4학년 진현빈
실습과제 1번 /* 1. 멤버 변수로 반경 radius를 갖고, 그 값을 모니터에 출력하는
Choi Younghwan CSE HUFS
Chapter8 : 인터페이스와 패키지 8.1 인터페이스 개요와 인터페이스 정의 8.2 인터페이스의 사용
자바 암호 프로그래밍 Java Cryptography Programming
Presentation transcript:

5장 조건과 반복 ①

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

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

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); }

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%)

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)); }

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

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 -> 반드시 $38.00을 전체 다 지불, 새 잔액이 $128 -> $50를 지불, 새 잔액이 $350 -> $70(20% of 350)를 지불 해야 한다.] New balance for a new balance less than $50 $50.00 for a new balance between $50 and $300 (inclusive) 20% of the new balance for a enw balance over $300

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

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

가위, 바위, 보 게임[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 //... 나머지 코드 부분을 채워넣으세요.

가위, 바위, 보 게임[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를 사용하여 컴퓨터가 랜덤 하게 값을 받아야 한다.