Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

fußgesteuerte do-Schleife

Empfohlene Antworten

Hallo,

ich habe hier eine Aufgabe mit ganzzahligen Potenzen gelöst, allerdings nur auf die mir als Anfänger bisher bekannte Methode mit der while-Schleife:

{public static void main(String args[]) 

 {int b=15;  /* Zahl    */

  int p=1; /* Potenz     */

  int n=0;  /* Exponent     */

  int kw=2100000000; /* Kontrollwert   */

  System.out.println("Ganzzahlige Potenzen");

  while ((b>1) && (kw>= && (p<=2100000000))

  {p=(int)Math.pow((double)b, (double)n);

   n=n;

   {System.out.println("Exponent: "+n);

    System.out.println(b+" hoch "+n+" = "+p);

   }

   kw=kw/b;

   n=n+1;

  }

  System.out.println("Ende der Berechnung");

 }

}

[/code]

ich will das Ganze nun in eine fußgesteuerte do-Schleife wandeln, stoße da im Moment aber etwas an meine Grenzen. Habe gelesen dass bei einer do-Schleife erst am Ende überprüft wird, ob die Schleife nochmal durchlaufen werden muss. Wie geht denn das? Würde mich freuen wenn mir jmd. kurz helfen könnte...

Schon folgendes probiert?

{public static void main(String args[]) 

 {int b=15;  /* Zahl    */

  int p=1; /* Potenz     */

  int n=0;  /* Exponent     */

  int kw=2100000000; /* Kontrollwert   */

  System.out.println("Ganzzahlige Potenzen");

  [B]do[/B] {p=(int)Math.pow((double)b, (double)n);

   n=n;

   {System.out.println("Exponent: "+n);

    System.out.println(b+" hoch "+n+" = "+p);

   }

   kw=kw/b;

   n=n+1;

  }  [B]while ((b>1) && (kw>= && (p<=2100000000));[/B]

  System.out.println("Ende der Berechnung");

 }

}

[/code]

Einfach schon mal so probiert? Die Variablen die du vor der Schleife initialisierst erfüllen im ersten Durchlauf die Bedingungen. Damit wird die Schleifen immer min. 1x betreten.

vielen Dank, so geht's. Eine Frage hätte ich noch: Angenommen jmd. gibt per Tastatureingabe einen Wert für b ein (bei mir ist b ja vorgegeben). Ich will jetzt, dass das Programm sofort beendet wird, wenn jmd. für b<1 eingibt.

Was muss ich dann machen? eine If-Anweisung?

if(b<1)

 System.out.println("Eingabefehler");

Da fehlt natürlich noch was, die Berechnung wird ja fortgesetzt.

hat sich erledigt

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Konto

Navigation

Suchen

Suchen

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.