Put a string in a boolean value

Asked

Viewed 161 times

0

As shown string in the ternary operator?

String aluno, aprovado, reprovado; 
      Console.WriteLine("Digite um número:");
      aluno = Console.ReadLine();

      Double media, nota1, nota2, nota3, nota4;
      media = (nota1 + nota2 + nota3 + nota4)/4;

      Boolean decisao,true,false;
      true = "Aprovado;"
      false = "Reprovado";

      decisao = media >= 5?  true : false;
      Console.WriteLine ($"{aluno} tem média {media} está: {decisao}.");
  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site

2 answers

2

This code does not make much sense and is full of incoherent things. One part I think you’ll complete later (I think you’ll get the data conversion wrong, the hint is when you do it, search on).

First of all don’t use keywords as variable names. Don’t actually create variables without need. Understand what a variable. It is not that I can not use keywords as variables, but it gets ugly and this should only be used in complex code that the name needs very much to be this word to be more readable. Your code creates confusion and becomes less readable. Can you give a plausible justification to create a variable to put a text that will only be used once and that its content does not change and is obvious? Worse, why does he need to use these keywords?

Without plausible justifications don’t do anything in your code.

In fact this code does not even compile, so it would work, although the average will be fixed.

using static System.Console;

public class Program {
    public static void Main() {
        WriteLine("Digite um número:");
        var aluno = ReadLine();
        var nota1 = 0.0;
        var nota2 = 0.0;
        var nota3 = 0.0;
        var nota4 = 0.0;
        var media = (nota1 + nota2 + nota3 + nota4) / 4;
        WriteLine ($"{aluno} tem média {media} está: {(media >= 5 ?  "Aprovado" : "Reprovado")}.");
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

1

I’ll answer your question:

As string shows in ternary operator?

bool var1;

var1 = true;

Console.WriteLine(var1?"Verdadeiro":"Falso");

var1 = false;

Console.WriteLine(var1?"Verdadeiro":"Falso");

Now if you want to use reserved words as variable name just put arroba in front:

var @true = "Verdadeiro";

var @object = "Objeto";

var @byte = "Byte";

var @new = "Novo";

var @dynamic = "Dinâmico";

And that’s not just for strings, any type of object.

Browser other questions tagged

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