Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

Selektion vermeiden (Schleifen)

Schreiben Sie folgendes Programm, ohne eine Verzweigung auszuführen.
Verwenden Sie lediglich die Iteration (while()). Fügen Sie bei Bedarf beliebige
Boole'sche Variable ein. Natürlich darf kein "break" oder analoges Konstrukt verwendet werden!

a := input("A")
b := input("B")
c := input("C")
if(a < b)
{
  print(a)
}
else
{
  print(c)
}

Diese Aufgabe aus der theoretischen Informatik deutet an, dass jedes Programm keine weitere Kontrollstruktur als while() benötigt.

Bem.: Die "input()"-Funktion soll den Anwender nach einem Wert fragen. Leider gibt es diese Funktion nicht in allen Sprachen und fast jede Programmiersprache hat hierfür einen anderen Namen verwendet.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

2 Lösung(en)

public class Selektionslos {
  public static void main(String[] args) {
    new Selektionslos().top(); }

 void top() {
     int a, b, c;
     a = input("A");
     b = input("B");
     c = input("C");
     boolean erledigt = false;
     while(a < b && !erledigt) {
         print(a);
         erledigt = true;
     }
     while(a >= b && !erledigt) {
         print(c);
         erledigt = true;
     }
     
  }
 
  void print(int i) {
    print("" + i);   }

  void print(String s) {
      System.out.println(s);  }
  
  int input(String meldung) {
    Scanner sc = new Scanner(System.in);
    print("Geben Sie " + meldung + " ein:");
    return sc.nextInt(); }

} // end of class Selektionslos
                
let a = Math.floor((Math.random() * 10) + 1),
    b = Math.floor((Math.random() * 10) + 1),
    c = Math.floor((Math.random() * 10) + 1);

console.log('1:');
console.log(function() {
  while (a < b) return a;
  return c;
}());

console.log('2:');
console.log(function() {
  let aSmallerB = a < b;
  while (aSmallerB) return a;
  return c;
}());

console.log('3:');
console.log(function() {
  while(Math.max(a, b) == b) return a;
  return c;
}());

                

Lösung von: Lisa Salander (Heidi-Klum-Gymnasium Bottrop)

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

Zeit: 0.5
Schwierigkeit: k.A.
Webcode: c75n-hpo8
Autor: Philipp G. Freimann (BBW (Berufsbildungsschule Winterthur) https://www.bbw.ch)

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen