목록IT (20)
IT 언어 실제
// Day07 생성자 class Car { // 자동차 필드 정의(객체의 속성) - 단순한 틀, 모델링 String color; // 색깔 int speed; // 속도 int gear; // 기어 void print() { // 출력할 내용 System.out.println("(" + color + ", " + speed + ", " + gear + ")"); } } class CarTest { // 자동차 메소드 정의(객체의 동작) public static void main(String[] args) { // 메인 메소드 Car myCar = new Car(); // myCar 자동차 객체 생성 - 실제 객체생성 myCar.color = "red"; // 색깔 myCar.speed = 0; // 속..
// 회원정보 적기 import java.util.Scanner; class Day06 { public static void main(String[] args) { String nick; // 닉네임 String name; // 이름 int age; // 나이 String number; // 전화번호 String address; // 주소 Scanner input = new Scanner(System.in); // 사용자로부터 값을 입력받음 System.out.print("닉네임: "); // 닉네임 출력 nick = input.next(); // 닉네임을 사용자로부터 입력받음 System.out.print("이름: "); // 이름 출력 name = input.next(); // 이름을 사용자로부터 입..
// for문 class Day05_4{ public static void main(String[] args) { for(int i = 2013; i < 2020; i++) { // i의 범위를 정해줌 System.out.println("i의 값은: " + i); //출력 } } } ================================================================ 출력결과 :
// if~else문과 while문 // 사용자에게 값을 받아서, // 2~9단까지의 구구단을 만들어보자 import java.util.*; class Day04_7 { public static void main(String[] args) { int n; // 사용자가 입력할 값 int i = 1; // 구구단은 뒤에 숫자가 1부터 곱해짐 System.out.print("구구단 중에서 출력하고 싶은 단을 고르세요(2~9단): "); Scanner scan = new Scanner(System.in); n = scan.nextInt(); // 사용자가 직접 값을 입력 if(n
// 제어문 : do while 문 class Day04_6 { public static void main(String[] args) { int i = 2013; do { System.out.println("i의 값: " + i); i++; } while (i< 100); // i의 값이 100보다 작아도 한 번은 i의 값이 출력된다 } } ================================================================ 출력결과 :
// 제어문: while 문, do while 문 class Day04_5 { public static void main(String[] args) { int i = 0; while (i < 10) { // i 의 범위를 10보다 작다고 정함 System.out.println("정수: " + i); i++; // i++ 이라고 적어주지 않으면 계속해서 0이 나옴 } } } ================================================================ 출력결과 : ※ 만약에 ' i++' 을 쓰지 않는다면 결과는 어떻게 나올까? 값이 없다고 숫자 0이 계속해서, 끝없이 나온다. 이러한 현상을 '무한루프' 라고 부른다.
// switch 문: switch ~ case class Day04_4 { public static void main(String[] args) { int January = 1; int March = 3; switch(January) { // 값이 1 이므로 case1 출력 case 1: System.out.println("1월"); break; case 2: System.out.println("2월"); break; case 3: System.out.println("3월"); break; default: System.out.println("해당사항 없음!"); break; } switch(March) { // 값이 3 이므로 case3 출력 case 1: System.out.println("1월");..
// if ~ else 문 class Day04_3 { public static void main(String[] args) { int candy = 500; int bread = 1300; // 설정한 사탕값(500)과 비교하는 값(500)과 같으면 "사탕" 출력 if(candy == 500) { System.out.println("사탕"); // 빵이 1300보다 크면 "빵" 출력, 여기선 설정한 빵값(1300)보다 작으므로 출력하지 않음 } if(bread > 1300) { System.out.println("빵"); // 빵을 출력하지 않으면 그 외의 값인 "쿠키"를 출력 } else { System.out.println("쿠키"); } } } =============================..
// if ~ else 문 class Day04_2 { public static void main(String[] args) { int num = 7; if(num % 2 == 0) { // num 을 2로 나누면 홀수이므로, 홀수를 출력 System.out.println("짝수입니다"); } else { System.out.println("홀수입니다"); } } } ================================================================ 출력결과 :
/* if 문: if, if ~ else , 내포된 if switch 문: switch ~ case 제어문: while 문, do while 문 반복문, loop문, for 문, */ // if 문 class Day04 { public static void main(String[] args) { int grade = 80; if(grade >= 90) { // 받은 점수가 90점보다 작으므로 B학점 출력 System.out.println("A학점"); } else { System.out.println("B학점"); } } } ================================================================ 출력결과 :
// 단항연산자(!, ~, ++, --, instanceof) class Day03_5 { public static void main(String[] args) { int a = 10; int b = 3; int c = 7; int d = 5; a = ++b; // 증가 연산자 c = --b; // 감소 연산자 System.out.println("a = ++b의 값: " + a); System.out.println("c = --b의 값: " + b); System.out.println("===================="); System.out.println(d++); // 연산(출력)하고 증가 System.out.println(d); // 본래 d의 값 System.out.println(++d); ..
// 비교연산자(, ==, !=), // 논리연산자(조건 연산자, &&, ||, !) class Day03_4 { public static void main(String[] args) { int a = 10; int b = 5; int c = 10; /* 비교연산자 참이면 true 가 출력됨 거짓이면 false 가 출력됨 */ System.out.println("[비교연산자]"); System.out.println("a = b)); // 10 >= 5 이기 때문에 true System.out.println("a > b: " + (a > b)); // 10 > 5 ..
// 대입연산자(+=, -=, *=, /=, %=, =) class Day03_3 { public static void main(String[] args) { int a = 12; int b = 3; int c = 6; double num1 = 3.0; double num2 = 6.0; double num3 = 6.0; System.out.println("[대입연산자 덧샘, 뺄셈]"); System.out.println("=========================="); System.out.println(a+= b); // a = a + b System.out.println(c-= b); // c = a - b System.out.println("a의 값:" + a); // 15 System.out...
// 사다리꼴의 넓이 구하기 import java.util.Scanner; class Day03_2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int widthTop; // 윗변 int widthLow; // 아랫변 int height; // 높이 System.out.println("\t" + "[사다리꼴 넓이 구하는 공식]"); System.out.println("\t" + "사다리꼴의 가로와 세로의 길이를 입력하세요"); System.out.println("\t" + "=========================================="); System.out.print("\t" + "윗..
// 사각형의 둘레, 넓이 구하기 import java.util.Scanner; class Day03_1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int width; // 가로 int height; // 세로 System.out.println("\t" + "[사각형 둘레와 넓이 구하는 공식]"); System.out.println("\t" + "사각형의 가로와 세로의 길이를 입력하세요"); System.out.println("\t" + "========================================"); System.out.print("\t" + "가로: "); width = input...