Buch Cover Buch Cover Buch Cover Buch Cover

Web-Code: - Webcode Help

FizzBuzz (2) (Schleifen)

Diese Version von FizzBuzz soll wieder alle Zahlen von 1 bis 100 ausgeben. Bei jeder Zahl, die eine Ziffer 7 enthält, soll "fizz" ausgegeben werden. Wenn die Zahl durch 7 teilbar ist, soll "buzz" ausgegeben werden. Wenn die Zahl sowohl durch 7 teilbar ist als auch eine Ziffer 7 enthält, soll "fizzbuzz" ausgegeben werden.

0 Kommentare

Bitte melde dich an um einen Kommentar abzugeben

4 Lösung(en)

/**
 * @author Philipp Gressly (phi@gressly.ch)
 */
public class FizzBuzz2 {
  public static void main(String[] args) {
    new FizzBuzz2().top();}

  void top() {
    int i = 1;
    while(i <= 100) {
        ausruf(i);
        i = i + 1;}
  }


  void ausruf(int zahl) {
      if(enthaeltEine7(zahl)) {
          System.out.print("fizz");
      }
      if(durch7Teilbar(zahl)) {
          System.out.print("buzz");
      }
      if(! enthaeltEine7(zahl) && !durch7Teilbar(zahl)) {
          System.out.print(zahl);
      }
      System.out.println();
  }

  boolean enthaeltEine7(int zahl) {
    while(zahl > 1) {
        if(letzteZifferIst7(zahl)) {
            return true;
        }
        zahl = zahl / 10;
    }
    return false;
  }
  
  boolean letzteZifferIst7(int zahl) {
    return 7 == zahl % 10;
  }

  boolean durch7Teilbar(int zahl) {
    return zahl / 7 * 7 == zahl;
  }
  
} // end of class FizzBuzz
                
package fizzBuzz;
/**
 * @author Endrit Haliti
 */
public class FizzBuzz {

	public static void main(String[] args) {
		new FizzBuzz().top();

	}
	void top() {
		/* Deklarationen */
		int i = 1; /* Zahlen */
		String iString = i + "";
		/* Iteration Zahlen 1-100 */
		while (i <= 100) {
			iString = i + "";
			if (i < 10) {
				if (iString.charAt(0) == '7') {
					System.out.println(i + " fizzbuzz");
				} else {
					System.out.println(i);
				}
			} else if (iString.charAt(0) == '7' || iString.charAt(1) == '7') {
				if (i % 7 == 0) {
					System.out.println(i + " fizzbuzz");
				} else {
					System.out.println(i + " fizz");
				}
			} else if (i % 7 == 0 && iString.charAt(0) != 7
					&& iString.charAt(1) != 7) {
				System.out.println(i + " buzz");
			} else {
				System.out.println(i);
			}
			i++;
		}
	}
}
                

Lösung von: Endrit Haliti (-)

function fizzbuzz2 (num) {
  function contains7 (num) {
    return num.toString().indexOf('7') != -1;
  }
  function divisibleBy7 (num) {
    return num % 7 == 0;
  }
  if (contains7(num) && divisibleBy7(num)) return 'fizzbuzz';
  if (contains7(num)) return 'fizz';
  if (divisibleBy7(num)) return 'buzz';
  return num;
}

let results = [];
for (let i = 1; i <= 100; i++) results.push(fizzbuzz2(i));
document.write(results.join(', '));                          // lissalanda@gmx.at
                

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

// NET Core 3.x

using System;
using System.Linq;

namespace CS_MDL_CORE_FizzBuzz2
{
    class Program
    {
        static void Main(string[] args)
        {
            Enumerable.Range(1, 100).Select(x => FizzBuzz2(x)).ToList().ForEach(Console.WriteLine);
            static string FizzBuzz2(int n) => (n.ToString().Contains("7") ? 0 : 1, n % 7) switch { (0, 0) => "FizzBuzz", (0, _) => "Fizz", (_, 0) => "Buzz", (_, _) => n.ToString() };
        }
    }
}
                

Lösung von: Jens Kelm (@JKooP)

Verifikation/Checksumme:

Bereits bei der Zahl sieben erscheint das erste mal "fizzbuzz".

Aktionen

Bewertung

Durchschnittliche Bewertung:

Eigene Bewertung:
Bitte zuerst anmelden

Meta

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

Download PDF

Download ZIP

Zu Aufgabenblatt hinzufügen