1. 두 숫자를 입력 받아 더한 값을 출력하는 프로그램 만들기
package ex02; import java.util.Scanner; // 입력하지 않고 7번째 줄을 입력하면 자동으로 됨. public class Add2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // System.in = 키보드 int x, y, sum; System.out.print("첫 번째 숫자를 입력하시오: "); x = sc.nextInt(); System.out.print("두 번째 숫자를 입력하시오: "); y = sc.nextInt(); sum = x + y; System.out.println(sum); } }
결과

2. 이름과 나이를 입력받아 화면에 출력하는 프로그램 만들기
package ex02; import java.util.Scanner; public class InputString { public static void main(String[] args) { String name; int age; Scanner sc = new Scanner(System.in); System.out.print("이름을 입력하시오: "); name = sc.nextLine(); System.out.print("나이를 입력하시오: "); age = sc.nextInt(); System.out.println(name + "님 안녕하세요! " + (age) + "살이시네요."); } }
결과

3. 관계 연산자를 이용 실습해보기
package ex02; public class CompOperator { public static void main(String[] args) { System.out.print((3 == 4) + " "); System.out.print((3 != 4) + " "); System.out.print((3 > 4) + " "); System.out.print((4 > 3) + " "); System.out.print((3 == 3 && 4 == 7) + " "); // && 둘다 참이면 참 System.out.print((3 == 3 || 4 == 7) + " "); // || 둘중 하나만 참이면 참 } }
결과

4. 조건 연산자를 이용하여 반지름이 20cm인 피자 2개와 30cm인 피자 1개의 면적을 비교하고, 어느 것이 더 유리한지 알려주는 프로그램 만들어 보기
package ex02; public class Pizza { public static void main(String[] args) { double area1 = 2 * 3.141592 * 20 * 20; double area2 = 3.141592 * 30 * 30; System.out.println("20cm 피자 면적: " + area1); System.out.println("30cm 피자 면적: " + area2); System.out.println((area1 > area2) ? "20cm 두 개를 주문하세요" : "30cm 한 개를 주문하세요"); } }
결과

5. 화씨 → 섭씨 변환 프로그램과 섭씨 → 화씨 변환 프로그램을 만들고, 1번을 누르면 화씨 → 섭씨, 2번을 누르면 섭씨 → 화씨로 변환 시켜 알려주는 프로그램을 만들어 보기
화씨 → 섭씨
package ex02; import java.util.Scanner; public class MiniProject01 { public static void main(String[] args) { double c; Scanner sc = new Scanner(System.in); System.out.print("화씨 온도를 입력하시오: "); double f = sc.nextDouble(); c =(double) 5/9*(f-32) ; // (double)로 형변환을 하지 않으면 5/9 = 0 System.out.println("섭씨 온도는" + c); } }
결과

섭씨 → 화씨
package ex02; import java.util.Scanner; public class MiniProject02 { public static void main(String[] args) { System.out.print("섭씨 온도를 입력하세요: "); Scanner sc = new Scanner(System.in); double c = sc.nextDouble(); double f = (double) 9/5 * c + 32; System.out.println("화씨 온도는 " + f); } }
결과

최종(if else 문 활용)
package ex02; import java.util.Scanner; public class MiniProject03 { public static void main(String[] args) { System.out.println("=============="); System.out.println("1. 화씨 -> 섭씨"); System.out.println("2. 섭씨 -> 화씨"); System.out.println("=============="); System.out.print("번호를 입력하세요: "); Scanner sc = new Scanner(System.in); int a = sc.nextInt(); if(a == 1) { System.out.print("화씨 온도를 입력하시오: "); Scanner sc1 = new Scanner(System.in); double f1 = sc1.nextDouble(); double c1 = 5.0 / 9.0 * (f1 - 32); // double 형변환 보다 이게 더 변함 System.out.println("섭씨 온도는" + c1); }else if(a == 2){ System.out.print("섭씨 온도를 입력하세요: "); Scanner sc2 = new Scanner(System.in); double c2 = sc2.nextDouble(); double f2 = 9.0 / 5.0 * c2 + 32; System.out.println("화씨 온도는 " + f2); }else{ System.out.println("번호를 잘못 입력하셨습니다."); } } }
최종(삼항 연산자 활용)
package ex02; import java.util.Scanner; public class FtoC3 { public static void main(String[] args) { System.out.println("================"); System.out.println("1. 화씨 -> 섭씨"); System.out.println("2. 섭씨 -> 화씨"); System.out.println("================"); System.out.println(); System.out.print("번호를 입력하시오: "); Scanner sc = new Scanner(System.in); int selectedNum = sc.nextInt(); System.out.print((selectedNum == 1) ? "화씨 온도를 입력하시오: " : "섭씨 온도를 입력하시오:"); double temp = sc.nextDouble(); double result = (selectedNum == 1) ? 5.0/9.0*(temp-32) : 9.0/5.0*temp+32; System.out.print((selectedNum == 1) ? "화씨 온도는 " + result : "섭씨 온도는 " + result); } }
결과

6. 하나의 상자에 오렌지를 10개씩 담을 수 있다고 하자. 오렌지가 127개가 있다면 상자 몇 개가 필요한가? 또 몇 개의 오렌지가 남을까?
package ex02.Programming; import java.util.Scanner; public class OrangeBox { public static void main(String[] args) { Scanner sc =new Scanner(System.in); System.out.print("오렌지의 개수를 입력하시오: "); int orange = sc.nextInt(); int resultOrange = orange%10; int resultBox = orange/10; System.out.println(resultBox+"박스가 필요하고 "+resultOrange+ "개가 남습니다."); } }
결과

7. 마일을 킬로미터로 변환하는 프로그램을 작성하라. 1마일은 1.609킬로미터와 같다. 사용자로부터 마일의 값을 읽어 들인다.
package ex02.Programming; import java.util.Scanner; public class MiletoKm { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 1. 마일의 값을 받는다. System.out.print("마일을 입력하시오: "); double mile = sc.nextDouble(); // 2. 연산하여 출력한다. double km = mile * 1.609; System.out.println(mile + "마일은 " + km + "킬로미터 입니다."); } }
결과

8. 사용자로부터 두 개의 정수를 받아서 정수의 합, 정수의 차, 정수의 곱, 정수의 평균, 큰 수, 작은 수를 계산하여 화면에 출력하는 프로그램을 작성하라. 큰수와 작은 수를 구할 때는 조건 연산자를 사용해보자.
package ex02.Programming; import java.util.Scanner; public class CalculateInt { public static void main(String[] args) { Scanner sc = new Scanner(System.in); // 1. 두 정수를 입력 받는다. System.out.print("x: "); int x = sc.nextInt(); System.out.print("y: "); int y = sc.nextInt(); // 2. 입력 받은 정수를 계산한다. System.out.println("두 수의 합:"+ (x+y)); System.out.println("두 수의 차:"+ (x-y)); System.out.println("두 수의 곱:"+ (x*y)); System.out.println("두 수의 평균:"+ (x+y)/2.0); int max = (x>y) ? x:y; System.out.println("큰 수: " + max); int min = (x<y) ? x:y; System.out.println("작은 수: " + min); } }
결과

Share article