Java 有人能帮我处理循环码吗
有人能告诉我我的代码有什么问题吗?我只能使用bufferedreader和loops。我不知道你想用代码做什么,甚至不知道如何开始阅读。但这里有一个循环的建议Java 有人能帮我处理循环码吗,java,loops,Java,Loops,有人能告诉我我的代码有什么问题吗?我只能使用bufferedreader和loops。我不知道你想用代码做什么,甚至不知道如何开始阅读。但这里有一个循环的建议 Scanner scanner = new Scanner(System.in); System.out.println("Enter intitial food supply: "); int foodSupply = scanner.nextInt(); System.out.println("Enter intial amoun
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);
Scanner Scanner=新的扫描仪(System.in);
System.out.println(“输入初始食物供应:”);
int foodSupply=scanner.nextInt();
System.out.println(“输入动物的初始数量:”);
int foodindtake=scanner.nextInt();
System.out.println(“输入每小时添加的食物量:”);
int foodAdded=scanner.nextInt();
整小时=0;
while(食物摄取<食物供应){
小时++;
食物摄取量*=2;
食品供应+=食品添加;
}
System.out.println(“动物生长超过食物供应需要“+小时+”小时”);
System.out.println(“当食物供应达到时的动物:“+foodIntake”);
System.out.println(“最后一小时后的食品供应:+食品供应”);
1)在System.out.println语句后添加这两行
initial = end2;
initialfood = j;
您没有重新分配初始和初始食物。因此,initial始终是初始输入,initialfood始终是initialfood输入
从代码行开始:
end=initial;
end have the same value every time...
(二)
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 ║
╚══════════════╩════════════╩════════════╩═════════════╩═══════╝