有人可以用我的循环代码帮助我

时间:2013-11-22 12:26:32

标签: java loops

有人能告诉我我的代码有什么问题。我只能使用bufferedreader和loops.It sh

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

1)在System.out.println语句

之后添加这两行
    initial = end2;

    initialfood = j;

你没有重新分配初始和初始食物。因此,initial始终是初始输入,而initialfood始终是initialfood输入。

从代码行:

   end=initial;

   end have the same value every time...

2)

2)while(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 ║
╚══════════════╩════════════╩════════════╩═════════════╩═══════╝