mardi 11 février 2020

How can select multiple values of an array of random numbers

I am creating a dice program to further my learning. The program will roll 5 dice and assign random number values to an array of 5 and print out the values. After that I am wanting the player to select the die or dice that he or she would like to keep, display those values and later I will create a method to roll the remaining dice.

I am having a problem with the selecting multiple dice part.

This is what I have so far:

    using System;


class HelloWorld {

  static void Main() {

    Random random = new Random();

    int[] diceEach = new int[5];


    int diceCount = 1;

    for(int i = 0; i < 5; i++)
    {
        diceEach[i] = random.Next(1, 7);
        Console.WriteLine("Dice " + diceCount +": " + diceEach[i]);
        diceCount++;
    }
    Console.WriteLine("To roll again hit R");

    Console.WriteLine("Please type the dice numbers that you would like to keep...");
    string diceKept = Console.ReadLine();

        if(diceKept == "1")
        {
            Console.Write(diceEach[0]);
        }

        else if(diceKept == "1")
        {
            Console.WriteLine(diceEach[1]);
        }

        else if(diceKept == "3")
        {
            Console.WriteLine(diceEach[2]);
        }

        else if(diceKept == "4")
        {
            Console.WriteLine(diceEach[3]);
        }

        else if(diceKept == "5")
        {
            Console.WriteLine(diceEach[4]);
        }

        else if(diceKept == "r")
        {
            Console.WriteLine("You have chosen to roll again");
        }


    Console.ReadLine();

  }

}

I have it currently printing out one dice value that you select but I wasn't sure how to print out multiple selections. The only way I can think of is typing out all the options. But that doesn't seem right and would make my code so long.

I was thinking some sort of a loop might work? But I can see how.

This is my first time posting here so hopefully I did it right. Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire