C# İle Pozitif Negatif Sayıları Ayırma

Csharp İle Pozitif Negatif Sayıları Ayırma

C# ile rastgele üretilen -100 ile +100 arasındaki 10 tane sayıdan pozitif olanları ayrı bir Listbox’a yazan ve negatif olanları ayrı bir Listbox’a yazan program.

Öncelikle programın tasarımında 3 tane listbox ve 2 tane buton bulunmaktadır.

pozitifnegatif1
Sayı üret butonuna tıklandığında 10 tane rastgele pozitif ve negatif sayı üretilip Listbox’a yazdırılıyor. Daha sonra ayrıştır butonuna tıklandığında pozitif sayılar ayrı, negatif sayılar ayrı bir yere yazdırılıyor.

pozitifnegatif2
Programın kodları:

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 pozitif_negatif
{
    public partial class Form1 : Form
    {
        Random r = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            int i, sayi;
            for (i = 0; i < 10; i++)
            {               
                sayi=r.Next(-100, 100);
                listBox1.Items.Add(sayi);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            listBox2.Items.Clear();
            listBox3.Items.Clear();
            int i = 0;
            for (i = 0; i < 10; i++)
            {
                textBox1.Text = listBox1.Items[i].ToString();
                if (Convert.ToInt16(textBox1.Text) < 0)
                {
                    listBox2.Items.Add(listBox1.Items[i]);
                }
                else
                {
                    listBox3.Items.Add(listBox1.Items[i]);
                }
            }
        }
    }
}
Bu Yazıya Tepkin Nedir?
+1
1
+1
0
+1
0
+1
3
+1
1
+1
0
+1
0

Yorum Yap

3 Yorum

  • Burada textbox yok fakat siz textbox1.text yazmışsınız.Orda bir hata mı var yoksa öylemi olacak?

    • Bu projede bir tane gizli textbox1.text nesnesi var. Visible özelliği false yapılmış. Amaç o text kutusu ile sayının negatif, pozitif yada sıfır olduğu karşılaştırarak kontrol ediyor. Bir tane textbox eklerseniz program hatasız çalışacaktır.