*****
1****
11***
111**
1111*
public class TestProjet10 {
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < i; j++) {
System.out.print("1");
}
for (int k = i; k < 5; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
char ch = '가';
System.out.println(ch + '\n')
위 코드에서 ch + '\n'은 문자 '가'에 개행 문자('\n')를 더한 것입니다. 이때, 문자와 개행 문자는 각각 정수 값으로 표현되며 더해집니다. '가'의 Unicode 값은 44032이고, 개행 문자('\n')의 Unicode 값은 10입니다. 그래서 결과적으로 44032 + 10이 계산되어 44042가 출력됩니다.
리턴타입 메소드이름(매개변수) {
// 메소드의 기능 구현
}
1)
starPrint2(5)
public class TestProjet8888 {
public static void starPrint2(int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
for (int k = 0; k < n - i; k++) {
System.out.print("*");
}
System.out.println();
}
}
public static void main(String[] args) {
starPrint2(5);
}
}
출력
*****
****
***
**
*
char grade;
double avg = 80;
grade = getGrade(avg);
// 우 입니다.
System.out.println(grade + " 입니다.");
public class TestProjet8888 {
public static char getGrade(double avg) {
if (avg >= 90) {
return '수';
} else if (avg >= 80) {
return '우';
} else {
return '미';
}
}
public static void main(String[] args) {
char grade;
double avg = 80;
grade = getGrade(avg);
System.out.println(grade + " 입니다.");
}
}
int sum = getHap(1,100);
//5050
System.out.println(sum);
public class TestProjet8888 {
public static int getHap(int num1, int num2) {
int sum = 0;
for (int i = num1; i <= num2; i++) {
sum += i;
}
return sum;
}
public static void main(String[] args) {
int sum = getHap(1, 100);
System.out.println(sum);
}
}
int count = get57(1,100);
//count 는 1부터 100 까지의 숫자중 5의 배수 이자 7의 배수인 수의 갯수
System.out.println(count );
public class TestProjet8888 {
public static int get57(int num1, int num2) {
int count = 0;
for (int i = num1; i <= num2; i++) {
if (i % 5 == 0 && i % 7 == 0) {
count++;
}
}
return count;
}
public static void main(String[] args) {
int count = get57(1, 100);
System.out.println(count);
}
}
printGuGudan(3)// 3단 출력
printGuGudan(4)// 4단 출력
public class TestProjet8888 {
public static void printGuGudan(int dan) {
for (int i = 1; i <= 9; i++) {
System.out.println(dan + " * " + i + " = " + (dan * i));
}
}
public static void main(String[] args) {
printGuGudan(3);
printGuGudan(4);
}
}
getRecArea(3,5) //사각형 넓이
getRecCirlce(5) //원넓이
getTriangleArea(4 , 5) //삼각형 넓이
public class TestProjet8888 {
public static double getRecArea(double width, double height) {
return width * height;
}
public static double getRecCircle(double radius) {
return Math.PI * radius * radius;
}
public static double getTriangleArea(double base, double height) {
return 0.5 * base * height;
}
public static void main(String[] args) {
double recArea = getRecArea(3, 5);
double circleArea = getRecCircle(5);
double triangleArea = getTriangleArea(4, 5);
System.out.println("사각형 넓이: " + recArea);
System.out.println("원 넓이: " + circleArea);
System.out.println("삼각형 넓이: " + triangleArea);
}
}
같은 작업을(중복) 하지 않기 위해
기능이 2번 이상 중복되면 반드시 함수로 만들것.
int month = 4;
getSeason(3)// 봄입니다. 출력
public class TestProjet8888 {
public static void getSeason(int month) {
if (month >= 3 && month <= 5) {
System.out.println("봄입니다.");
} else if (month >= 6 && month <= 8) {
System.out.println("여름입니다.");
} else if (month >= 9 && month <= 11) {
System.out.println("가을입니다.");
} else {
System.out.println("겨울입니다.");
}
}
public static void main(String[] args) {
int month = 4;
getSeason(month);
}
}
getSeason(12)// 겨울 입니다. 출력
public class TestProjet8888 {
public static void getSeason(int month) {
if (month >= 3 && month <= 5) {
System.out.println("봄입니다.");
} else if (month >= 6 && month <= 8) {
System.out.println("여름입니다.");
} else if (month >= 9 && month <= 11) {
System.out.println("가을입니다.");
} else {
System.out.println("겨울입니다.");
}
}
public static void main(String[] args) {
int month = 12;
getSeason(month);
}
}