C# :: Aufgabe #275 :: Lösung #3

8 Lösungen Lösungen öffentlich
#275

Glücksspiel Zufällige Zahl 0-9

Fortgeschrittener - C# von re_mas - 27.11.2019 um 18:30 Uhr
Die Aufgabenstellung ist wie folgt:
- Glücksspiel bei der eine random Zahl zwischen 0 - 9 erzeugt werden soll.
- Der Spieler hat ein Startkonto von 10.000 Punkten und kann damit einen beliebigen Teilbetrag auf die zufällig erzeugte Zahl setzen.
- Liegt er richtig bekommt er das 9 Fache seines Einsatzes als Gewinn
- Programmieren Sie ein entsprechendes Programm, welches die Eingaben von der Tastatur einliest und
die Ausgaben auf dem Bildschirm liefert. Die zu erratende Zahl kann durch einen verfügbaren Zufallsgenerator gezogen werden.
#3
vote_ok
von Exception (7090 Punkte) - 17.12.2019 um 19:58 Uhr
Quellcode ausblenden C#-Code
using System;

class MainClass 
{
  private static int score;
  private static int rounds;

  public static void Main (string[] args) 
  {
    score     =  10000;
    rounds    =  0;

    Random rand = new Random();

    do 
    {
      updateUserInterface();

      try
      {
        int b, g, r = rand.Next(0, 10);

        Console.Write("\nYour bet        : ");
        if(!Int32.TryParse(Console.ReadLine(), out b))
        {
          throw new Exception("Invalid input for bet.");
        }

        if(b <= 0 && b > score)
        {
          throw new Exception("Bet invalid.");
        }

        Console.Write("Your guess      : ");
        
        if(!Int32.TryParse(Console.ReadLine(), out g))
        {
          throw new Exception("Invalid input for guess.");
        }

        Console.Write("\nNumber is       : {0}", r);

        if(g <= 0 && g >= 10)
        {
          throw new Exception("Guess invalid.");
        }

        updateScore(b, g, r);
        rounds++;
      }
      catch(Exception ex)
      {
        Console.WriteLine(ex.Message);
      }

      Console.ReadLine();
    }
    while(score > 0);
  }

  public static void updateUserInterface()
  {
    Console.Clear();
    Console.WriteLine("+---------------------------------------+");
    Console.WriteLine("| #275 - Glücksspiel Zufällige Zahl 0-9 |");
    Console.WriteLine("+---------------------------------------+");
    Console.WriteLine();
    Console.WriteLine("Rounds played   : {0}", rounds);
    Console.WriteLine("Score           : {0}", score);
  }

  public static void updateScore(int bet, int guess, int result)
  {
    if(guess == result)
    {
      score += bet * 9;
    }
    else 
    {
      score -= bet;
    }
  }
}


Konsolenausgabe:


+---------------------------------------+
| #275 - Glücksspiel Zufällige Zahl 0-9 |
+---------------------------------------+

Rounds played : 2
Score : 10450

Your bet : 25
Your guess : 3

Number is : 1

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben