U
U
Urlxd2021-09-16 15:07:10
C++ / C#
Urlxd, 2021-09-16 15:07:10

Code not going further in c#?

The code doesn't go any further. In the while condition, it does not go beyond the first condition

Code:

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;

namespace war_simulator
{

    public class Program 
    {
        public static void Main()
        {
        	Random rnd1 = new Random(Environment.TickCount);
    		
    		// создание переменных
    		int randatk1 = 0;
    		int randatk2 = 0;
    		
      double atk1, def1, def2, hp1, hp2, randwar2 = 0;
      double atk2 = 0;
      double randwar1 = 0;
      
      
      // вписывание категории
      atk1 = int.Parse(Console.ReadLine());
      def1 = int.Parse(Console.ReadLine());
      hp1 = int.Parse(Console.ReadLine());
      atk2 = int.Parse(Console.ReadLine());
      def2 = int.Parse(Console.ReadLine());
      hp2 = int.Parse(Console.ReadLine());
      
      if (atk1 == 0){
        atk1 = 0.1;
      }
      
      // вычесление силы первого персонажа
      if (atk1 <= def2){
        randwar1 = atk1 * 50 / def2;
      }
      if (atk1 > def2){
        randwar1 = def2 * 50 / atk1 - 100;
        randwar1 = Math.Pow(randwar1, 2);
        randwar1 = Math.Pow(randwar1, 0.5);
      }
      
      // вычесление силы второго персонажа
      if (atk2 <= def1){
        randwar2 = atk2 * 50 / def1;
      }
      if (atk2 > def1){
        randwar2 = def1 * 50 / atk2 - 100;
        randwar2 = Math.Pow(randwar2, 2);
        randwar2 = Math.Pow(randwar2, 0.5);
      }
      Console.WriteLine("hh");
      Console.WriteLine(randwar1);
      Console.WriteLine(randwar2);
      
      
      // процесс битвы
      while(true){
        randatk1 = rnd1.Next(0,101);
        randatk2 = rnd1.Next(0,101);
        if (randwar1 > randatk1){
          hp2 = hp2 - 1;
          Console.WriteLine("bruh 1");
          continue;
        }
        else if (randwar1 < randatk1){
          Console.WriteLine(hp2);
          continue;
        }
        if (randwar2 > randatk2){
          hp1 = hp1 - 1;
          Console.WriteLine("bruh 2");
          continue;
        }
        else if (randwar2 < randatk2){
          Console.WriteLine("iu 2");
          continue;
        }
        if (hp2 < 0){
          Console.WriteLine("1 team win");
          break;
        }
        if (hp1 == 0){
          Console.WriteLine("2 team win");
          break;
        }
      }
      Console.WriteLine("sksk");
      Console.WriteLine(randatk1);
      Console.WriteLine(randatk2);
        }
    }
}

Maybe I don't see something

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
spaceatmoon, 2021-09-16
@Urlxd

In your code, whoever hits first wins. Also, you will have an endless loop until the first and second miss the target.
I tried to rewrite the part with the battle so that each of the players equally randomly beat the other in one turn.

// процесс битвы
    while(true)
      {
        randatk1 = rnd1.Next(0,101);
        randatk2 = rnd1.Next(0,101);
        Console.WriteLine("h1 = " + hp1);
        Console.WriteLine("h2 = " + hp2);
        Console.WriteLine("Current chance h1 = " + randatk1);
        Console.WriteLine("Current chance h2 = " + randatk2);
        
        if (randatk1 > randatk2)
        {
          if (randwar1 > randatk1){
            hp2 = hp2 - 1;
            Console.WriteLine("atack player 1");
          }
        }
        else
        {
          if (randwar2 > randatk2){
            hp1 = hp1 - 1;
            Console.WriteLine("atack player 2");
          }
        }
      
        Console.WriteLine("All attacks repelled");
        if (hp1 <= 0 || hp2 <= 0)
        {
          if (hp1 <= 0)
          {
            Console.WriteLine("2 team win");
          }
          else
          {
            Console.WriteLine("1 team win");
          }
          break;
        }
        
      }

V
Vasily Bannikov, 2021-09-16
@vabka

The following after the first condition will not be met if:
randwar1 > randatk1- then the first condition will be met and continue will work;
randwar1 < randatk1- then the else branch will be executed, and continue will also work;
That is , execution will proceed to the following conditions only if randwar1 == randatk1
randwar2 > randatk2- again gets into continue;
randwar2 < randatk2- also

if (randwar1 > randatk1){
          hp2 = hp2 - 1;
          Console.WriteLine("bruh 1");
          continue;
        }
        else if (randwar1 < randatk1){
          Console.WriteLine(hp2);
          continue;
        }
        if (randwar2 > randatk2){
          hp1 = hp1 - 1;
          Console.WriteLine("bruh 2");
          continue;
        }
        else if (randwar2 < randatk2){
          Console.WriteLine("iu 2");
          continue;
        }
        if (hp2 < 0){
          Console.WriteLine("1 team win");
          break;
        }
        if (hp1 == 0){
          Console.WriteLine("2 team win");
          break;
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question