JAVA
190626 콘솔 창으로 숫자 입력 받은 후 if문
猫猫
2019. 6. 26. 10:34
반응형
package prj190626;
import java.util.*;
public class If_2 {
public static void main(String[] args) {
int a =0;
int math = 0;
int eng = 0;
int kor = 0;
int total = 0;
float avg = 0.0f;
System.out.println("원하는 숫자를 입력하세요");
System.out.println("1. 총점");
System.out.println("2. 평균");
Scanner scan = new Scanner(System.in);
String tmp = scan.nextLine();
a = Integer.parseInt(tmp);
if(a==1) {
System.out.println("수학 점수를 입력하세요");
String tmpMath = scan.nextLine();
math = Integer.parseInt(tmpMath);
System.out.println("수학점수는 " + math + "점 입니다");
System.out.println("영어 점수를 입력하세요");
String tmpEng = scan.nextLine();
eng = Integer.parseInt(tmpEng);
System.out.println("영어점수는 "+eng+"점 입니다");
System.out.println("국어 점수를 입력하세요");
String tmpKor = scan.nextLine();
kor = Integer.parseInt(tmpKor);
System.out.println("국어점수는 "+kor+"점 입니다");
total = math+eng+kor;
System.out.println("총점은 " + total + "점 입니다");
}
if(a==2) {
System.out.println("수학 점수를 입력하세요");
String tmpMath = scan.nextLine();
math = Integer.parseInt(tmpMath);
System.out.println("수학점수는 " + math + "점 입니다");
System.out.println("영어 점수를 입력하세요");
String tmpEng = scan.nextLine();
eng = Integer.parseInt(tmpEng);
System.out.println("영어점수는 "+eng+"점 입니다");
System.out.println("국어 점수를 입력하세요");
String tmpKor = scan.nextLine();
kor = Integer.parseInt(tmpKor);
System.out.println("국어점수는 "+kor+"점 입니다");
total = math+eng+kor;
avg = (float)total/3; //total 먼저 float으로 강제 캐스팅 후 3으로 나누는것이 더 정확 한 값 출력
System.out.println("총점은 " + total + "점이고" + " 평균은 " + (int)avg + "점 입니다");
}
}
}
c++과 달리 JAVA는 new 로 할당하는 동적할당 delete를 해주지 않아도 된다 라고 하셨다.
근데 이건 의견 분분 한듯
가비지 콜렉터로 자동으로 메모리 정리 해준다고 하지만 구문 길어질땐 null값 주는게 좋지 않을까
반응형