Advertisement
Guest User

Untitled

a guest
Nov 9th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.27 KB | None | 0 0
  1. package ueb05;
  2. import com.sun.media.sound.InvalidFormatException;
  3.  
  4. import java.util.Scanner;
  5. public class Ueb05 {
  6. public static void main(String[] args) {
  7. double[] zahlen = new double[3];
  8. boolean eingabe = true;
  9. int i = 0;
  10. Scanner SC = new Scanner(System.in);
  11. System.out.println("Bitte den Benzinpreise angeben");
  12. while (i < zahlen.length && eingabe) {
  13. String input = SC.nextLine();
  14. System.out.println(i);
  15. if (input.toUpperCase().equals("x")) {
  16. eingabe = false;
  17. }
  18. else {
  19. try {
  20. double Preis = Double.parseDouble(input);
  21. if (Preis >= 1 && Preis <= 2) {
  22. zahlen[i] = Preis;
  23. i++;
  24. } else {
  25. System.out.println("Es wurde ein ungueltiger Preis eingegeben!");
  26. }
  27. } catch (NumberFormatException e) {
  28. System.out.println("");
  29. }
  30. }
  31. }
  32.  
  33. if (zahlen.length == 3) {
  34. eingabe = false;
  35. }
  36.  
  37. if (eingabe == false) {
  38. System.out.format("Anzahl der Preise: %d%n", zahlen.length);
  39. double kleinsteZahl = zahlen[0];
  40. for (int j = 1; j < zahlen.length; j++) {
  41. if (zahlen[j] < kleinsteZahl) {
  42. kleinsteZahl = zahlen[j];
  43. }
  44. }
  45. System.out.format("Kleinster Preis: %1.2f%n", kleinsteZahl);
  46. double größteZahl = zahlen[0];
  47. for (int g = 1; g < zahlen.length; g++) {
  48. if (zahlen[g] > größteZahl) {
  49. größteZahl = zahlen[g];
  50. }
  51. }
  52. System.out.format("Höchster Preis: %1.2f%n", größteZahl);
  53. double summe = zahlen[0];
  54. for (int h = 1; h < zahlen.length; h++) {
  55. summe += zahlen[h];
  56. }
  57. summe = summe / zahlen.length;
  58. System.out.format("Mittelwert: %1.2f%n", summe);
  59. double spanne;
  60. spanne = größteZahl - kleinsteZahl;
  61. System.out.format("Spanne: %1.2f%n", spanne);
  62. }
  63.  
  64.  
  65. }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement