@FLS BG 11/7 DV: TeWi Schleifen/Array Hilfe


So an alle, die Java Schleifen und Arrays noch nicht ganz durchgeblickt haben: hier eine kleine Hilfe:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***
* Wie verwende ich Schleifen und Array's?
* @author Lukas Schreiner
* Erstellt: 14.12.2008 12:10
* Für: Edgar 
*
* Methode 1: While-Schleife
* Methode 2: Do-While Schleife
* Methode 3: For-Schleife
* Methode 4: Array
***/
 
public class Edgar {
  //Variablen
  int i; //Zählervariable für die For-Schleife (ist wichtig)
  int array[]; // Festlegen eines Arrays (keine Erzeugung!)
  int durchlauefe; // Variable für die Anzahl der Durchläufe einer For-Schleife
  int variable0, variable1; // Festlegen zweier Hilfs-Integer Variablen für das Programm
  double variable2, variable3; // Festlegen zweier Hilfs-Double Variablen für das Programm
 
  public void methode1() { // While-Schleife
    variable2 = 20;
    variable3 = 40;
 
      /***
      * While bedeutet:
      * Es wird erst die Bedingung überprüft und dann die Anweisungen ausgeführt.
      ***/
 
      while(variable2 != variable3){ //Solange variable2 nicht variable3 entspricht
	 variable3 = variable3-variable2; //Rechne 40-20 ab und speichere es in variable3
      } //Die Schleife wird nur einmal durchgeführt.
      System.out.println("Variable2 : "+variable2+";nVariable3: "+variable3);
  }
 
  public void methode2() { // Do-While Schleife
    variable2 = 40.388;
    variable3 = 8893;
 
      /***
      * Do-While bedeutet:
      * Es wird erst eine Anweisung ausgeführt und dann die Bedingungs überprüft!
      ***/
 
    do{
      if( variable2 > variable3 ){ //Wenn variable2 größer als variable3 ist...
	variable2 = variable2-variable3; // führe folgendes aus: variable2 = variable2 - variable3;
      }else{ //Ansonsten lege anderes fest:
	variable3 = variable3-variable2; // Ziehe variable2 von variable3 ab und speichere es in variable3
      }
    }while( variable2 != variable3 ); // Führe Schleife solange aus, solange variable2 nicht variable3 entspricht.
    System.out.println("Variable2 : "+variable2+";nVariable3: "+variable3);
  }
 
  public void methode3() { //For-Schleife
    /***
    * For-Schleife
    * Es wird solange durchgeführt, bis die Zählervariable (hier: i) einen bestimmten Wert entspricht.
    ***/
    variable0 = 4;
    System.out.println("Vorher:");
    System.out.println("Variable2 : "+variable2+";nVariable3: "+variable3);
    for(i = 0; i < variable0; i++){ // Setze i auf 0; Bedingung: i kleiner als variable0; Erhöhe i immer um 1
				    // Da variable0 auf 4, wird die Schleife 3 mal ausgeführt.
      variable2 = variable2+variable3;
      variable3 = variable3-variable2;
    }
    System.out.println("nNacherher:");
    System.out.println("Variable2 : "+variable2+";nVariable3: "+variable3);
 
  }
 
  public void methode4() {
      // Erzeuge nun das Array array[]:
      array = new int[4]; // durch die 4 können 4 Werte in dem Array gespeichert werden:
			  // 0, 1, 2, 3 => 4 Fächer
      array[0] = 1; // Weise dem Fach 0 eine 1 zu.
      array[1] = 2; // Weise dem Fach 1 eine 2 zu.
      array[2] = 3; // Weise dem Fach 2 eine 3 zu.
      array[3] = 4; // Weise dem Fach 3 eine 4 zu.
 
      for(i = 0; i < array.length; i++){ // array.length => Zählt die Fächer des Arrays und gibt sie dort aus: 4
	System.out.println("Fach #"+i+": "+array[i]); // array[i] gibt das Fach mit der Nummer i zurück.
	// Mit array[i] = 5; kann man dem Array auch einem neuen Wert festlegen
      }
  }
 
 
  public static void main(String args[]){ // String args[] ist momentan noch nicht notwendig, dient zur Übergabe von Parametern beim Aufruf
    Edgar ed = new Edgar();
    System.out.println("nnWhile-Schleife:nn");
    ed.methode1();
    System.out.println("nnDo-While Schleife:nn");
    ed.methode2();
    System.out.println("nnFor-Schleife:nn");
    ed.methode3();
    System.out.println("nnArrays:nn");
    ed.methode4();
  }
}

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Other Posts

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!