Mat*_*ind 1 .net c# arrays random foreach
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace lotto
{
class Program
{
static void Main(string[] args)
{
char k = 'l';
while (!(k == 'k'))
{
Random rnd = new Random();
int[] tablica = new int[6];
for (int i = 0; i < 6; i++)
{
tablica[i] = 0;
}
for (int i = 0, z; i < 6; i++)
{
{
z = rnd.Next(1, 49);
while (tablica.Contains(z))
{
z = rnd.Next(1, 49);
}
tablica[i] = z;
}
}
Array.Sort(tablica);
foreach (int q in tablica)
{
Console.Write(q);
Console.Write(", ");
}
k = Convert.ToChar(Console.Read()) ;
Console.WriteLine("\n\n\n");
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它工作正常.当我使用逐步点击(视觉工作室中的F10)时,它运行正常.但是,当我正常运行它,然后
k=Convert.ToChar(Console.Read());
Run Code Online (Sandbox Code Playgroud)
当我提供'k'时,程序会按预期停止.当我什么都不提供的时候,就是这样
foreach (int q in tablica)
{
Console.Write(q);
Console.Write(", ");
}
k = Convert.ToChar(Console.Read()) ;
Console.WriteLine("\n\n\n");
Run Code Online (Sandbox Code Playgroud)
两次,当我提供"k"以外的任何东西时,它会做三次.搞什么鬼.
Console.Read reads a single character at a time from the input stream (doc). When you press enter you are supplying two characters: \r then \n, so there are two characters to read before pausing for further user input.
I'm confused how it ran fine when you used F10 as I did this to see what was looping, I just so happened to use Enter when supplying "nothing" and saw the characters pop through.