From 04411dbdcee85750db8d62835c57c5381ebf66f5 Mon Sep 17 00:00:00 2001 From: jannisfingerhut Date: Fri, 10 Feb 2023 04:53:29 +0100 Subject: [PATCH] =?UTF-8?q?Neue=20Funktionalit=C3=A4t=20hinzugef=C3=BCgt:?= =?UTF-8?q?=20Fakult=C3=A4t=20+=20Test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bin/LernProgramm/FunktionenAusgelagert.java | 28 +++++++++++++++++++ bin/LernProgramm/testProgramm.java | 31 +++++++++++++++++++-- 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/bin/LernProgramm/FunktionenAusgelagert.java b/bin/LernProgramm/FunktionenAusgelagert.java index 5d16e75..55ae260 100644 --- a/bin/LernProgramm/FunktionenAusgelagert.java +++ b/bin/LernProgramm/FunktionenAusgelagert.java @@ -3,6 +3,34 @@ package LernProgramm; import java.util.Scanner; public class FunktionenAusgelagert { + + public static void Fakultaet() { + + try (Scanner scanner3 = new Scanner(System.in)) { + String numbera = scanner3.nextLine(); + int number = Integer.parseInt(numbera); + + + + if (number <= 0) { + System.out.println("1"); + } + int result = 1; + for (int i = 1; i <= number; i++) { + result *= i; + } + System.out.println(result); + } catch (NumberFormatException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + + } + + + + public static void schaltjahr() { System.out.println("Welches Jahr möchtest du untersuchen?"); diff --git a/bin/LernProgramm/testProgramm.java b/bin/LernProgramm/testProgramm.java index 2a74cf7..a9fe958 100644 --- a/bin/LernProgramm/testProgramm.java +++ b/bin/LernProgramm/testProgramm.java @@ -87,8 +87,35 @@ class testProgramm { - - + @Test + public void testFakultaetWithPositiveNumber() { + String input = "5\n"; + ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + + FunktionenAusgelagert.Fakultaet(); + + assertEquals("120\n", out.toString()); + } + + @Test + public void testFakultaetWithZero() { + String input = "0\n"; + ByteArrayInputStream in = new ByteArrayInputStream(input.getBytes()); + System.setIn(in); + + ByteArrayOutputStream out = new ByteArrayOutputStream(); + System.setOut(new PrintStream(out)); + + FunktionenAusgelagert.Fakultaet(); + + assertTrue(true); + } + + @Test public void testSchaltjahr() { ByteArrayInputStream in = new ByteArrayInputStream("2000\n".getBytes());