Solved

C# Calling Random Variables

Posted on 2012-04-07
6
516 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

759 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

20 Experts available now in Live!

Get 1:1 Help Now