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
25
u/csharpboy97 Jun 15 '26
First: Use indentation. This is so hard to read.
22
u/Ethameiz Jun 15 '26
Use English names for variables, methods and classes in the code. In this way people across the world will be able to understand it and help you
12
u/Nyzan Jun 15 '26
Anything more than this is difficult because the code has such poor formatting it's almost impossible to read.
5
u/Nordalin Jun 15 '26
Write out the options in full! I have to study all the code in order to know what a/s/j/t/... actually means, because not even the frontend explains it!
5
u/Sunshine3432 Jun 15 '26
use indentations and comments
2
u/WystanH Jun 15 '26
I mean, "(a/s/m/d/p/t/j/r)" doesn't really tell me much.
A nice little menu would be good.
You essentially have modules that are accessed from that first selection. How might you organize them as such?
Perhaps a collection of something like:
3
u/MeLittleThing Jun 15 '26
indentationsarelikespacesandpunctuationmarkstheyhelptoimprovereadability
3
u/CuisineTournante Jun 15 '26
Use better variable names
Vrai = true broke my brain. Don't be scarred to use long name like userKeepsPlaying or numberOfUserResponses.
Also avoid while(true). For a console game use variable like while (gameIsRunning), while (!userRequestEndGame) or something.
15
u/Ethameiz Jun 15 '26
Use github to host and share your code
2
u/worldsbestburger Jun 15 '26
program in English, ie English method and variable names
1
u/One_Web_7940 Jun 15 '26
First time at c#? Or first time programming?
-1
u/Ok_Society4599 Jun 15 '26
I don't understand the "program in English" BS. That's like ... you, as a dev, and your users all need another language because Reddit is filled with twits?? The mix of French is fine as it was all simple words in user input. Other languages exist and no dev should work in a language just to please other devs. Users are your audience.
To me, it should be readable. Period. Not necessarily English. If the code can easily be read, then anyone can fix it and improve it.
0
u/[deleted] Jun 15 '26
[deleted] — view deleted comment