用java写一个鸡兔同笼
时间: 2024-05-24 15:12:48 浏览: 134
鸡兔同笼问题和编程,里面包含鸡兔同笼题目和编程代码,下载即可运行,适用于所有鸡兔同笼问题
5星 · 资源好评率100%
import java.util.Scanner;
public class ChickenRabbitCage {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入总数:");
int total = scanner.nextInt();
System.out.println("请输入总腿数:");
int legs = scanner.nextInt();
int rabbits = (legs - total * 2) / 2;
int chickens = total - rabbits;
if (rabbits < 0 || chickens < 0 || legs % 2 != 0) {
System.out.println("无法计算鸡兔数量,请重新输入!");
} else {
System.out.println("鸡的数量为:" + chickens);
System.out.println("兔的数量为:" + rabbits);
}
}
}
阅读全文