179 lines
4.3 KiB

  1. package LernProgramm;
  2. import java.util.Random;
  3. import java.util.Scanner;
  4. public class ProgrammMain {
  5. // Quizz
  6. public static void Quizz() {
  7. Random rand = new Random();
  8. try (Scanner scanner = new Scanner(System.in)) {
  9. String[][] questions = {
  10. { "Welche Farbe hat ein Bananen?", "A) Gelb", "B) Grün", "C) Blau", "D) Rot", "A" },
  11. { "Wie viele Beine hat eine Spinne?", "A) 4", "B) 6", "C) 8", "D) 10", "C" },
  12. { "Wer hat die Formel E=mc² entwickelt?", "A) Isaac Newton", "B) Albert Einstein",
  13. "C) Galileo Galilei", "D) Stephen Hawking", "B" },
  14. { "Welches ist der größte Planet im Sonnensystem?", "A) Merkur", "B) Venus", "C) Erde",
  15. "D) Jupiter", "D" }
  16. // Sonstige Fragen
  17. };
  18. int questionIndex = rand.nextInt(questions.length);
  19. String[] currentQuestion = questions[questionIndex];
  20. System.out.println(currentQuestion[0]);
  21. System.out.println(currentQuestion[1]);
  22. System.out.println(currentQuestion[2]);
  23. System.out.println(currentQuestion[3]);
  24. System.out.println(currentQuestion[4]);
  25. String answer = scanner.nextLine();
  26. if (answer.equalsIgnoreCase(currentQuestion[5])) {
  27. System.out.println("Richtig!");
  28. } else {
  29. System.out.println("Falsch!");
  30. }
  31. }
  32. }
  33. // Prinmbis100
  34. public static void PrimBis100() {
  35. for (int i = 2; i <= 100; i++) {
  36. boolean isPrime = true;
  37. for (int j = 2; j < i; j++) {
  38. if (i % j == 0) {
  39. isPrime = false;
  40. break;
  41. }
  42. }
  43. if (isPrime) {
  44. System.out.print(i + " ");
  45. }
  46. }
  47. }
  48. // Binärinverter
  49. public static void Binaerrechner() {
  50. try (Scanner scanner = new Scanner(System.in)) {
  51. System.out.print("Gebe den ersten Binärcode ein: ");
  52. String binaryCode1 = scanner.nextLine();
  53. System.out.print("Gebe den zweiten Binärcode ein: ");
  54. String binaryCode2 = scanner.nextLine();
  55. System.out.print("Gebe die gewünschte Operation ein (+, -, *, /): ");
  56. char operation = scanner.next().charAt(0);
  57. int result = calculate(binaryCode1, binaryCode2, operation);
  58. System.out.println("Das Ergebnis ist: " + result);
  59. }
  60. }
  61. public static int calculate(String binaryCode1, String binaryCode2, char operation) {
  62. int decimal1 = binaryToDecimal(binaryCode1);
  63. int decimal2 = binaryToDecimal(binaryCode2);
  64. int result = 0;
  65. switch (operation) {
  66. case '+':
  67. result = decimal1 + decimal2;
  68. break;
  69. case '-':
  70. result = decimal1 - decimal2;
  71. break;
  72. case '*':
  73. result = decimal1 * decimal2;
  74. break;
  75. case '/':
  76. result = decimal1 / decimal2;
  77. break;
  78. default:
  79. System.out.println("Ungültige Operation! Bitte wähle +, -, * oder /.");
  80. return 0;
  81. }
  82. return decimalToBinary(result);
  83. }
  84. public static int binaryToDecimal(String binaryCode) {
  85. int decimal = 0;
  86. for (int i = binaryCode.length() - 1; i >= 0; i--) {
  87. char currentChar = binaryCode.charAt(i);
  88. if (currentChar == '1') {
  89. decimal += Math.pow(2, binaryCode.length() - i - 1);
  90. } else if (currentChar != '0') {
  91. System.out.println("Ungültiger Binärcode! Bitte gebe nur Nullen und Einsen ein.");
  92. return 0;
  93. }
  94. }
  95. return decimal;
  96. }
  97. public static int decimalToBinary(int decimal) {
  98. int binary = 0;
  99. int power = 0;
  100. while (decimal > 0) {
  101. binary += (decimal % 2) * (int) Math.pow(10, power);
  102. decimal /= 2;
  103. power++;
  104. }
  105. return binary;
  106. }
  107. public static void main(String[] args) {
  108. System.out.println("Willkommen bei diesem kleinen konsolenbasierten Spiel!\n");
  109. System.out.println("Du hast 3 Spielmodi!\n");
  110. System.out.println("1. Karteikarten\n");
  111. System.out.println("2. Quizz\n");
  112. System.out.println("3. Binaer-Inverter\n");
  113. System.out.println("4. PrimZahlenAusgabe\n");
  114. try (Scanner einleser = new Scanner(System.in)) {
  115. int wahl = einleser.nextInt();
  116. switch (wahl) {
  117. case 1:
  118. FunktionenAusgelagert.Karteikarten();
  119. break;
  120. case 2:
  121. Quizz();
  122. // Funktion
  123. break;
  124. case 3:
  125. Binaerrechner();
  126. // Funktion
  127. break;
  128. case 4:
  129. PrimBis100();
  130. // Funktion
  131. break;
  132. case 5:
  133. System.out.println("Diese Funktion wird derzeit entwickelt! Hab bitte etwas Geduld oder sei kreativ und erstelle dir selber eine Funktion!");
  134. // Funktion
  135. break;
  136. default:
  137. System.out.println("Ungültige Eingabe, versuche es bitte erneut!\n");
  138. }
  139. }
  140. System.out.println("Programm beendet\n");
  141. }
  142. }