-4

How can I make a program that will pick one random string from given strings like this:

int x;
x = Random.Range(0,2);
string[] Quest0 = {"You","Are","How","Hello"};
string[] Quest1 = {"Hey","Hi","Why","Yes"};
string[] Quest2 = {"Here","Answer","One","Pick"};

I would like to print out like this: if x = 2 it would print out Quest2 and so on.

Thank you!

User 12345678
  • 7,714
  • 2
  • 28
  • 46
divjad---
  • 15
  • 7

2 Answers2

0

Fistly you need to declare a random variable.

Random random = new Random();

this will create a variable in which you can now get random numbers from. to get random numbers you will use random.next(x,y) or in your case random.next(0,3) because the final argument is exclusive, so if you want 0, 1 or 2, you must use (0,3).

you then need to make some conditional statments, i would use If statments, to accomplish your goal use something like this:

 if (x == 2) 
        {
            foreach (string s in Quest2)
            {
                Console.WriteLine(s);
            }
        }

Do this for each possible outcome and it will print out all of the values in your array of strings. Hope I have been helpful, thanks.

Also if you new become familiar with these links:

http://msdn.microsoft.com/en-us/library/system.random%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-gb/library/aa288453%28v=vs.71%29.aspx

Needham
  • 457
  • 1
  • 6
  • 15
0
List<String[]> quests = new ArrayList<String[]>();
quests.add(0, new string[]{"You","Are","How","Hello"});
quests.add(1, new string[]{"Hey","Hi","Why","Yes"});
quests.add(2, new string[]{"Here","Answer","One","Pick"});
int x = new Random().nextInt((2 - 0) + 1);
System.out.println(quests.get(x).toString());
Morad
  • 2,761
  • 21
  • 29