프로그래밍

 3198, 1/160 회원가입  로그인  
   zenojm
   자바 질문이요ㅜㅜ

http://www.hackerschool.org/HS_Boards/zboard.php?AllArticle=true&no=6483 [복사]


public class Box01 {
int boxId, amount;
String width, length, height;
Box01(int boxId, String width, String length, String height){
this.boxId = boxId;
this.width = width;
this.length = length;
this.height = height;
}
void addbox(int amount){
boxId+=amount;
}
}

------------------------------------------------------
public class Box01Test {
public static void main(String args[]){
Box01 obj1, obj2, obj3, obj4;
obj1 = new Box01(1, "1", "2", "3");
obj2 = new Box01(2, "4", "5", "6");
obj3 = new Box01(3, "7", "8", "9");
obj4 = new Box01(4, "10", "11", "12");
printBox01(obj1);
printBox01(obj2);
printBox01(obj3);
printBox01(obj4);
}
static void printBox01(Box01 obj){
System.out.println("mybox1의 id 번호 : " + obj.boxId);
System.out.println("전체 박스의 개수는 "+ obj.amount);
}
}


이렇게 두 파일 따로 해서 생성자를 이용해서 id번호와 박스의 개수를 구해야하는데  
id번호는 구해졌는데 박스의 개수가 안구해지네요ㅜㅜ 계속0으로 뜨네요..ㅜㅜ
void addbox(int amount){
boxId+=amount;
}
이 부분이 박스의 수를 구하는 부분인데 여기서 어떻게 해야 총 4개가 나오는지 ㅜㅜ
부탁드릴게요ㅜㅜ

  Hit : 5027     Date : 2014/09/24 06:45



    
sametim 코딩에서 문제가 있다면
void addbox(int amount){
boxId+=amount;
}
여기서 이 코드는 boxId 에 amount를 더하는 것이거든요? 우리가 amount를 지정한 적이 없으니까 0이 되는거죠.
만약에 박스의 수를 구하고 싶으면 코딩을 box01이 아니라 boxTest에 해야될 듯 하네요. box01은 boxTest에서 호출된 후 각각의 obj1,2,3,4에 따로 저장되기 때문에 box01에서 갯수를 세 봤자 지금들어온 한가지 밖에는 셀 수 없습니다.
2014/12/19  
sametim JAVA가 가물가물해서 코딩은 못해드려서 죄송합니다 ㅠㅠ 2014/12/19  
sametim 코드 낭비일지도 모르지만.. 저로서는 이거말고는 되지가 않네요. 무능력해 죄송합니다.

public class Box01 {

int boxId, amount=0;
String width, length, height;

Box01(int boxId, String width, String length, String height) {
this.boxId = boxId;
this.width = width;
this.length = length;
this.height = height;
amount ++;
}
Box01(int boxId, String width, String length, String height, Box01 obj) {
this.boxId = boxId;
this.width = width;
this.length = length;
this.height = height;
this.amount = obj.amount + 1;
}
}

===============================================

public class Box01Test {
int amount=0;
public static void main(String args[]) {

Box01 obj1, obj2, obj3, obj4;
obj1 = new Box01(1, "1", "2", "3");
obj2 = new Box01(2, "4", "5", "6", obj1);
obj3 = new Box01(3, "7", "8", "9", obj2);
obj4 = new Box01(4, "10", "11", "12", obj3);
printBox01(obj1);

printBox01(obj2);
printBox01(obj3);
printBox01(obj4);
}

static void printBox01(Box01 obj) {
System.out.println("Box"+obj.boxId+"번의 id 번호 : " + obj.boxId);
System.out.println("box의 개수 : "+ obj.amount);
}

}
2014/12/19  
sametim 결국 괴상한 방법으로 코딩해버렷는데 죄송해요... 2014/12/19