Solved

C# Calling Random Variables

Posted on 2012-04-07
6
521 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you haven’t already, I encourage you to read the first article (http://www.experts-exchange.com/articles/18680/An-Introduction-to-R-Programming-and-R-Studio.html) in my series to gain a basic foundation of R and R Studio.  You will also find the …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

739 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