D
D
Dymetrey2017-10-01 10:17:13
C++ / C#
Dymetrey, 2017-10-01 10:17:13

How to return to the beginning of the program?

if (att == 1) {
Console.WriteLine("You used a normal attack");
Console.WriteLine("Damage: " + attack);
Console.WriteLine("Enemy left: " + (ehp - attack) + "hp");
Console.WriteLine ("The enemy killed you, please try again")
(This should be a simple restart of the program.)
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tom Nolane, 2017-10-01
@Klirik_High

there are three options:
1) use the label

M:
if (att == 1) {
//или тут M:
Console.WriteLine ("Вы использовали обычную атаку");
Console.WriteLine ("Урон: " + attack);
Console.WriteLine ("У врага осталось: " + (ehp - attack) + "хп");
Console.WriteLine ("Враг вас убил, попробуйте еще раз")
goto M; //(Здесь должен быть банальный рестарт программы.)
}

2) make the function separate (recursion)
private void MyFunc(bool stop = false;)
{
if(stop) return;
if (att == 1) { 
Console.WriteLine ("Вы использовали обычную атаку");
Console.WriteLine ("Урон: " + attack);
Console.WriteLine ("У врага осталось: " + (ehp - attack) + "хп");
Console.WriteLine ("Враг вас убил, попробуйте еще раз")
MyFunc(); //(Здесь должен быть банальный рестарт программы.)
}
}

3) use a for loop (infinite)
int damage = 100;
for(;;)
{
if (att == 1) { 
Console.WriteLine ("Вы использовали обычную атаку");
Console.WriteLine ("Урон: " + attack);
Console.WriteLine ("У врага осталось: " + (ehp - attack) + "хп");
Console.WriteLine ("Враг вас убил, попробуйте еще раз") 
}
hp < damage  ? break : continue; //(Здесь должен быть банальный рестарт программы.)
}

upd
In my own way, I added conditions for exiting a method/loop in some places. Since it is not clear from your code in which case all this should stop, therefore, correct the condition when everything should end. The main thing is that the above three methods fully answer your question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question