[英]Whats wrong with my C# program?
I need to create an application that fills an array with random numbers. 我需要创建一个使用随机数填充数组的应用程序。 I written the whole code and it works but the numbers are not displaying in the console window. 我编写了整个代码,并且可以正常工作,但数字未显示在控制台窗口中。 What could be wrong with the code? 代码有什么问题? Thanks 谢谢
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]);
}
}
}
}
You can either use 您可以使用
Console.WriteLine("The array value is: {0}", array[i]);
or 要么
Console.WriteLine("The array value is: " + array[i]);
but what you wrote, 但是你写的
Console.WriteLine("The array value is: ", array[i]);
entirely misses on telling the console where and how you use the array[i] variable. 完全错过了告诉控制台在哪里以及如何使用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]);
You are missing 你不见了
Console.ReadLine();
After Console.WriteLine();
在Console.WriteLine();
As others have mentioned, to get this line to work with {0}
, you'd need to format it as a string, 正如其他人提到的那样,要使该行与{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.