Friday 4 July 2014

Random number in C#

Write a C# program to allow a user to guess a number( from 1 to 6) that will be randomly generated by computer.

The user is asked to input his/her number. Then the number will be compared with the random number. See the example below:
Enter your number: 2 
You lost.
My number is: 4

Solution:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{


class Program
{


  static void Main(string[] args)
  {

      ranguess();

     Console.ReadLine();

  }

   public static void ranguess()
  {
        int yn, rn; 
        Random rd = new Random(); //create random object
    
        Console.Write("Enter your guess number:");
        yn = int.Parse(Console.ReadLine());
        rn =rd.Next(1,7);//generate random number from 1 t 6
        Console.WriteLine(rn);
        if (yn == rn)
        {
          Console.WriteLine("You won.");

        }
      else
        {
           Console.WriteLine("You lost.");
           Console.WriteLine("My number is {0}.", rn);

        }


  }
 }

}

1 comment:

  1. Poker Bonus Codes - Jtm Hub
    We have a 충청남도 출장마사지 lot 하남 출장샵 of different poker 김해 출장샵 bonuses, which are based 울산광역 출장샵 on multiple different categories, but we have all the poker bonuses available. The 보령 출장안마 reason? Because

    ReplyDelete