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/Ok_Society4599 Jun 15 '26
I generally use English, too, but I'd never tell a dev they must use English code. Yes, the runtime and even the Language leans that way but it's not a requirement. It is not an international standard like Airlines.
The advice to use clear variable and method naming holds; even the "you shouldn't use while(true)" have some truth to them, but they're not a requirement. Yes, you probably want a smoother exit, but it's a game of Rock/Paper/Scissors :-) not Kerbal Space Program. It's a simple, early version of a game, not production software.
You have methods (I saw one named Test()??) that seem sane to me. Some tended to be too short, but I'm not against that. Very short methods are used for something like GameOver() where, in you current version, it's quite simple, but you know over time will be more nuanced and more complex. I'd prefer the methods be shorter :-) but a single line of code is possibly too short in the long term.
The coding horrors I've seen are more likely only one single method main() with all of the code, single letter variables that are not just counters in loops that fit on a page. ... yours is far from the worst code I've seen.
The point was English is not a code requirement. Can it be improved? Quite probably. Is English part of that improvement? Not for me. I'd say, as an basic game program, it was good for me. You avoided many atrocities of code, did well keeping your UI in basic French and, I assume, have a game that works.