728x90
SMALL
변수, static변수, 배열, 클래스멤버필드, 클래스배열
static은 메모리가 적어서 최대한 안쓰는게 좋음
int[] arr = new int[2];
arr는 참조변수
스택에서 담는게아니라 힙에서 담음
<for문 다시 복습>
- 합구하기
public class Ex01 {
public static void main(String[] args) {
int sum = 0;
for (int i = 0; i < 101; i++) {
sum+= i;
if (i%10 ==0) {
System.out.println(1 + "~" + i + "합 : " + sum);
}
}
}
}
1~0합 : 0
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 Ex02 {
public static void main(String[] args) {
int mul = 1;
for (int i = 1; i < 10; i++) {
for (int j = 2; j < 10; j++) {
mul = j*i;
System.out.print(j +"*" + i + "=" + mul+ "\t");
if (j==9) {
System.out.println(" ");
}
}
}
}
}
2*1=2 3*1=3 4*1=4 5*1=5 6*1=6 7*1=7 8*1=8 9*1=9
2*2=4 3*2=6 4*2=8 5*2=10 6*2=12 7*2=14 8*2=16 9*2=18
2*3=6 3*3=9 4*3=12 5*3=15 6*3=18 7*3=21 8*3=24 9*3=27
2*4=8 3*4=12 4*4=16 5*4=20 6*4=24 7*4=28 8*4=32 9*4=36
2*5=10 3*5=15 4*5=20 5*5=25 6*5=30 7*5=35 8*5=40 9*5=45
2*6=12 3*6=18 4*6=24 5*6=30 6*6=36 7*6=42 8*6=48 9*6=54
2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49 8*7=56 9*7=63
2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64 9*8=72
2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
출력결과 아주이쁨
안움직이는게 바깥, 움직이는게 안에
public class Ex02 {
public static void main(String[] args) {
for (int i = 1; i < 10; i++) {
for (int j = 2; j < 10; j++) {
System.out.print(j +"*" + i + "=" + (j*i)+ "\t");
}
System.out.println("");
}
}
}
이건 쌤이한거 변수하나도안쓰고 for문 두개밖에안썼네...
- 별찍기
public class Ex03 {
public static void main(String[] args) {
for (int i = 5; i > 0; i--) {
if (i==5) {
System.out.print("*****");
}else if (i==4) {
System.out.print("****");
}else if (i==3) {
System.out.print("***");
}else if (i==2) {
System.out.print("**");
}else if (i==1) {
System.out.print("*");
}
System.out.println(" ");
}
}
}
넘나 주먹구구식
public class Ex03 {
public static void main(String[] args) {
int line = 5;
for (int i = line-1; i > -1; i--) {
for (int j = 0; j < i+1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
다른 결과
아니 이런생각 어케하는겨 ?ㅡㅡ
public class Ex04 {
static void name_func(String name) {
System.out.println(name);
}
public static void main(String[] args) {
String name = "이한나";
name_func(name);
}
}
- 성적입력프로그램 (swtich문)
import java.util.Scanner; //switch문
public class Ex05 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("성적을 입력하세요 : ");
int score = in.nextInt();
switch (score/10) {
case 10:
case 9:
System.out.println("A학점");
break;
case 8:
System.out.println("B학점");
break;
case 7:
System.out.println("C학점");
break;
case 6:
System.out.println("D학점");
break;
default:
System.out.println("F학점");
break;
}
}
}
- 성적입력프로그램 (if else문)
import java.util.Scanner; //if else문
public class Ex06 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("성적을 입력하세요 : ");
int score = in.nextInt();
if (score/10 == 9 || score/10 ==10) {
System.out.println("A학점");
} else if (score/10 ==8) {
System.out.println("B학점");
} else if (score/10 ==7) {
System.out.println("C학점");
}else if (score/10 ==6) {
System.out.println("D학점");
}else {
System.out.println("F학점");
}
}
}
728x90
LIST
'JAVA > Java' 카테고리의 다른 글
20201007_12 Review 교과목어쩌구 프로그램 (0) | 2020.10.07 |
---|---|
20201007_12 교과목, 학생추가 프로그램 (0) | 2020.10.07 |
20201006_11 Review 출석부 프로그램 (0) | 2020.10.06 |
20201006_11 출석부프로그램, 설계 (0) | 2020.10.06 |
20201006_11(생성자, this, 가비지) (0) | 2020.10.06 |
댓글