Şimdi Ara

(c#) toplamayı yapamadım ?

Daha Fazla
Bu Konudaki Kullanıcılar: Daha Az
2 Misafir - 2 Masaüstü
5 sn
6
Cevap
0
Favori
495
Tıklama
Daha Fazla
İstatistik
  • Konu İstatistikleri Yükleniyor
Öne Çıkar
0 oy
Sayfa: 1
Giriş
Mesaj
  • mehaba arkadaşlar

    amacım 1-10 arası tek sayıları listbox da sıralamak ve daha sonra listbox daki tek sayıları toplamak

    tek sayıları listbox'a şü şekilde aldım

    private void button3_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < 10; i++)
    {
    if (i%2==1)
    {
    listBox2.Items.Add(i);
    }
    }
    }

    ve bu şekilde de toplamaya çaliştim

    int teksayilar=0;
    for (int i = 0; i <listBox2.Items.Count ; i++)
    {
    teksayilar += listBox2.Items.IndexOf(i);
    }

    ama yanliş topluyor nerede yanliş yapiyorum



    _____________________________
  • foreach kullan


    foreach (var item in listBox2.Items)
    {

    teksayilar += (int)item;
    }



    < Bu mesaj bu kişi tarafından değiştirildi Guest-2D6B32B7D -- 5 Kasım 2009; 5:03:41 >
    _____________________________
  • cok teşekkur ediyorum
    _____________________________
  • bir sorun daha cıktı ben listboxsa dişardanda deger girmem gerekiyor ve girip topla butonuna basınca hata veriyor (dişardan deger girmedigim zaman sorunsuz) kodlar bunlar

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace odevv
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }

    private void button2_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < 6; i++)
    {
    listBox1.Items.Add(i);

    }
    }

    private void button3_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < 10; i++)
    {
    if (i%2==1)
    {
    listBox2.Items.Add(i);

    }
    }
    }

    private void button4_Click(object sender, EventArgs e)
    {
    for (int i = 0; i < 9; i++)
    {
    if (i%3==0)
    {
    listBox3.Items.Add(i);

    }
    }
    }

    private void button1_Click(object sender, EventArgs e)
    {
    int a = 0;
    a=int.Parse(textBox1.Text);
    if (a%2==1)
    {
    listBox4.Items.Add(textBox1.Text);
    listBox2.Items.Add(textBox1.Text);

    textBox1.Clear();

    }
    else if (a % 3 == 0)
    {
    listBox4.Items.Add(textBox1.Text);
    listBox3.Items.Add(textBox1.Text);
    textBox1.Clear();
    }
    else
    {
    listBox4.Items.Add(textBox1.Text);
    listBox1.Items.Add(textBox1.Text);
    textBox1.Clear();
    }



    }

    private void button5_Click(object sender, EventArgs e)
    {
    int secilmeyen = 0;
    int secilenlerintoplami = 0;
    int normalsayilar = 0;
    int ucunkatlari = 0;
    int teksayilar = 0;
    // for (int i = 1; i <listBox1.Items.Count; i++)
    foreach (var item in listBox1.Items)
    {
    normalsayilar += (int)item;
    }
    // normalsayilar += listBox1.Items.IndexOf(i);
    // for (int i = 0; i <listBox2.Items.Count ; i++)
    foreach (var item in listBox2.Items)
    {
    teksayilar += (int)item;
    }
    // teksayilar += listBox2.Items.IndexOf(i);
    // for (int i = 0; i <listBox3.Items.Count; i++)
    foreach (var item in listBox3.Items)
    {
    ucunkatlari += (int)item;
    }
    // ucunkatlari += listBox3.Items.IndexOf(i);


    // for (int i = 0; i < listBox4.Items.Count; i++)
    // if (listBox4.GetSelected(i)) secilenlerintoplami += listBox4.Items.IndexOf(i);
    // else
    // secilmeyen+=listBox4.Items.IndexOf(i) ;
    textBox2.Text = (normalsayilar + ucunkatlari + teksayilar + secilenlerintoplami+secilmeyen).ToString();
    textBox3.Text = secilenlerintoplami.ToString();
    textBox4.Text = secilmeyen.ToString();

    }
    }
    }


     (c#) toplamayı yapamadım ?
    _____________________________




  • sorun nedir

    ayrıca butonlara isim vermelisiniz, karışık olmuş koyduğunuz kod
    _____________________________
  • sorun çözüldü

    object tipinden direk int tipine dönüşüm olmuyormuş önce string tipine sonra int tipine dönüştürüldüğünde sorun çözülüyor

    yani Bu şekilde olacakmiş

    teksayilar += Convert.ToInt32(listBox2.Items[ i ] .ToString());
    _____________________________
  • Yapay Zeka’dan İlgili Konular
    Daha Fazla Göster
    
Sayfa: 1
- x
Bildirim
mesajınız kopyalandı (ctrl+v) yapıştırmak istediğiniz yere yapıştırabilirsiniz.