*****
*****
*****
*****
*****
public class Star01 {
public static void main(String[] args) {
for(int i=1; i<=5; i++) {
for(int j =1; j<=5; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
*
**
***
****
*****
public class Star02 {
public static void main(String[] args) {
for(int i=1; i<=5; i++) {
for(int j =1; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
*
**
***
****
*****
public class Star03 {
public static void main(String[] args) {
for(int i=1; i<=5; i++) {
for(int j =5; j>i; j--) {
System.out.print(" ");
}
for(int j=1; j<=i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
char ch = '가';
System.out.println(ch + '\n')
ch 는 char타입, \n 은 String타입이기 때문에 ch가 String 타입으로 변환된 후에 연산이 되어 문자열로 출력되는 것이다.
'가' 의 유니코드 값과 '\n'의 유니코드 값이 int형식으로 연산되어 int 값이 출력된다.
의도를 표현하기 위해서는 '\n'을 "\n" 로 바꿔줘야한다.
Scope: 영역, 범위
변수는 변수가 선언된 시점부터 그 중괄호 블럭 안에서 사용 가능하다.
클래스 밖에서 변수 선언은 불가능함
같은 영역 내에서 동일 이름의 변수 선언 불가
함수 밖, 클래스 안에 선언된 변수(범위는 클래스 전체)
함수 안에서 선언된 변수(범위는 선언 된 함수 내)
변수와 함수
(1) 클래스명.java 코딩 상에서의 클래스
(2) 클래스명.class (컴파일 된 파일)
원의 넓이는 구하는 프로그램을 아래와 같이 작성하시오.
-원클래스를 만들것
-메인 메소드를 가진 다른 클래스에서 원 객체를 생성할것
public class Circle {
double radius;
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
public double circleArea(double radius) {
return radius * radius * Math.PI;
}
}
public class CircleMain {
public static void main(String[] args) {
Circle circle = new Circle();
circle.setRadius(3.5);
double area;
area = circle.circleArea();
System.out.println("원의 넓이는: "+ area);
}
}
객체(instance)는 '클래스'라는 틀을 통해 만들어낸 실체를 말한다.
객체를 생성한다는 것은 해당 클래스의 .class 파일을 메모리에 올린다는 것을 의미한다.
1부터 10까지의 합 : 55
1부터 20까지의 합 : 210
1부터 30까지의 합 : 465
1부터 40까지의 합 : 820
1부터 50까지의 합 : 1275
1부터 60까지의 합 : 1830
1부터 70까지의 합 : 2485
1부터 80까지의 합 : 3240
1부터 90까지의 합 : 4095
1부터 100까지의 합 : 5050
public class Sum {
public static void main(String[] args) {
for(int n = 10; n <=100; n++) {
if (n % 10 == 0) {
getHap(n);
}
}
}
public static void getHap(int num1) {
int sum = 0;
for (int i = 1; i <= num1; i++) {
sum += i;
}
System.out.println("1부터 "+ num1 + "까지의 합 : " + sum);
}
}
1+2+3+4+5+6+7+8+9+10 = 55
public class getHapPrint {
public static void main (String[] args) {
getHapPrint(10);
}
public static void getHapPrint (int num1) {
int sum = 0;
for (int i = 1; i <= num1; i++) {
if(i != num1) {
System.out.print(i + "+");
} else {
System.out.print(i + "=");
}
sum += i;
}
System.out.println(sum);
}
}
123456789
12345678
1234567
123456
12345
1234
123
12
1
public class Star {
public static void main(String[] args) {
for(int i=9; i>0; i--){
for(int j=1; j<=i; j++){
System.out.print(j);
}
System.out.println();
}
}
*********(7)
*****(5)
***(3)
*(1)
public class Star {
public static void main(String[] args) {
for(int i = 7; i > 0; i--) {
if (i % 2 == 1) {
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.print("(" + i + ")");
System.out.println();
}
}
}
}