Solved

C# Calling Random Variables

Posted on 2012-04-07
6
522 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

729 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