0

我需要创建一个用随机数填充数组的应用程序。我编写了整个代码并且它可以工作,但是数字没有显示在控制台窗口中。代码可能有什么问题?谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Arrays
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[10]; 
            Random randomNumbers = new Random();

            for (int i = 0; i < array.Length; i++)
            {
                int randomValue = randomNumbers.Next(0,500);
                array[i] = randomValue;
            }

            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine("The array value is: ", array[i]);
            }
        }
    }
}
4

7 回答 7

6

您可以使用

Console.WriteLine("The array value is: {0}", array[i]);

或者

Console.WriteLine("The array value is: " + array[i]);

但你写的,

Console.WriteLine("The array value is: ", array[i]);

完全没有告诉控制台你在哪里以及如何使用 array[i] 变量。

于 2013-10-20T22:12:35.600 回答
2
Console.WriteLine("The array value is: {0}", array[i]);

您必须在格式字符串中指出要打印一些值。

于 2013-10-20T22:12:39.973 回答
1
//Console.WriteLine("The array value is: ", array[i]);
  Console.WriteLine("The array value is: {0}", array[i]);
于 2013-10-20T22:12:33.487 回答
1

Console.WriteLine("数组值为:{0}", array[i]);

于 2013-10-21T01:48:53.560 回答
0

你不见了

Console.ReadLine();

Console.WriteLine();

正如其他人所提到的,要让这条线使用{0},您需要将其格式化为字符串,

Console.WriteLine(string.Format("The array value is: {0}", array[i]));
于 2013-10-20T22:27:11.257 回答
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Coefficient Of x (Positive): ");
            int coef = Convert.ToInt32(Console.ReadLine());
            Console.Write("Enter Value Of c: ");
            int c = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("From? (Positive)");
            int from = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("To? (Positive)");
            int to = Convert.ToInt32(Console.ReadLine());
            Console.Clear();
            int[] y_val = new int[(to - from) + 1];
            int counter = 0;
            for (int i = to; i >= from; i--)
            {
                y_val[counter] = ((coef * i) + c);
                counter++;
            }
            int x = 0;
            for (int i = to; i >= from; i--)
            {
                if (y_val[x] == ((coef * i) + c))
                {
                    if (y_val[x] >= 10)
                    {
                        Console.WriteLine("{0}|", y_val[x]);
                    }
                    else
                    {
                        Console.WriteLine("0{0}|", y_val[x]);
                    }
                    for (int j = 0; j < i; j++)
                    {
                        Console.Write("  ");
                    }
                    Console.Write("*");
                    Console.WriteLine();

                }
                x++;
            }
            Console.WriteLine("______________________________________________________________");
            Console.WriteLine("0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23");
            Console.ReadKey();
        }
    }
}
于 2014-12-23T06:58:42.593 回答
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication36
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("asd");
        }
    }
}
于 2014-12-23T07:00:42.760 回答