300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > html计算鸡兔同代码 鸡兔同笼问题

html计算鸡兔同代码 鸡兔同笼问题

时间:2018-11-29 12:54:41

相关推荐

html计算鸡兔同代码 鸡兔同笼问题

鸡兔同笼是古代算数的一个难题,但是在现在的我们看起来似乎很简单。

代码实现:

import java.util.Scanner;

public class test {

private static int chook;

private static int hare;

private static int head = 0;

private static int foot = 0;

public static void main(String[] args) {

show();

}

public static void show() {

//进行数据的录入

String num = "";

String[] nums;

Scanner sc = new Scanner(System.in);

System.out.println("请输入头和脚的数量(用逗号隔开):");

try {

while(true) {

num = sc.nextLine();

nums = num.split(",|,");

head = Integer.parseInt(nums[0]);

foot = Integer.parseInt(nums[1]);

if(foot%2 != 0) {

System.out.println("请输入正确的脚数!!!");

continue;

}else {

count();

break;

}

}

}catch(Exception e){

System.out.println("输入有误,退出!!!");

System.exit(-1);

}

}

public static void count() {

//计算鸡兔的数量

for(int chook = 0;chook <= head;chook++) {

if((((chook*2)+(head-chook)*4) == foot)){

hare = head -chook;

System.out.println("鸡的数量:"+ chook +"兔的数量:"+ hare);

continue;

}

}

}

}

来源:/lijiafeng/p/7608339.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。