Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
107 views
in Technique[技术] by (71.8m points)

c# - Create random number and make an array from that number

I'm making a small project using c# for my class. The program is

  1. Ask user to enter an Integer value between 2 and 9. This value gives the maximum number of digits for random number I need to generate. For example, if the user enters a 7, then the maximum will be seven nines (9999999);

  2. Create a random number between 10 and that maximum value. So, if user enters a 5, then the random number will be in range (10,99999]

  3. After I created the random number, I need to create an array of digits for that number: for example, if the user enters a 3, then the random number is in (10,999]. Let's say it is 271. Then I need to generate an array of digits for 271, starting from right:

Array[0]=1
Array[1]=7
Array[2]=2

I tried to change the random number type to a string, so that I can determine the length of the array using that number:

Console.WriteLine("pleas enter value  between 2 and 9");
string s = Console.ReadLine();

int v = int.Parse(s);
int v2=99, v3=999, v4=9999, v5=99999, v6=999999, v7=9999999, v8=99999999, v9=999999999;
if(v==2)
{
    Console.WriteLine("Max number = " + v2);
    Random randomNumber = new Random();
    int number = randomNumber.Next(10, (++v2));
    Console.WriteLine("random number " + number);
    string sv =""+number;
    string[] arry1 = new string [sv.Length];
    Console.WriteLine("length array is :" + arry1.Length);
    Console.WriteLine("array elements:"+ arry1[1]);
}else
{
    if (v == 3)
    {
        Console.WriteLine("Max number = " + v3);
        Random randomNumber = new Random();
        int number = randomNumber.Next(10, (++v3));
        Console.WriteLine("random number " + number);
        string sv = "" + number;
        string[] arry1 = new string[sv.Length];
        Console.WriteLine("length array is :" + arry1.Length);
    }
    else
    {
        if(v==4)
        {
            Console.WriteLine("Max number = " + v4);
            Random randomNumber = new Random();
            int number = randomNumber.Next(10, (++v4));
            Console.WriteLine("random number " + number);
            string sv = "" + number;
            string[] arry1 = new string[sv.Length];
            Console.WriteLine("length array is :" + arry1.Length);
        }
        else ...

I am stuck on this step. How do I fill the array using the random number I have crated before?

This is how it should be:

user interaction

I, however, can't use any external functions or methods.

question from:https://stackoverflow.com/questions/65517646/create-random-number-and-make-an-array-from-that-number

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There are several ways to address what you're asking. Here are two, given the blocks you are using above.

Given:

int number = randomNumber.Next(10, (++v2));
string sv =""+number;
string[] arry1 = new string [sv.Length];

Option A:

for (int i = 0; i < sv.Length; i++)
{
    arry1[i] = sv[sv.Length - i - 1];
}

Option B:

for (int i = 0; i < sv.Length; i++)
{
    arry1[i] = number % 10;
    number /= 10;
}

I'll echo the previous commenter, there are prettier solutions to this all around. But this will get you where you want to go.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...