Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of chicken and rabbit in the same cage with Java language

Shulou Source: shulou.com Published: 2022-06-01 13:34:45 10月03日 Update

This article introduces the relevant knowledge of "how to solve the problem of chickens and rabbits in the same cage with Java language". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Topic description

It is known that the number of chickens is n, the number of rabbits is m, the total number of chickens and rabbits is H, the total number of feet is Y

For loop syntax

For (expression 1; expression 2; expression 3) {

/ / cyclic body

}

/ / expression 1 is an assignment statement.

Function: assign an initial value to a loop variable

Expression 2 is a conditional statement.

Function: it is the cyclic condition of cyclic structure.

Expression 3 is an assignment statement (usually + or -).

Function: use open to modify the value of a loop variable

Problem-solving ideas

We know that the number of chickens is n and the number of rabbits is m. That is,

There is no difference between the two parts of the plant.

Since we don't know the number of chickens and rabbits, we first assume that the number of chickens is 1, and then use the for loop to find the number that matches the rabbits, that is, the code is as follows

Import java.util.Scanner;public class Demo {public static void main (String [] args) {int chicken,rabbit; int Hmagy Y; Scanner input=new Scanner (System.in); System.out.println ("Please enter the total number of chickens and rabbits"); H=input.nextInt () System.out.println ("Please enter the total number of feet of chicken and rabbit"); Y=input.nextInt (); for (chicken=1;chicken

Tags: Quantity loop expression function statement language problem content variable head count more condition knowledge input utility achievement next code dilemma reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Linux Huawei MySQL MariaDB