Solved

C# Calling Random Variables

Posted on 2012-04-07
6
517 Views
Last Modified: 2012-04-07
Need a extra pair of eyes... What am I doing wrong with comparing random numbers and the input string


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace test
{
    public class Program
    {
        static void Main(string[] args)
        {
            string inputString;
            string r, p, c;
            int randomNumber;
            string numberString;

            Random ranNumberGenerator = new Random();
            randomNumber = ranNumberGenerator.Next(4);


            Console.Write("Enter r, p, or c");
            inputString = Console.ReadLine();
            numberString = Convert.ToInt32(inputString);
            randomNumber =  + ranNumberGenerator.Next(4);    

           if (inputString == randomNumber)
               Console.WriteLine("Draw");
           else Console.WriteLine("Lose");
           Console.Read();
0
Comment
Question by:December2000
  • 3
  • 3
6 Comments
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37819820
randomNumber is an integer and inputString a string, here is the modified version:-

string inputString;
            string r, p, c;
            int randomNumber;
            int numberString;

            Random ranNumberGenerator = new Random();
            randomNumber = ranNumberGenerator.Next(4);


            Console.Write("Enter r, p, or c");
            inputString = Console.ReadLine();
            numberString = Convert.ToInt32(inputString);
            randomNumber = +ranNumberGenerator.Next(4);

            if (numberString == randomNumber)
                Console.WriteLine("Draw");
            else Console.WriteLine("Lose");
            

Open in new window

0
 

Author Comment

by:December2000
ID: 37819980
Thank you!

I am getting an error on

 numberString = Convert.ToInt32(inputString);

say's "Input string was not in a correct format."
0
 
LVL 20

Expert Comment

by:BuggyCoder
ID: 37819986
you must be entering a string in console...
use int.tryparse to check if integer is added or not....
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:December2000
ID: 37820030
Thanks,  as obvious I am a newbie, When I use number it works,  How do I use the int.tryparse? How would I compare r to random generated  r, p, s ? I am having a datatype issue
0
 
LVL 20

Accepted Solution

by:
BuggyCoder earned 500 total points
ID: 37820045
Random class generates Random Number, here is a random character generator to help you:-

static class RandomLetter
    {
        static Random _random = new Random();
        public static char GetLetter()
        {
            // This method returns a random lowercase letter.
            // ... Between 'a' and 'z' inclusize.
            int num = _random.Next(0, 26); // Zero to 25
            char let = (char)('a' + num);
            return let;
        }
    }

Open in new window


Just Call RandomLetter.GetLetter() to get your random letter....

Reference:
http://www.dotnetperls.com/random-lowercase-letter

Then you can have your input string and compare it directly with the user input(assuming that user enters a single character only)
0
 

Author Closing Comment

by:December2000
ID: 37820056
Awesome still not there but, I am a lot further than I was... sorta stuck because the only characters that the computer should be able to choose from is r, p, s (it is a rock paper scissor game) ... However, you have more than earned the points already... thank you :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now