Advertisement
Guest User

sdasfsdfsdf

a guest
Oct 30th, 2014
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. package inout;
  2.  
  3. import java.util.Scanner;
  4. import java.util.Calendar;
  5. import java.util.Locale;
  6. import java.util.Date;
  7. import java.util.NoSuchElementException;
  8. import java.util.InputMismatchException;
  9. import java.util.regex.Pattern;
  10. import java.text.ParseException;
  11. import java.text.DateFormat;
  12.  
  13. /** Diese Klasse stellt Methoden zur Verfügung, <br/>
  14. * um Texte und einfache Typen von der Konsole einzulesen.<br/>
  15. * Die Ausnahmebehandlung ist Aufgabe des Aufrufers.<hr/>
  16. * @author Helmut Balzert
  17. * @version 2.1 / 1.11.2012
  18. */
  19. public class Console
  20. {
  21. private static Scanner sc;
  22.  
  23. //Unterdrückung des default-Konstruktor,
  24. //um eine Objekterzeugung zu verhindern
  25. private Console()
  26. {
  27. //Dieser Konstruktor wird nie aufgerufen
  28. }
  29.  
  30. /**Liest eine Zeile von der Konsole
  31. * @return Eingelesene Zeile vom Typ String.
  32. * @exception NoSuchElementException:
  33. * Es wurde keine Eingabezeile gefunden.
  34. * @exception IllegalStateException:
  35. * Die verwendete Methode ist nicht geöffnet.
  36. */
  37. public static String readString()
  38. throws NoSuchElementException, IllegalStateException
  39. {
  40. Scanner sc = new Scanner(System.in);
  41. return sc.nextLine();
  42. }
  43.  
  44. /**Liest eine Zeile von der Konsole
  45. * @return Eingelesene Zeile vom Typ char[].
  46. * @exception NoSuchElementException:
  47. * Es wurde keine Eingabezeile gefunden.
  48. * @exception IllegalStateException:
  49. * Die verwendete Methode ist nicht geöffnet.
  50. */
  51. public static char[] readCharArray()
  52. throws NoSuchElementException, IllegalStateException
  53. {
  54. sc = new Scanner(System.in);
  55. String text = sc.nextLine();
  56. return text.toCharArray();
  57. }
  58.  
  59. /**Liest einen booleschen Wert von der Konsole
  60. * @return Boolescher Wert true oder false.
  61. * @exception NoSuchElementException:
  62. * Es wurde keine Eingabezeile gefunden.
  63. * @exception IllegalStateException:
  64. * Die verwendete Methode ist nicht geöffnet.
  65. * @exception InputMismatchException:
  66. * Die Eingabe entspricht nicht dem Typ.
  67. */
  68. public static boolean readBoolean() throws
  69. InputMismatchException, NoSuchElementException,
  70. IllegalStateException
  71. {
  72. sc = new Scanner(System.in);
  73. return sc.nextBoolean();
  74. }
  75.  
  76. /**Liest eine ganze Zahl vom Typ int von der Konsole
  77. * @return Ganze Zahl vom Typ int.
  78. * @exception InputMismatchException:
  79. * Die Eingabe entspricht nicht dem Typ.
  80. * @exception NoSuchElementException:
  81. * Es wurde keine Eingabezeile gefunden.
  82. * @exception IllegalStateException:
  83. * Die verwendete Methode ist nicht geöffnet.
  84. */
  85. public static int readInt() throws
  86. InputMismatchException, NoSuchElementException,
  87. IllegalStateException
  88. {
  89. return new Scanner(System.in).nextInt();
  90. }
  91.  
  92. /**Liest eine ganze Zahl vom Typ long von der Konsole
  93. * @return Ganze Zahl vom Typ long
  94. * @exception InputMismatchException:
  95. * Die Eingabe entspricht nicht dem Typ.
  96. * @exception NoSuchElementException:
  97. * Es wurde keine Eingabezeile gefunden.
  98. * @exception IllegalStateException:
  99. * Die verwendete Methode ist nicht geöffnet.
  100. */
  101. public static long readLong() throws
  102. InputMismatchException, NoSuchElementException,
  103. IllegalStateException
  104. {
  105. return new Scanner(System.in).nextLong();
  106. }
  107.  
  108. /**Liest eine Gleitpunktzahl vom Typ float von der Konsole
  109. * Englische Notation: Trennung der
  110. * Nachkommastellen durch Punkt
  111. * @return Gleitpunktzahl vom Typ float
  112. * @exception InputMismatchException:
  113. * Die Eingabe entspricht nicht dem Typ.
  114. * @exception NoSuchElementException:
  115. * Es wurde keine Eingabezeile gefunden.
  116. * @exception IllegalStateException:
  117. * Die verwendete Methode ist nicht geöffnet.
  118. */
  119. public static float readFloatPoint() throws
  120. InputMismatchException, NoSuchElementException,
  121. IllegalStateException
  122. {
  123. Locale.setDefault(Locale.ENGLISH);
  124. return new Scanner(System.in).nextFloat();
  125. }
  126.  
  127. /**Liest eine Gleitpunktzahl vom Typ float von der Konsole
  128. * Deutsche Notation: Trennung der
  129. * Nachkommastellen durch Komma
  130. * @return Gleitpunktzahl vom Typ float
  131. * @exception InputMismatchException:
  132. * Die Eingabe entspricht nicht dem Typ.
  133. * @exception NoSuchElementException:
  134. * Es wurde keine Eingabezeile gefunden.
  135. * @exception IllegalStateException:
  136. * Die verwendete Methode ist nicht geöffnet.
  137. */
  138. public static float readFloatComma() throws
  139. InputMismatchException, NoSuchElementException,
  140. IllegalStateException
  141. {
  142. Locale.setDefault(Locale.GERMAN);
  143. return new Scanner(System.in).nextFloat();
  144. }
  145.  
  146. /**Liest eine Gleitpunktzahl vom Typ double von der Konsole
  147. * Englische Notation: Trennung der
  148. * Nachkommastellen durch Punkt
  149. * @return Gleitpunktzahl vom Typ double
  150. * @exception InputMismatchException:
  151. * Die Eingabe entspricht nicht dem Typ.
  152. * @exception NoSuchElementException:
  153. * Es wurde keine Eingabezeile gefunden.
  154. * @exception IllegalStateException:
  155. * Die verwendete Methode ist nicht geöffnet.
  156. */
  157. public static double readDoublePoint() throws
  158. InputMismatchException, NoSuchElementException,
  159. IllegalStateException
  160. {
  161. Locale.setDefault(Locale.ENGLISH);
  162. return new Scanner(System.in).nextDouble();
  163. }
  164.  
  165. /**Liest eine Gleitpunktzahl vom Typ double von der Konsole
  166. * Deutsche Notation: Trennung der
  167. * Nachkommastellen durch Komma
  168. * @return Gleitpunktzahl vom Typ double
  169. * @exception InputMismatchException:
  170. * Die Eingabe entspricht nicht dem Typ.
  171. * @exception NoSuchElementException:
  172. * Es wurde keine Eingabezeile gefunden.
  173. * @exception IllegalStateException:
  174. * Die verwendete Methode ist nicht geöffnet.
  175. */
  176. public static double readDoubleComma() throws
  177. InputMismatchException, NoSuchElementException,
  178. IllegalStateException
  179. {
  180. Locale.setDefault(Locale.GERMAN);
  181. return new Scanner(System.in).nextDouble();
  182. }
  183.  
  184. /**Liest ein Zeichen vom Typ char von der Konsole
  185. * @return Erstes eingegebene Zeichen vom Typ char.
  186. * @exception NoSuchElementException:
  187. * Es wurde keine Eingabezeile gefunden.
  188. */
  189. public static char readChar() throws
  190. NoSuchElementException,IllegalStateException
  191. {
  192. String s = new Scanner(System.in).next();
  193. return s.charAt(0);
  194. }
  195.  
  196. }
  197. Legen Sie für diese Klasse eine Datei Console.java in dem Ordner inout an und übersetzen Sie diese. In Ihr Programm BMI3 importieren Sie nun dieses Paket:
  198. Java Symbol
  199.  
  200. BMI3
  201. /************************************************
  202. Programm zur Berechnung des BMI (Body Mass Index)
  203. Eingabewerte werden über die Konsole eingelesen
  204. *************************************************/
  205.  
  206. //Importieren des Pakets inout mit der Klasse Console
  207. import inout.Console;
  208.  
  209. public class BMI3
  210. {
  211. //Berechnet den BMI
  212. public static void main (String args[])
  213. {
  214. double bmi; //lokale Variablen
  215. double koerperGewicht = 0.0;
  216. double koerperGroesse = 0.0;
  217. System.out.println
  218. ("Geben Sie bitte Ihr Gewicht in kg ein:");
  219. //Aufruf der Methode readDoubleComma()
  220. koerperGewicht = Console.readDoubleComma();
  221. System.out.println
  222. ("Geben Sie bitte Ihre Groesse in m ein:");
  223. //Aufruf der Methode readDoubleComma()
  224. koerperGroesse = Console.readDoubleComma();
  225.  
  226. bmi = koerperGewicht / (koerperGroesse * koerperGroesse);
  227. System.out.println
  228. ("Ihr Gewicht von " + koerperGewicht + " kg");
  229. System.out.println
  230. ("und Ihre Groesse von " + koerperGroesse + " m");
  231. System.out.println("ergeben einen BMI von " + bmi);
  232. System.out.println
  233. ("Die hoechste Lebenserwartung haben Menschen");
  234. System.out.println("mit einem BMI zwischen 20 und 24");
  235. }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement