오늘은 지난 시간에 이어서 반복문에 대해 글을 써보려한다.
import java.util.Scanner;
/* 연습문제
국어점수, 영어점수, 수학 점수를 입력받아 총점, 평균, 합격 여부를 구하는 프로그램을 작성해보자
(단, 학점은 평균기준이며 90점이상 A학점, 80점 이상 B학점, 70점 이상 C학점, 60점 이상 D학점, 나머지는 F학점)
결과)
국어점수를 입력하세요.
영어점수를 입력하세요.
수학점수를 입력하세요.
국어 점수 : 90
영어 점수 : 80
수학 점수 : 70점
총점 : 240점
평균 : 80점
학점 : B학점
*/
public class Switch2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("국어점수를 입력하세요.");
int kor = sc.nextInt();
System.out.println("영어점수를 입력하세요.");
int eng = sc.nextInt();
System.out.println("수학점수를 입력하세요.");
int math = sc.nextInt();
int total = kor + eng + math;
int avg = total / 3;
System.out.println("국어점수 : " + kor + "점");
System.out.println("영어점수 : " + eng+ "점");
System.out.println("수학점수 : " + math+ "점");
System.out.println("총점 : "+ total+ "점");
System.out.println("평균 : " + avg+ "점");
// 90 / 10 -> 10 9 8 7 6 5 4 3 2 1
switch (avg / 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학점");
}
}
}
while(조건식){
조건식이 true인 동안 반복할 문장;
...
}
import java.util.Scanner;
public class While4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("원하는 단을 입력하세요.");
int dan = sc.nextInt();
int i = 1;
System.out.println(dan + " 단");
while (i <=9){
// 5 * 1 = 5
System.out.println(dan + " * " + i + " = " + (dan * i));
i++;
}
}
}
do{
조건식이 true인 동안 반복할 문장;
...
}while(조건식);
for(변수의 초기화; 조건식; 증감식){
조건식이 true인 동안 반복할 문장;
...
}
public class Continue1 {
public static void main(String[] args) {
for(int i = 1; i <= 100; i++){
if(i % 3 == 0){
System.out.print("짝! ");
continue;
}
System.out.print(i + " ");
}
}
}
import java.util.Scanner;
public class Break1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("음식 자판기");
while(true){
System.out.println("<< 원하는 메뉴를 선택 >>");
System.out.println("1. 라면, 2. 치킨, 3. 초밥, 4. 파스타, 5. 닭가슴살, 6.프로그램 종료");
int sel = sc.nextInt();
switch (sel){
case 1:
System.out.println("라면이 나왔습니다.");
break;
case 2:
System.out.println("치킨이 나왔습니다.");
break;
case 3:
System.out.println("초밥이 나왔습니다.");
break;
case 4:
System.out.println("파스타가 나왔습니다.");
break;
case 5:
System.out.println("닭가슴살이 나왔습니다.");
break;
case 6:
System.out.println("주문을 종료합니다.");
}
if(sel == 6) break;
}
}
}
for(int i =1; i ≤ 5; i++){
....
for(int j = 1; j ≤ 3; j++){
....
}
}
Class Loader
Execution Engine
Garbage Collector
stack
- 지역변수, 매개변수, 리턴 값, 연산에 사용되는 임시 값 등이 저장되는 영역
Heap
- new 키워드로 생성된 객체와 배열이 저장되는 영역
- 생성된 객체의 배열은 JVM 스택 영억의 변수나 다른 객체의 필드에서 참조
Method
- 클래스 멤버 변수의 이름, 데이터 타입, 접근 제어자 정보, 필드 정보, 메소드 이름, static 변수, final class 변수..영역
PC Register
Native methodd stack
import java.util.Scanner;
/*
20221년 05월 25일
가위바위보 게임 만들기
*/
public class Homework1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("<<<가위 바위 보 게임하기 >>");
while(true){
System.out.println("가위, 바위, 보 중 하나를 선택하세요.");
System.out.print("컴퓨터 : ");
String x = sc.next();
System.out.print("유저 : ");
String y = sc.next();
if(y.equals("가위")){ // 유저 : 가위
if(x.equals("가위")){
System.out.println("비겼습니다. 다시하세요.");
continue;
} else if(x.equals("바위")){
System.out.println("졌습니다.");
continue;
} else if(x.equals("보")){
System.out.println("이겼습니다. 게임을 종료합니다.");
break;
}
} else if(y.equals("바위")){ // 유저 : 바위
if(x.equals("보")){
System.out.println("졌습니다.");
continue;
} else if(x.equals("바위")) {
System.out.println("비겼습니다.");
continue;
} else if(x.equals("가위")){
System.out.println("이겼습니다. 게임을 종료합니다.");
break;
}
} else if(y.equals("보")){ // 유저 : 보
if(x.equals("가위")){
System.out.println("졌습니다.");
continue;
} else if(x.equals("보")) {
System.out.println("비겼습니다.");
continue;
} else if(x.equals("바위")){
System.out.println("이겼습니다. 게임을 종료합니다.");
break;
}
}
break;
}
}
}