Stránka 1 z 1

C# - Cannot implicitly convert type 'string' to 'int'

Napsal: 26 kvě 2014 16:31
od VirtualPixelCZ
Dobrý den,
píšu svůj první program v C#, ale pořád mě to hlásí nějakou chybu a já nevím jak to vyřešit

Kód: Vybrat vše

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication9
{
    class Program
    {
        static void Main(String[] args)
        {

            Console.WriteLine("Zadejte číslo k zdvojnásobení:");
            int a = Console.ReadLine();
            a = a * 2;
            Console.WriteLine(a);
            Console.ReadKey();
        }
    }
}


Chyba: Cannot implicitly convert type 'string' to 'int'
Print screen: http://imgur.com/uUbNyu3

Mockrát děkuju za všechny odpovědi

Re: C# - Cannot implicitly convert type 'string' to 'int'

Napsal: 26 kvě 2014 16:41
od faraon
Do číselné proměnné nemůžeš načítat text, takže místo

int a = Console.ReadLine();

zkus něco jako

int a = int.Parse(Console.ReadLine());

nebo převod řetězce na číslo

int a = Convert.ToInt32(Console.Readline());