Presentation is loading. Please wait.

Presentation is loading. Please wait.

Byung-Hyun Ha bhha@pusan.ac.kr 프로그래밍 실습 #1 Byung-Hyun Ha bhha@pusan.ac.kr.

Similar presentations


Presentation on theme: "Byung-Hyun Ha bhha@pusan.ac.kr 프로그래밍 실습 #1 Byung-Hyun Ha bhha@pusan.ac.kr."— Presentation transcript:

1 Byung-Hyun Ha bhha@pusan.ac.kr
프로그래밍 실습 #1 Byung-Hyun Ha

2 정수를 사용한 연산: 섭씨를 화씨로 import java.util.Scanner; public class Temp {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Temp. in degree Celsius? "); int c = s.nextInt(); int f = c * 9 / ; System.out.println("It's " + f + " deg. F."); }

3 실수를 사용한 연산: 섭씨를 화씨로 import java.util.Scanner; public class Temp {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Temp. in degree Celsius? "); double c = s.nextDouble(); double f = c * 9 / ; System.out.println("It's " + f + " deg. F."); }

4 실수에 관하여 import java.util.Scanner; public class Big {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); double n = s.nextDouble(); for (int i = 0; i < 10000; i++) { n = n ; } System.out.println("Result: " + n);

5 실수에 관하여 1을 입력해보자 10000000000을 입력해보자 (동그라미가 10개)
어떤 결과 vs. 실제 결과 을 입력해보자 (동그라미가 10개) 을 입력해보자 (동그라미가 15개)

6 문자열 import java.util.Scanner; public class Name {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Name? "); String name = s.nextLine(); int n = name.indexOf(' '); String sir = name.substring(0, n); String given = name.substring(n + 1, name.length()); System.out.print("Sir name is " + sir + " and given name is " + given); }

7 Java 2 Platform API Specification

8 반올림: 잘못된 프로그램 import java.util.Scanner; public class Round {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); double n = s.nextDouble(); double r = (int)(n + 0.5); System.out.println(n + " is rounded off to " + r); }

9 반올림 import java.util.Scanner; public class Round {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); double n = s.nextDouble(); double r = Math.floor(n + 0.5); System.out.println(n + " is rounded off to " + r); }

10 약수1 import java.util.Scanner; public class Divisor {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); int n = s.nextInt(); for (int i = 1; i <= n; i++) { if ((n % i) == 0) { System.out.print(i + " "); }

11 약수2 import java.util.Scanner; public class Divisor {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); int n = s.nextInt(); for (int i = 1; i <= n; i++) { if ((n / i * i) == n) { System.out.print(i + " "); }

12 소수 (Prime Number) import java.util.Scanner; public class Prime {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number? "); int n = s.nextInt(); boolean isPrime = true; for (int i = 2; i < n; i++) { if (n / i * i == n) { isPrime = false; break; } if (isPrime) { System.out.println("Prime Number"); } else { System.out.println("Not Prime Number");

13 숫자 피라미드 출력 Input number (less than 10)? 6 010 01210 0123210 012343210
010 01210

14 숫자 피라미드 출력 import java.util.Scanner; public class Pramid {
public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Input number (less than 10)? "); int n = s.nextInt(); for (int i = 0; i <= n; i++) { for (int j = 0; j < n - i; j++) { System.out.print(' '); } for (int j = 0; j <= i; j++) { System.out.print(j); for (int j = i - 1; j >= 0; j--) { System.out.println();

15 숫자 n개 입력 (999를 입력하면 끝) import java.util.Scanner; public class InputN {
public static void main(String[] args) { Scanner s = new Scanner(System.in); int[] ns = new int[1000]; int count = 0; for (int i = 0; i < 1000; i++) { System.out.print((i + 1) + "'th: "); ns[i] = s.nextInt(); if (ns[i] == 999) { count = i; break; } System.out.print("Your input: "); for (int i = 0; i < count; i++) { System.out.print(ns[i] + " ");

16 정렬 public class Sort { public static void main(String[] args) {
int[] ns = {4, 5, 1, 7, 1, 3}; for (int i = 0; i < ns.length - 1; i++) { for (int j = i + 1; j < ns.length; j++) { if (ns[i] > ns[j]) { int temp = ns[i]; ns[i] = ns[j]; ns[j] = temp; } System.out.println("Result: "); for (int i = 0; i < ns.length; i++) { System.out.print(ns[i] + " ");

17 빈도수 구하기 public class Frequency {
public static void main(String[] args) { int[] ns = {4, 5, 1, 7, 1, 3, 1, 3, 1, 4}; int[] frequency = new int[20]; for (int i = 0; i < 20; i++) { frequency[i] = 0; } for (int i = 0; i < ns.length; i++) { frequency[ns[i]]++; System.out.println(i + ": " + frequency[i]);


Download ppt "Byung-Hyun Ha bhha@pusan.ac.kr 프로그래밍 실습 #1 Byung-Hyun Ha bhha@pusan.ac.kr."

Similar presentations


Ads by Google