-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomChoice.cs
More file actions
79 lines (62 loc) · 2.02 KB
/
RandomChoice.cs
File metadata and controls
79 lines (62 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
// Author: Jonathan Scholl
// Date: 10/19/2021
// Project: Random Choice
// Description: Add random objects to list and choose at random based on how many selections you want from list
*/
using System;
using System.Collections.Generic;
namespace RandomChoiceGenerator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Create a choice list");
List<string> choiceList = createChoiceList();
printChoiceList(choiceList);
listRandomizer(choiceList);
}
public static List<string> createChoiceList()
{
List<string> choices = new List<string>();
bool addItem = true;
string item = "";
while (addItem == true)
{
string anotherItem = "";
Console.WriteLine("Add item to the list: ");
item = Console.ReadLine();
choices.Add(item);
Console.WriteLine("Add another item? (Y/N)");
anotherItem = Console.ReadLine().ToUpper();
if (anotherItem == "Y")
{
addItem = true;
}
else if (anotherItem == "N")
{
addItem = false;
}
}
return choices;
}
public static void printChoiceList(List<string> list)
{
Console.Write("List: " + String.Join(", ", list));
Console.WriteLine("\n");
}
public static void listRandomizer(List<string> list)
{
var random = new Random();
var index = random.Next(list.Count);
Console.Write("\nHow many items from the list do you want: ");
int numChoices = int.Parse(Console.ReadLine());
while (list.Count != numChoices)
{
list.Remove(list[index]);
}
list.Sort();
printChoiceList(list);
}
}