繁体   English   中英

我的C#程序怎么了?

[英]Whats wrong with my C# program?

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

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]);
            }
        }
    }
}

您可以使用

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]变量。

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

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

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

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

你不见了

Console.ReadLine();

Console.WriteLine();

正如其他人提到的那样,要使该行与{0} ,您需要将其格式化为字符串,

Console.WriteLine(string.Format("The array value is: {0}", array[i]));
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();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication36
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("asd");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM