Difference between r1.1 and the current
@@ -29,8 +29,6 @@
}
a.showPeople();
b.showPeople();
System.out.print("어디로 이동? ");
String destination = input.next();
System.out.print("강 건널 사람 몇 명(0~2)? ");
int num = input.nextInt();
int [] name = new int[num];
a.showPeople();
b.showPeople();
int num = input.nextInt();
int [] name = new int[num];
@@ -39,7 +37,7 @@
System.out.print("건널 사람 이름?(Layton: 1, Luke: 2, Bad: 3)->");
name[i] = input.nextInt();
}
switch(name[i]){
case 1:
name[i] = input.nextInt();
}
if(mustCheck(destination,num,name)){
if(mustCheck(num,name)){
for(int i=0; i<num; i++){switch(name[i]){
case 1:
@@ -52,7 +50,7 @@
ship = badUncle;
break;
}
}else{
b.crossRiver(a, ship);
break;
}
if(b.getTownName().equals(destination)){
if(a.getTownName().equals(ship.getPosition().getTownName())){
a.crossRiver(b, ship);}else{
b.crossRiver(a, ship);
@@ -63,11 +61,7 @@
a.showPeople();
b.showPeople();
}
if(destination !="A" && destination !="B"){
System.out.println("[Error] 거긴 어디?");
return false;
}
System.out.println("[Error] 배는 2인승이야!");
return false;
b.showPeople();
}
public static boolean mustCheck(String destination, int num, int [] name){
public static boolean mustCheck(int num, int [] name){
if(num > 2){System.out.println("[Error] 배는 2인승이야!");
return false;
코드 ¶
Main.java ¶
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); Town a = new Town("A"); Town b = new Town("B"); Person layton = new Person(); layton.setName("Layton"); Person luke = new Person(); luke.setName("Luke"); Person badUncle = new Person(); badUncle.setName("Bad"); a.addPerson(layton); a.addPerson(luke); a.addPerson(badUncle); while(b.getPeopleIndex() < 3){ if(layton.getPosition() != luke.getPosition() && luke.getPosition() == badUncle.getPosition()){ System.out.println("Luke가 강에 빠졌습니다ㅠㅠ"); System.exit(1); } a.showPeople(); b.showPeople(); System.out.print("강 건널 사람 몇 명(0~2)? "); int num = input.nextInt(); int [] name = new int[num]; Person ship = new Person(); for(int i=0; i<num; i++){ System.out.print("건널 사람 이름?(Layton: 1, Luke: 2, Bad: 3)->"); name[i] = input.nextInt(); } if(mustCheck(num,name)){ for(int i=0; i<num; i++){ switch(name[i]){ case 1: ship = layton; break; case 2: ship = luke; break; case 3: ship = badUncle; break; } if(a.getTownName().equals(ship.getPosition().getTownName())){ a.crossRiver(b, ship); }else{ b.crossRiver(a, ship); } } } } a.showPeople(); b.showPeople(); } public static boolean mustCheck(int num, int [] name){ if(num > 2){ System.out.println("[Error] 배는 2인승이야!"); return false; } if(num == 1 && name[0]==2){//Luke 혼자 가면 System.out.println("[Error] Luke 혼자 못탐ㅋ"); return false; } return true; } }
Person.java ¶
public class Person { private String name; private Town position; public void setName(String name){ this.name = name; } public void setPosition(Town position){ this.position = position; } public String getName(){ return name; } public Town getPosition(){ return position; } }
Town.java ¶
public class Town { private String townName; private Person [] people; private int maxPeople = 3; private int peopleIndex = 0; public Town(){ people = new Person[maxPeople]; } public Town(String townName){ this.townName = townName; people = new Person[maxPeople]; } public void setTownName(String townName){ this.townName = townName; } public String getTownName(){ return townName; } public int getPeopleIndex(){ return peopleIndex; } public void addPerson(Person p){ p.setPosition(this); people[peopleIndex++] = p; } public void crossRiver(Town destination, Person ship){ for(int i=0; i<peopleIndex;i++){ if(ship.getName().equals(people[i].getName())){ people[i] = people[peopleIndex-1]; destination.addPerson(ship); peopleIndex--; } } } public void showPeople(){ for(int i=0; i<peopleIndex; i++){ System.out.print("town : " + people[i].getPosition().getTownName()); System.out.println(", name : " + people[i].getName()); } } }