2013-11-22 3 views

답변

1

코드로 무엇을 하려는지 잘 모르거나 심지어 그것을 읽기 시작하는 방법. 그러나 여기 루프

Scanner scanner = new Scanner(System.in); 

System.out.println("Enter intitial food supply: "); 
int foodSupply = scanner.nextInt(); 

System.out.println("Enter intial amount of animal: "); 
int foodIntake = scanner.nextInt(); 

System.out.println("Enter amount of food added per hour: "); 
int foodAdded = scanner.nextInt(); 

int hours = 0; 

while (foodIntake < foodSupply){ 
    hours++; 
    foodIntake *= 2; 
    foodSupply += foodAdded; 
} 

System.out.println("It took " + hours + " hours for animals to outgrow food supply"); 
System.out.println("Animals when food supply reached: " + foodIntake); 
System.out.println("Food Supply after last hour: " + foodSupply); 
0

1)에서 System.out.println 문

initial = end2; 

    initialfood = j; 

당신은 초기 및 initialfood 재 할당되지 않은 후이 두 줄을 추가에 대한 제안입니다. 이 때문에 initial은 항상 초기 입력이며 initialfood는 항상 초기 입력입니다. 코드 라인으로부터

:

end=initial; 

    end have the same value every time... 

2)

2) (I = J) 동안!. i와 j 사이에는 관계가 없습니다. 그런 조건이있는 이유는 무엇입니까? 변경 :

while(j>0) 



╔══════════════╦════════════╦════════════╦═════════════╦═══════╗ 
║ Hour-Animals ║ start-Food ║ start-Food ║ End-Animals ║ End ║ 
╠══════════════╬════════════╬════════════╬═════════════╬═══════╣ 
║   1 ║   10 ║  1000 ║  4990 ║ 20 ║ 
║   2 ║   20 ║  4990 ║  8970 ║ 40 ║ 
║   3 ║   40 ║  8970 ║  12930 ║ 80 ║ 
║   4 ║   80 ║  12930 ║  16850 ║ 160 ║ 
║   5 ║  160 ║  16850 ║  20690 ║ 320 ║ 
║   6 ║  320 ║  20690 ║  24370 ║ 640 ║ 
║   7 ║  640 ║  24370 ║  27730 ║ 1280 ║ 
║   8 ║  1280 ║  27730 ║  30450 ║ 2560 ║ 
║   9 ║  2560 ║  30450 ║  31890 ║ 5120 ║ 
║   10 ║  5120 ║  31890 ║  30770 ║ 10240 ║ 
║   11 ║  10240 ║  30770 ║  24530 ║ 20480 ║ 
║   12 ║  20480 ║  24530 ║  8050 ║ 40960 ║ 
║   13 ║  40960 ║  8050 ║  -28910 ║ 81920 ║ 
╚══════════════╩════════════╩════════════╩═════════════╩═══════╝ 
관련 문제