Prazer em desenvolver software
Pessoal, sou iniciante e gostaria de tirar uma dúvida com vocês. Estou aprendendo C# e queria fazer um for no qual o mesmo ira colocar um for dentro do outro. Basicamente o resultado na tela seria assim:
0 - A
1 - B
2 - C
(...)
E assim sucessivamente. Segue abaixo o código que eu fiz. Não sei o que estou errando. Se alguém puder me ajudar.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string[] nome;
int[] contador;
nome = new string[26];
contador = new int[26];
contador[0] = 0;
nome[0] = "A";
nome[1] = "B";
nome[2] = "C";
nome[3] = "D";
nome[4] = "E";
nome[5] = "F";
nome[6] = "G";
nome[7] = "H";
nome[8] = "I";
nome[9] = "J";
nome[10] = "K";
nome[11] = "L";
nome[12] = "M";
nome[13] = "N";
nome[14] = "O";
nome[15] = "P";
nome[16] = "Q";
nome[17] = "R";
nome[18] = "S";
nome[19] = "T";
nome[20] = "U";
nome[21] = "V";
nome[22] = "W";
nome[23] = "X";
nome[24] = "Y";
nome[25] = "Z";
for (int c = 0; c < 26; c++)
{
Console.Write(c + " - ");
Console.Write("\n");
}
for (int i = 0; i < 26; i++)
{
Console.Write(nome[i]);
Console.Write("\n");
}
Console.Write("\n");
Console.Write("\n");
Console.ReadKey();
}
}
}
Tags:
Permalink Responder até Felipe C de Jesus em 2 maio 2017 at 17:53
Bruno, veja meu exempo abaixo utiliza a tabela ascii para pegar as letras assim vc não precisa do array e nem de 2 for
https://equipe.nce.ufrj.br/adriano/c/apostila/tabascii.htm
Veja meu exemplo abaixo
static void Main(string[] args)
{
int unicode = 65;
for (int i = 1; i <= 26; i++)
{
char character = (char) unicode;
string text = character.ToString();
Console.WriteLine(String.Format("{0} - {1}",i,text));
unicode ++;
}
Console.ReadKey();
}
Permalink Responder até Frank Dantas Cunha em 2 maio 2017 at 17:55
for (int c = 0; c < 26; c++)
{
Console.WriteLine(c + " - " + nome[c]);
}
Valeu Felipe,
Esse código ficou muito enxuto e interessante de utilizar em aplicações futuras.
Felipe C de Jesus disse:
Bruno, veja meu exempo abaixo utiliza a tabela ascii para pegar as letras assim vc não precisa do array e nem de 2 for
https://equipe.nce.ufrj.br/adriano/c/apostila/tabascii.htmVeja meu exemplo abaixo
static void Main(string[] args)
{
int unicode = 65;for (int i = 1; i <= 26; i++)
{
char character = (char) unicode;
string text = character.ToString();
Console.WriteLine(String.Format("{0} - {1}",i,text));
unicode ++;
}Console.ReadKey();
}
Frank esse código seu ficou excelente. Fácil entendimento, simples e ágil.
Frank Dantas Cunha disse:
for (int c = 0; c < 26; c++)
{
Console.WriteLine(c + " - " + nome[c]);}
Permalink Responder até Anderson de Assis em 10 maio 2017 at 12:15
bom dia.
Use substring... sem precisar de array
string alfabeto = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i=0; i <= alfabeto.Length-1;i++)
{
Console.WriteLine("{0} - {1}", i, alfabeto.Substring(i, 1));
}
Console.ReadKey();
© 2018 Criado por Ramon Durães.
Ativado por