Advertisement
mmayoub

Ex01, 28.06.2021

Jun 28th, 2021
1,183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace ConsoleApp2
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Random rnd = new Random();
  10.  
  11.             int x; // random number
  12.             int sum = 0; // sum of the random numbers
  13.             int c = 0; // number of trials
  14.  
  15.  
  16.             // stop the game if trials>=15 or sum>=30
  17.             while (sum < 30 && c < 15)
  18.             {
  19.                 // get another random number
  20.                 x = rnd.Next(1, 7);
  21.                 // increase the counter
  22.                 c = c + 1;
  23.                 Console.WriteLine(x);
  24.  
  25.                 // if x==4 start sum from zero
  26.                 if (x == 4)
  27.                 {
  28.                     sum = 0;
  29.                 }
  30.                 else
  31.                 {
  32.                     // if not 4 add it to sum
  33.                     sum = sum + x;   //sum += x;
  34.                 }
  35.                 Console.WriteLine("The sum is {0}", sum);
  36.             }
  37.  
  38.             // if the game stopped, check if winner or loser
  39.             if (sum < 30)
  40.             {
  41.                 Console.WriteLine("No more! you lose");
  42.             }
  43.             else
  44.             {
  45.                 Console.WriteLine("You Win !!!");
  46.             }
  47.         }
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement