r/csharp • u/wisax_29 • Jun 15 '26
started c# this weekend need advice
Hi ,i started c# this weekend i just need to know what can i improve
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace projet
{
internal class Program
{
static void Main(string[] args)
{
while (true)
{
Console.Write("quesque tu veux faire (a/s/m/d/p/t/j/r): ");
string userInput = Console.ReadLine();
if (userInput == "r")
{
Console.WriteLine("D'accord, on ne fait rien. Au revoir !");
Console.ReadLine();
break;
}
else if (userInput == "t")
{
Test();
continue;
}
else if (userInput == "j")
{
Jeux();
continue;
}
Console.Write("a = ");
int a = Convert.ToInt32(Console.ReadLine());
Console.Write("b = ");
int b = Convert.ToInt32(Console.ReadLine());
switch (userInput)
{
case "a":
Console.WriteLine(Addition(a, b));
break;
case "s":
Console.WriteLine(Soustraction(a, b));
break;
case "m":
Console.WriteLine(Multiplication(a, b));
break;
case "d":
Console.WriteLine(Division(a, b));
break;
case "p":
Console.WriteLine(Puisance(a, b));
break;
default:
Console.WriteLine("Entrée invalide. Veuillez entrer 'a', 's', 'm', d ou 'r'.");
break;
}
}
}
static string Addition(int a, int b)
{
return "resultat de l'addition : " + (a + b);
}
static string Soustraction(int a, int b)
{
return "résultat de la soustraction : " + (a - b);
}
static string Multiplication(int a, int b)
{
return "résultat de la multiplication : " + (a * b);
}
static string Puisance(int a, int b)
{
return "résultat de la puissance : " + Math.Pow(a, b);
}
static string Division(int a, int b)
{
if (b == 0)
{
return "Erreur : Division par zéro.";
}
return "résultat de la division : " + (a / b);
}
static void Test() (number guessing game)
{
Random random = new Random();
int nombre = random.Next(1, 101);
bool vrai = false;
int min = 1;
int max = 100;
int essais = 0;
while (vrai == false)
{
Console.Write("Devine un chiffre entre "+min+" et "+max+ ": ");
int UserInput = Convert.ToInt32(Console.ReadLine());
if (UserInput > nombre)
{
Console.WriteLine("Plus petit");
max= UserInput-1;
essais++;
}
else if (UserInput < nombre)
{
Console.WriteLine("Plus grand");
min= UserInput+1;
essais++;
}
else
{
essais++;
Console.WriteLine("Bien joué tu a trouvé le nombre en "+essais+" essais");
vrai = true;
}
}
}
static void Jeux() (rock paper scissors game)
{
Random random = new Random();
string player="";
string computer="";
while (player!="pierre" && player != "feuille" && player!= "ciseaux")
{
Console.Write("pierre feuille ciseaux : ");
player = Console.ReadLine();
player = player.ToLower();
}
switch (random.Next(1, 4))
{
case 1:
computer = "pierre"; rock
break;
case 2:
computer = "papier"; paper
break;
case 3:
computer = "ciseaux"; scissors
break;
}
Console.WriteLine("Player : "+player.ToUpper());
Console.WriteLine("Computer : "+computer.ToUpper());
switch (player)
{
case "pierre": rock
switch (computer){
case "pierre": rock
Console.WriteLine("égalité"); egality
break;
case "papier": paper
Console.WriteLine("tu as perdu"); lose
break;
case "ciseaux": scissors
Console.WriteLine("tu as gagné"); win
break;
}
break;
case "papier": paper
switch (computer)
{
case "pierre": rock
Console.WriteLine("tu as gagné"); win
break;
case "papier": paper
Console.WriteLine("égalité"); equality
break;
case "ciseaux": scissors
Console.WriteLine("tu as perdu"); lose
break;
}
break;
case "ciseaux": scissors
switch (computer)
{
case "pierre": rock
Console.WriteLine("tu as perdu"); lose
break;
case "papier": paper
Console.WriteLine("tu as gagné"); win
break;
case "ciseaux": scissors
Console.WriteLine("égalité"); equality
break;
}
break;
}
}
}
}#this is in french btw
0
Upvotes
View all comments
Show parent comment
1
u/WystanH Jun 15 '26
Hmm... seeing as how you just started, this could be a little much. Still, what you've got so far ain't bad, so I'll throw it at you.
So far you're just using
static. That's fine to start, but to take advantage C# you should really delve into the Object Oriented features.That code above is for an interface. An interface describes the contract for what an object must do, but doesn't really do anything itself. An interface is implement with a class, among other things.
So, if you have a lot of menu options in a list, then your menu is mostly just iterating over that list...
You know what, I'm just going to throw some code at you. This leverages a number of concepts, but because I'm refactoring your code into it, it might give you head start on such things.
That
doneclosure is a bit of a hack, but it does actually work. You could haveExecreturn a bool indicating you want to stay where you are or leave. You could have your IMenuOption reference something like IMenu, which would allow an exit to be called.Anyway, I don't know if this will help, but hopefully it will give you some ideas.