用java 怎样编写鸡兔问题?问鸡有几只,
public class ChickenAndRabbit {
public static int legs = 100;
public static int heads = 40;
public ChickenAndRabbit(){
calculate();
}
public void calculate(){
int chicken,rabbit;
for(chicken = 0;chicken <= legs/2;chicken++){//最多100/2只鸡
for(rabbit = 0;rabbit <= legs/4;rabbit++){//最多100/4只兔子
if((chi...全部
public class ChickenAndRabbit {
public static int legs = 100;
public static int heads = 40;
public ChickenAndRabbit(){
calculate();
}
public void calculate(){
int chicken,rabbit;
for(chicken = 0;chicken <= legs/2;chicken++){//最多100/2只鸡
for(rabbit = 0;rabbit <= legs/4;rabbit++){//最多100/4只兔子
if((chicken*2+rabbit*4) == legs && (chicken+rabbit) == heads){
System。
out。
println("chicken:"+chicken+",rabbit:"+rabbit);
}
}
}
}
public static void main(String[] args) {
new ChickenAndRabbit();
}
}。收起