import java.util.Scanner; /** * calculate the number of rabbits and chickens * * @author Kevin Maurizi * @since 2021-11-11 * @version 1.0 */ public class Animals { Scanner scanner; /** * constructor */ public Animals() { scanner = new Scanner(System.in); } /** * starts the execution * * @param args * command line arguments */ public static void main(String[] args) { Animals program = new Animals(); System.out.println("Willkommen"); program.calculate(); System.out.println("Bye Bye"); } /** * calculates the numbers */ private void calculate() { int animals; int legs; int rabbit; int chicken; int maxLegs; int minLegs; System.out.print("Anzahl Tiere >"); animals = scanner.nextInt(); scanner.nextLine(); // Scanner leeren maxLegs = animals * 4; minLegs = animals *2; System.out.print("Anzahl Beine >"); legs = scanner.nextInt(); scanner.nextLine(); // Scanner leeren if(legs > maxLegs || legs < minLegs){ System.out.print("Anzahl Beine ungültig, bitte erneut eingeben (Min: "+minLegs +" Max: " + maxLegs +") >"); legs = scanner.nextInt(); scanner.nextLine(); // Scanner leeren } rabbit = animals; chicken = 0; while ((rabbit * 4 + chicken * 2) > legs) { rabbit = rabbit - 1; chicken = chicken + 1; } System.out.println("Es sind " + rabbit + " Hasen und " + chicken + " Huehner"); } }