#어제거에 과목명 입력추가하기
import java.util.Scanner;
public class Test13 {
public static void main(String[] args) {
//배열
//배열선언1
//과목'명'을 입력하세요.
Scanner sc = new Scanner(System.in);
int num=0;
System.out.println("과목수를 입력하세요.");
num=sc.nextInt();
System.out.println("과목명을 입력하세요.");
String [] subject = new String[num];
// .length 배열의 갯수
for(int i=0 ; i<subject.length ; i++){
System.out.println(i+1+"번째 과목명을 입력하세요.");
subject[i]=sc.next();
}
int [] score = new int[num];
for(int i=0 ; i<score.length ; i++){
System.out.println(subject[i]+"과목의 점수를 입력하세요");
score[i]=sc.nextInt();
}
int sum=0;
for(int i=0 ; i<num ; i++){
sum = sum + score[i];
}
System.out.println("총합계는"+sum);
}
}
##
public class Test14 {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
// int [] ar = new int[3];
int [] ch = {10,20,30};
for(int i=0 ; i<ch.length ; i++){
System.out.println(ch[i]);
}
}
}
## java_0716 프로젝트 생성
#클래스 Student, TestMain, Car , 3개 클래스 생성
#Student 클래스
public class Student {
String name;
int kor, eng, math;
}
#Car 클래스
public class Car {
int wheel;
String brand;
String color;
}
#TestMain 클래스
public class TestMain {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
Student suji = new Student();
Car c = new Car();
System.out.println(suji);
}
}
출력결과
>
java_0716.Student@659e0bfd
>주소값이 나온다.
#
public class TestMain {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
//suji, iu는 참조변수
Student suji = new Student(); //suji의 데이터타입은 Student 타입
suji.name="suji";
Student iu = new Student(); //iu의 데이터타입은 Student 타입
iu.name="iu";
System.out.println(suji);
System.out.println(suji.name);
System.out.println(suji.eng);
//name, eng은 멤버변수
}
}
출력결과
>
java_0716.Student@659e0bfd
iu
0
***suji 는 주소값을 참조한다. 시작위치를 알고 있음
>>> 참조변수!
***Student 를 구성하는 변수 : 멤버변수 (name, kor, eng, mean...)
#######################################################
##
public class TestMain {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
//suji, iu는 참조변수
Student suji = new Student(); //suji의 데이터타입은 Student 타입
suji.name="suji";
Student iu = new Student(); //iu의 데이터타입은 Student 타입
iu.name="iu";
suji = iu ;
iu.name = "choa";
System.out.println(suji.name);
System.out.println(iu.name);
//name, eng은 멤버변수
}
}
출력결과
>
##
##연습
public class TestMain {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
//suji, iu는 참조변수
//학생2명(객체2개) 각 멤버변수 초기화 후 2명 학생에 대한 총점 출력
Student suji = new Student(); //suji의 데이터타입은 Student 타입
suji.name="suji";
suji.kor=70;
suji.eng=80;
suji.math=90;
int sutotal = suji.kor + suji.eng + suji.math;
Student iu = new Student(); //iu의 데이터타입은 Student 타입
iu.name="iu";
iu.kor=55;
iu.eng=65;
iu.math=75;
int iutotal = iu.kor + iu.eng + iu.math;
System.out.println(suji.name+"의 총점:"+sutotal);
System.out.println(iu.name+"의 총점:"+iutotal);
}
}
##Student 클래스안에 메소드 생성
//hap이라는 메소드를 생성
public class Student {
String name;
int kor, eng, math;
//메소드 생성
public void hap(){
//할일을 중괄호 안에 적는다
int sum = kor + eng + math;
System.out.println(name+"의 총점음"+sum+"이다.");
}
}
############################################################
##Student 클래스
public class Student {
String name;
int kor, eng, math;
//메소드 생성
public void hap(){
//할일을 중괄호 안에 적는다
int sum = kor + eng + math;
System.out.println(name+"의 총점은"+sum+"이다.");
}
}
##TeseMain 클래스
public class TestMain {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
// s1, s2는 참조변수
Student s1 = new Student(); //s1의 데이터타입은 Student 타입
s1.name="iu";
s1.kor=55;
s1.eng=65;
s1.math=75;
//메소드 호출
//메소드의 이름()
// hap이라는 메소드 호출
s1.hap();
Student s2 = new Student();
s2.name="suji";
s2.kor=30;
s2.eng=55;
s2.math=75;
s2.hap();
}
}
##Student 클래스에 평균을 출력하는 메소드를 만들자
public class Student {
String name;
int kor, eng, math;
//메소드 생성
public void hap(){
//할일을 중괄호 안에 적는다
int sum = kor + eng + math;
System.out.println(name+"의 총점은"+sum+"이다.");
}
//평균 출력하는 메소드 생성해보자
public void aver(){
double sum = (double)(kor+eng+math);
double mean = sum/3;
System.out.println(name+"의 평균은"+mean+"이다.");
}
//접근지정자 리턴타입 메서드명 매개변수 ,
/*public void aver(){
'중괄호 안은 메소드의 body'
}
*/
}
#평균출력하는 메소드를 TestMain 클래스에서 호출해보자.
public class TestMain {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
//s1, s2는 참조변수
Student s1 = new Student(); //s1의 데이터타입은 Student 타입
s1.name="iu";
s1.kor=55;
s1.eng=65;
s1.math=75;
//메소드 호출
//메소드의 이름()
// hap이라는 메소드 호출
s1.hap();
s1.aver();
Student s2 = new Student();
s2.name="suji";
s2.kor=30;
s2.eng=55;
s2.math=75;
s2.hap();
s2.aver();
}
}
###########################################################3
public class Student {
String name;
int kor, eng, math;
int sum=kor+eng+math; //합을 멤버변수로 선언
int mean=sum/3;
//메소드 생성
public void hap(){
//할일을 중괄호 안에 적는다
sum = kor + eng + math;
double mean = (double)sum/3;
//클래스 안에는 변수, 메소드 2개만 생성
//메소드안에서 반복문, 조건문....
/*if(sum==10){
}
*/
}
//접근지정자 리턴타입 메서드명 매개변수 ,
/*public void aver(){
'중괄호 안은 메소드의 body'
}
*/
}
##
public class TestMain {
public static void main(String[] args) {
// 데이터타입 변수명 = 값
// int a = 10;
//s1, s2는 참조변수
Student s1 = new Student(); //s1의 데이터타입은 Student 타입
s1.name="iu";
s1.kor=55;
s1.eng=65;
s1.math=75;
//메소드 호출
//메소드의 이름()
// hap이라는 메소드 호출
s1.hap();
Student s2 = new Student();
s2.name="suji";
s2.kor=30;
s2.eng=55;
s2.math=75;
s2.hap();
System.out.println(s1.mean);
}
}
>출력결과
0
>왜?
####################################
##Scv 클래스 생성
public class Scv {
int price=50;
int supply=1;
String color;
int hp = 60;
public void work(){
//미네랄캐기
}
public void fix(){
//수리
}
public void build(){
//건설
}
public void attack(){
//공격
}
}
##메인클래스
public class TestMain {
public static void main(String[] args) {
Scv s1 = new Scv();
Scv s2 = new Scv();
Scv s3 = new Scv();
Scv s4 = new Scv();
s1.attack();
s2.work();
s3.build();
s4.fix();
s1.hp=0;
s1=null; //죽음
}
}