Is there any way to round a negative value to zero in Visualg?

Asked

Viewed 187 times

2

I wish it were always displayed Vida: 0 in the last block, even if the value is negative, without doing that "Gambiarra" that I did just below.

Is there any function around vidaInimigo, without the need to create an additional condition: Se(vidaInimigo <= 0) entao EscrevaL("Vida: 0")?

inserir a descrição da imagem aqui

Because the enemy had 4 of Life, received 39 and was left with -35 of life.

I wanted life to be shown equal to 0, and not -35.

I imagine there’s some function that goes up.

Follow the code of the algorithm:

algoritmo "RPG"
var
   danoCausado, vidaInimigo, baseDanoPlayer: Inteiro
inicio
   baseDanoPlayer <- 50
   vidaInimigo <- 100
   EscrevaL("Vida: ", vidaInimigo)
   Repita
       danoCausado <- randi(baseDanoPlayer)
       vidaInimigo <- vidaInimigo - danoCausado
       EscrevaL(" >>> Dano causado: ", danoCausado)
       Se(vidaInimigo <= 0) entao
           EscrevaL("Vida: 0")
           senao
           EscrevaL("Vida: ", vidaInimigo)
       FimSe
   Ate(vidaInimigo <= 0)
   Se (vidaInimigo <= 0) entao
       EscrevaL("Inimigo abatido!")
   FimSe
fimalgoritmo

inserir a descrição da imagem aqui

  • This is not the question, but I did not have time to comment on what you deleted: https://en.wikipedia.org/wiki/Printf_format_string - it is not specific to Java, but it may help

  • This is more specific http://www.javawithus.com/tutorial/displaying-text-using-printf-method

  • And the link has been updated to double without decimal, applying the links above: https://ideone.com/irUPu3

1 answer

1


How about replacing that:

Se(vidaInimigo <= 0) entao
    EscrevaL("Vida: 0")
    senao
    EscrevaL("Vida: ", vidaInimigo)
FimSe

That’s why:

Se(vidaInimigo < 0) entao
    vidaInimigo <- 0
FimSe
EscrevaL("Vida: ", vidaInimigo)
  • 1

    It worked perfectly! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.