c# - Visual Studio C# 隐形朋友游戏代码
问题描述
我一直在尝试如何制作情侣,我要做的是开发一个程序(控制台应用程序),我必须
- 添加玩家(最多 30 人)
- 在那之间混合这些球员(每个名字必须在每列中出现一次
例如:
玛丽亚的搭档是胡安,所以玛丽亚要把礼物送给胡安,每个人都必须在两边出现一次。我试过2个代码:
代码 1:
Console.WriteLine("Bienvenido al juego del amigo invisible!");
string[] nombres= new string[30];
bool salir = false;
int personas=30 ;
int cant = 1;
while (salir != true)
{
int opc;
Console.WriteLine("1-Introducir parejas 2-Amigo invisible 3-Introducir parejas 4- Añadir más personas 5-Salir ");
while (!int.TryParse(Console.ReadLine(), out opc))
{
Console.Write("Valor incorrecto, inserta de nuevo: ");
}
switch (opc)
{
case 1:
Nombres(ref nombres, ref personas);
break;
case 2:
Parejas(nombres,ref personas);
break;
case 3:
Parejitas(cant);
break;
case 4:
MasParejas(ref personas);
break;
case 5:
salir = true;
break;
}
}
static void Nombres(ref string[] nombres, ref int personas)
{
Console.WriteLine("Cuantas personas sois");
while (!int.TryParse(Console.ReadLine(), out personas))
{
Console.Write("Valor incorrecto, inserta de nuevo: ");
}
nombres = new string[personas];
Console.WriteLine("introduzca" + personas + "nombres");
for (int a = 0; a < personas; ++a)
{
nombres[a] = Console.ReadLine();
}
for (int a = 0; a >= personas;)
{
Console.WriteLine("Las personas que jugaran contigo serán:" + nombres[a]);
}
Console.Clear();
}
static void Parejas(string[] nombres, ref int personas)
{
Random rnd = new Random();
bool aprobado = false;
int[] usados = new int[personas];
Console.WriteLine("Hagamos las parjeas");
foreach (string persons in nombres)
{
do
{
for (int i = 0; i < personas; i++)
{
int h = rnd.Next(0, personas - 1);
do
{
string name = nombres[h];
if (name != null)
{
if (persons?.CompareTo(name) != 0 && usados.Contains(h))
{
aprobado = true;
Console.WriteLine($"{nombres[i]} va con {nombres[h]}");
}
}
usados[i] = h;
} while ( usados.Contains(h));
}
} while (!aprobado);
}
}
static void Parejitas(int cant)
{
Console.WriteLine("Hagamos las parejas por separado.");
string[] parejitas = new string[cant];
do
{
if (cant % 2 != 0)
{
while (!int.TryParse(Console.ReadLine(), out cant))
{
Console.Write("Valor incorrecto, inserta de nuevo: ");
}
}
} while (cant % 2 != 0);
Console.WriteLine("Introduzca los nombres de las parejas");
for (int i = 0; i < cant; i++)
{
parejitas[i] = Console.ReadLine();
}
for (int i = 0; i < cant; i++)
{
Console.WriteLine($"las parejas son {parejitas[i]} con {parejitas[i + 1]}");
}
Console.Clear();
}
static void MasParejas(ref int personas)
{
int más;
Console.WriteLine("¿Cuanta gente quiere añadir?");
while (!int.TryParse(Console.ReadLine(), out más))
{
Console.Write("Valor incorrecto, inserta de nuevo: ");
}
int total = personas + más;
do
{
if (total >= 30 || más % 2 != 0)
{
Console.WriteLine("Ha superado el limite de personas o introducido un número inpar, el limite es 30");
while (!int.TryParse(Console.ReadLine(), out más))
{
Console.Write("Valor incorrecto, inserta de nuevo: ");
}
}
} while (total >= 30 || más % 2 != 0);
int a = 1;
string[] personasañadidas = new string[más];
Console.WriteLine("Introduzca los nombres de los nuevos");
for (int i = 0; i < más; i++)
{
personasañadidas[i] = Console.ReadLine();
}
for (int j = 0;j < personasañadidas.Length; j++)
{
if (j< personasañadidas.Length-1)
{
Console.WriteLine($" Parejas: {personasañadidas[j]} con {personasañadidas[j + 1]}");
}
if (j==personasañadidas.Length-1)
{
Console.WriteLine($" Parejas: {personasañadidas[j]} con {personasañadidas[0]}");
}
}
}
代码 2:
static void Main(string[] args)
{
bool salir = false;
int cantidadPersonas=30 ;
string[] Personas = new string[cantidadPersonas];
while (salir != true)
{
int opc;
Console.WriteLine("1-Introducir parejas 2-Amigo invisible 3-Salir ");
while (!int.TryParse(Console.ReadLine(), out opc))
{
Console.Write("Valor incorrecto, inserta de nuevo: ");
}
switch (opc)
{
case 1:
MeterNombres(ref cantidadPersonas, ref Personas);
break;
case 2:
GenerarParejas(cantidadPersonas, Personas);
break;
case 3:
salir = true;
break;
}
}
static void MeterNombres(ref int cantidadPersonas, ref string[] Personas) {
Console.WriteLine("¿Cuantos sois?");
cantidadPersonas = Convert.ToInt32(Console.ReadLine());
do
{
if (cantidadPersonas % 2 != 0)
{
cantidadPersonas = Convert.ToInt32(Console.ReadLine());
}
} while (cantidadPersonas % 2 != 0);
Console.WriteLine("Introduzca los nombres de las personas que vayan a participar:");
for (int i = 0; i < cantidadPersonas; i++)
{
Personas[i]= Console.ReadLine();
}
Console.Clear();
Console.WriteLine("Los participantes son:");
for (int i = 0; i < cantidadPersonas; i++)
{
Console.WriteLine(Personas[i]);
}
}
static void GenerarParejas(int cantidadPersonas, string[] Personas)
{
Console.WriteLine("Comencemos a generar las parejas...");
string[] PersonasCopia = new string[cantidadPersonas];
Random rnd = new Random();
bool Repeticiones = false;
int NumeroAleatorio;
for (int i = 0; i < cantidadPersonas; i++)
{
PersonasCopia[i] = Personas[i];
}
for (int i = 0; i < cantidadPersonas; i++)
{
do
{
Repeticiones = false;
NumeroAleatorio = rnd.Next(0, cantidadPersonas);
if (Personas[i] == PersonasCopia[i])
{
Repeticiones = true;
}
} while (Repeticiones = false); ;
}
}
解决方案
推荐阅读
- javascript - 使用@public JsDoc 标记和@export 会导致警告
- sql - 选择替换其他列值的值
- excel - 遍历列表并为每个项目运行代码(VBA)
- wordpress - 使用 ACF 创建新的自定义帖子类型时更新帖子标题
- qlikview - Qlikview 滚动 52 周报告
- php - 在 POST 期间无限制地发送大量变量
- javascript - 选择页面上具有相同外观的项目扩展脚本
- javascript - 在网络表单上实施自动完成建议
- maven - 具有多个 wsdl 文件的相对 wsdlLocation
- android - WorkManager - 可以更改重试的 WorkRequest 最大退避时间吗?