java写一个鸡兔同笼计算代码
时间: 2023-04-01 10:02:02 浏览: 181
鸡兔同笼问题和编程,里面包含鸡兔同笼题目和编程代码,下载即可运行,适用于所有鸡兔同笼问题
5星 · 资源好评率100%
可以的,以下是 Java 代码:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入总头数:");
int totalHead = scanner.nextInt();
System.out.println("请输入总脚数:");
int totalFoot = scanner.nextInt();
int rabbitFoot = 4;
int chickenFoot = 2;
int rabbitNum = (totalFoot - totalHead * chickenFoot) / (rabbitFoot - chickenFoot);
int chickenNum = totalHead - rabbitNum;
System.out.println("鸡的数量为:" + chickenNum);
System.out.println("兔的数量为:" + rabbitNum);
}
}
阅读全文