My if Else doesn’t work on the Console.Readkey, so he was doing a little program he’s running but he "jumps" the commands inside if Else

Asked

Viewed 47 times

0

using System;
using System.Security.Cryptography.X509Certificates;

namespace projeto
{
    class Program
    {
        static void Main(string[] args)
        {
            Dados a = new Dados();

            Console.Write("Entre com o numero da conta: ");
            a.Nconta = double.Parse(Console.ReadLine());

            Console.WriteLine();

            Console.Write("Entre com o nome: ");
            a.Nome = Console.ReadLine();

            Console.WriteLine();
            
            // O erro começa aqui em baixo

            Console.Write("Haverá um deposito inicial (s/n)? :  ");
            var deposito = Console.ReadKey().ToString();

            if (deposito == "N")
            {
                double depositado = 0;
            }
            else if (deposito == "S")
            {
                Console.Write("Entre com o valor depositado: ");
                double depositado = double.Parse(Console.ReadLine());
            }
        }
    }
}
  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

1

There are several errors in that code. I will correct some, but not all, mainly the object of the account that should have constructor, methods that do certain operations instead of accessing the data directly. And I didn’t treat mistakes properly, just don’t let it happen.

I gave a better name to what is the object you are trying to deal with. And using the right type to deal with monetary value.

I test if the typed data makes sense and only accept it when it is ok. The way it was breaking the application by a wrong typing.

I gave more meaningful names to all variables.

When I ask yes or no I ask for a normal text, because letting only one key define what it will do may not be a good UI. Actually I wouldn’t even ask that, I asked for the value and I accept the 0, but I didn’t take it to show how it corrects the value.

ReadKey() does not do what it expects, it does not return a text or a number that makes sense as text, it returns the code of the key, and not a code of a character.

And you cannot declare a variable conditionally.

using static System.Console;
using System;

namespace Projeto {
    public class Program {
        public static void Main() {
            Conta conta = new Conta();
            Write("Entre com o numero da conta: ");
            if (!int.TryParse(ReadLine(), out var numero)) return;
            conta.Numero = numero;
            WriteLine();
            Write("Entre com o nome: ");
            conta.Nome = ReadLine();
            WriteLine();
            Write("Haverá um deposito inicial (s/n)? :  ");
            var temDeposito = ReadLine();
            var valor = 0M;
            if (temDeposito.Equals("S", StringComparison.CurrentCultureIgnoreCase)) {
                Write("Entre com o valor depositado: ");
                if (!decimal.TryParse(ReadLine(), out valor)) return;
            }
            conta.Saldo = valor;
        }
    }
    
    public class Conta {
        public int Numero { get; set; }
        public string Nome { get; set; }
        public decimal Saldo { get; set; }
    }
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • Our guy you are an angel! thank you very much!

  • @Apectocio see in the [tour] the best way to say thank you here.

Browser other questions tagged

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