Question Better way to code

Miroslav

New member
Joined
Dec 5, 2022
Messages
2
Programming Experience
Beginner
Hello every1. i did simple console app. on C# that excute code untill is done. its function but just dosent feel good. is there any other ways that i can make it look bvetter?

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    class Program
    {
        static void Main(string[] args)
        {
            int health = 100;

            int dmg ;

            
            Console.WriteLine("Hit the moster");

            while (health > 0) {

                dmg = Int32.Parse(Console.ReadLine());
                 int left = health - dmg;
                 if (left <= 0) goto end;

                 Console.WriteLine($"Monsterhas {left} life");
                
                again:

                if (left > 0)
                {

                    dmg = Int32.Parse(Console.ReadLine());

                    left = left - dmg;
                    if (left <= 0) goto end;
                    Console.WriteLine($"Monsterhas {left} life");

                    goto again;
                }
                else goto end;
              
                
            } end:

            Console.WriteLine("Monster has died!!");

        }       
            
    }
  
}
tnx.
 
Solution
Using top level statements:

C#:
int health = AskInt("How much health does the monsert have? Enter a whole number: ");

while (health > 0) {
    health -= AskInt("How much damage did you do?");
 
    if(health <= 0)
        Console.WriteLine("Monster has died!!");
    else
        Console.WriteLine($"Monster has {health} life left");
}

       


int AskInt(string question){
    while (true) {
        Console.WriteLine(question);

        if (int.TryParse(Console.ReadLine(), out int r))
            return r;
    }
}

AskInt is a method that asks for an integer and repeats the question if the user types garbage. Your logic has some superfluous elements; minimally we can get away with a loop that repeats while the monster has health...
Any code that uses goto is bad code. There's never a need to use that and doing so is a great way to make your code confusing, even to yourself. Your first goto should probably be a loop and the second one should probably use break.
 
Using top level statements:

C#:
int health = AskInt("How much health does the monsert have? Enter a whole number: ");

while (health > 0) {
    health -= AskInt("How much damage did you do?");
 
    if(health <= 0)
        Console.WriteLine("Monster has died!!");
    else
        Console.WriteLine($"Monster has {health} life left");
}

       


int AskInt(string question){
    while (true) {
        Console.WriteLine(question);

        if (int.TryParse(Console.ReadLine(), out int r))
            return r;
    }
}

AskInt is a method that asks for an integer and repeats the question if the user types garbage. Your logic has some superfluous elements; minimally we can get away with a loop that repeats while the monster has health. If it does not, then the loop ends after printing the died message and the program exits, otherwise it prints the life and asks for the damage again..

Echo jmc's sentiments about goto; it's very 1980s way of writing convoluted code and is usually entirely avoidable. I haven't used goto in a C# program in the last 20 years
 
Last edited:
Solution
Using top level statements:

C#:
int health = AskInt("How much health does the monsert have? Enter a whole number: ");

while (health > 0) {
    health -= AskInt("How much damage did you do?");
 
    if(health <= 0)
        Console.WriteLine("Monster has died!!");
    else
        Console.WriteLine($"Monster has {health} life left");
}

      


int AskInt(string question){
    while (true) {
        Console.WriteLine(question);

        if (int.TryParse(Console.ReadLine(), out int r))
            return r;
    }
}

AskInt is a method that asks for an integer and repeats the question if the user types garbage. Your logic has some superfluous elements; minimally we can get away with a loop that repeats while the monster has health. If it does not, then the loop ends after printing the died message and the program exits, otherwise it prints the life and asks for the damage again..

Echo jmc's sentiments about goto; it's very 1980s way of writing convoluted code and is usually entirely avoidable. I haven't used goto in a C# program in the last 20 years
Hello and thanks alot for the answear!!! I wasnt sure how to do properly the while loop and thats the reason for the spaguetti. but with that metod n your example already make alot sence. Tnx alot!
 
Back
Top Bottom