Deehz
15-12-2013, 12:32
Premetto dicendo che ho pochissima esperienza in programmazione e fino ad ora ho fatto solo piccoli problemi. Comunque, stavo facendo una programma che simulava il gioco della morra cinese, e mi chiedevo come potessi fare due cicli: uno che chiede , alla fine di ogni partita, se si vuole rigiocare, e un altro che dopo aver verificato che il valore immesso dall'utente sia sbagliato, gli permetta di reinserirne il valore.
Questo è quello che ho fatto fin'ora:
namespace Morra_Cinese
{
class Program
{
static void Main(string[] args)
{
string nome_utente, scelta_utente, scelta_computer_int;
int scelta_computer;
Console.WriteLine("Inserisci il tuo nome: ");
nome_utente = Console.ReadLine();
//SCELTA UTENTE
Console.WriteLine(nome_utente + " fai la tua scelta: ");
scelta_utente = Console.ReadLine();
//VERIFICA ERRORI
if (scelta_utente != "carta" && scelta_utente != "forbice" && scelta_utente != "sasso")
{
Console.WriteLine("Hai inserito un valore non valido");
Console.ReadLine();
return;
}
else
{
//SCELTA COMPUTER
Random gen = new Random();
scelta_computer = gen.Next(1, 4);
switch (scelta_computer)
{
case 1: scelta_computer_int = Convert.ToString("sasso");
break;
case 2: scelta_computer_int = Convert.ToString("forbice");
break;
default: scelta_computer_int = Convert.ToString("carta");
break;
}
//RISULTATI
if (scelta_utente == "sasso" && scelta_computer_int == "sasso" || scelta_utente == "forbice" && scelta_computer_int == "forbice" || scelta_utente == "carta" && scelta_computer_int == "carta")
Console.WriteLine("Il computer ha giocato " + scelta_computer_int + ". Parità!");
else if (scelta_utente == "carta" && scelta_computer_int == "sasso" || scelta_utente == "sasso" && scelta_computer_int == "forbice" || scelta_utente == "forbice" && scelta_computer_int == "carta")
Console.WriteLine("Il computer ha giocato " + scelta_computer_int + ". Hai vinto!");
else if (scelta_utente == "forbici" && scelta_computer_int == "sasso" || scelta_utente == "carta" && scelta_computer_int == "forbice" || scelta_utente == "sasso" && scelta_computer_int == "carta")
Console.WriteLine("Il computer ha giocato " + scelta_computer_int + ". Hai perso!");
}
Console.ReadLine();
}
}
}
Potete darmi una mano?
Questo è quello che ho fatto fin'ora:
namespace Morra_Cinese
{
class Program
{
static void Main(string[] args)
{
string nome_utente, scelta_utente, scelta_computer_int;
int scelta_computer;
Console.WriteLine("Inserisci il tuo nome: ");
nome_utente = Console.ReadLine();
//SCELTA UTENTE
Console.WriteLine(nome_utente + " fai la tua scelta: ");
scelta_utente = Console.ReadLine();
//VERIFICA ERRORI
if (scelta_utente != "carta" && scelta_utente != "forbice" && scelta_utente != "sasso")
{
Console.WriteLine("Hai inserito un valore non valido");
Console.ReadLine();
return;
}
else
{
//SCELTA COMPUTER
Random gen = new Random();
scelta_computer = gen.Next(1, 4);
switch (scelta_computer)
{
case 1: scelta_computer_int = Convert.ToString("sasso");
break;
case 2: scelta_computer_int = Convert.ToString("forbice");
break;
default: scelta_computer_int = Convert.ToString("carta");
break;
}
//RISULTATI
if (scelta_utente == "sasso" && scelta_computer_int == "sasso" || scelta_utente == "forbice" && scelta_computer_int == "forbice" || scelta_utente == "carta" && scelta_computer_int == "carta")
Console.WriteLine("Il computer ha giocato " + scelta_computer_int + ". Parità!");
else if (scelta_utente == "carta" && scelta_computer_int == "sasso" || scelta_utente == "sasso" && scelta_computer_int == "forbice" || scelta_utente == "forbice" && scelta_computer_int == "carta")
Console.WriteLine("Il computer ha giocato " + scelta_computer_int + ". Hai vinto!");
else if (scelta_utente == "forbici" && scelta_computer_int == "sasso" || scelta_utente == "carta" && scelta_computer_int == "forbice" || scelta_utente == "sasso" && scelta_computer_int == "carta")
Console.WriteLine("Il computer ha giocato " + scelta_computer_int + ". Hai perso!");
}
Console.ReadLine();
}
}
}
Potete darmi una mano?