В чем проблема с циклом С#?

Код:

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

    namespace SkyFall
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            double C, m, y, ay, ny,  dt=1;

            private void button1_Click(object sender, EventArgs e)
            {
                chart1.Series[0].Points.Clear();
                chart2.Series[0].Points.Clear();
                chart3.Series[0].Points.Clear();
                //chart4.Series[0].Points.Clear();
                y = double.Parse(textBox1.Text);
                m = double.Parse(textBox2.Text);
                C = 0.55 * Math.Pow(m * m, 0.33333);
                Runge();
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
            public void Runge()
            {

                double Vy = 0, g = -9.81,  t =0;
                double A = 0, Ro;
                double k1, k2, k3, k4;
                double olday = ay;
                double neway;
                int i = 0;
                while (y >= 0)
                {
                    i++;
                    Ro = 0.0025 * y/1000 * y/1000 - 0.1047 * y/1000 + 1.225;
                    A = C * (Ro * Vy * Vy) / 2;
                    k1 = -g  + A / m;
                    k2 = -g  + dt / 2 + A / m + dt * k1 / 2;
                    k3 = -g  + dt / 2 + A / m + dt * k2 / 2;
                    k4 = -g  + dt + A / m + dt * k3;
                    neway = olday + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4);
                    neway = (-m * g + A) / m;
                    t = t + dt;
                    Vy = Vy + (olday + neway) / 2 * dt;
                    y = y + Vy * dt;
                    ny = -olday / g;
                    //chart1.Series[0].Points.AddXY(ny, y);
                    //chart2.Series[0].Points.AddXY(y, t);
                    //chart3.Series[0].Points.AddXY(Vy, t);
                    //chart4.Series[0].Points.AddXY(neway, y);
                    ///////////////////
                    olday = neway;
                    //dataGridView1[0, i].Value = t.ToString();
                    //dataGridView1[1, i].Value = y.ToString();
                    //dataGridView1[2, i].Value = Vy.ToString();
                    //dataGridView1[3, i].Value = neway.ToString();
                    label3.Text = olday.ToString() + ", " + Vy.ToString() + ", "+ y.ToString();

                }
            }
        }

    }

Суть проблемы: значения olday, Vy и y имеют значения Nan. ошибка

Не могу понять где я ошибся, прошу указать на ошибку и как ее исправить. Спасибо.


Ответы (1 шт):

Автор решения: timob256

Я не считаю что это ответ(ибо по сути не расписан вопрос) но всё же распишу .

Во первых сам код с ошибками основная ошибка CS0103 вы вызываете не объявленные элементы (как переменные так и функции)

 comp@comp0:~/Qt_project/TCP_IP$ mcs tehn5.cs -r:System.Windows.Forms.dll -r:System.Data.dll -r:System.Drawing.dll
    tehn5.cs(17,17): error CS0103: The name `InitializeComponent' does not exist in the current context
    tehn5.cs(23,17): error CS0103: The name `chart1' does not exist in the current context
    tehn5.cs(24,17): error CS0103: The name `chart2' does not exist in the current context
    tehn5.cs(25,17): error CS0103: The name `chart3' does not exist in the current context
    tehn5.cs(27,34): error CS0103: The name `textBox1' does not exist in the current context
    tehn5.cs(28,34): error CS0103: The name `textBox2' does not exist in the current context
    tehn5.cs(71,21): error CS0103: The name `label3' does not exist in the current context
    error CS5001: Program `tehn5.exe' does not contain a static `Main' method suitable for an entry point
    Compilation failed: 8 error(s), 0 warnings
comp@comp0:~/Qt_project/TCP_IP$ mcs tehn5.cs -r:System.Windows.Forms.dll -r:System.Data.dll -r:System.Drawing.dll
tehn5.cs(17,17): error CS0103: The name `InitializeComponent' does not exist in the current context
tehn5.cs(71,21): error CS0103: The name `label3' does not exist in the current context
error CS5001: Program `tehn5.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 3 error(s), 0 warnings

Также у вас в коде отсутствует точка входа Main() её просто нету .

comp@comp0:~/Qt_project/TCP_IP$ mcs tehn5.cs -r:System.Windows.Forms.dll -r:System.Data.dll -r:System.Drawing.dll
tehn5.cs(19,20): warning CS0649: Field `SkyFall.Form1.C' is never assigned to, and will always have its default value `0'
tehn5.cs(19,23): warning CS0649: Field `SkyFall.Form1.m' is never assigned to, and will always have its default value `0'
tehn5.cs(19,29): warning CS0649: Field `SkyFall.Form1.ay' is never assigned to, and will always have its default value `0'
tehn5.cs(19,33): warning CS0414: The private field `SkyFall.Form1.ny' is assigned but its value is never used
error CS5001: Program `tehn5.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 1 error(s), 4 warnings

Хотя на самом деле (это лично моё мнение) основная проблема что у вас не расписана логика, что именно должна делать программа, я к примеру совершенно не пойму в чём цель 0_о (не держитесь за стучание по клавишам )

Лично для меня программирование вторично на фоне алгоритмики, сначала алгоритм, а потом код :-)

В общем я закомментировал весь код который даёт ошибку и увы так и не понял что именно должна делать ваша программа вот код:

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

    namespace SkyFall
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
         //       InitializeComponent();
            }
            double C, m, y, ay, ny,  dt=1;

         //   private void button1_Click(object sender, EventArgs e)
         //   {
         //       chart1.Series[0].Points.Clear();
         //       chart2.Series[0].Points.Clear();
         //       chart3.Series[0].Points.Clear();
         //       //chart4.Series[0].Points.Clear();
         //       y = double.Parse(textBox1.Text);
         //      m = double.Parse(textBox2.Text);
         //       C = 0.55 * Math.Pow(m * m, 0.33333);
         //       Runge();
         //   }

            private void Form1_Load(object sender, EventArgs e)
            {

            }
            public void Runge()
            {

                double Vy = 0, g = -9.81,  t =0;
                double A = 0, Ro;
                double k1, k2, k3, k4;
                double olday = ay;
                double neway;
                int i = 0;
                while (y >= 0)
                {
                    i++;
                    Ro = 0.0025 * y/1000 * y/1000 - 0.1047 * y/1000 + 1.225;
                    A = C * (Ro * Vy * Vy) / 2;
                    k1 = -g  + A / m;
                    k2 = -g  + dt / 2 + A / m + dt * k1 / 2;
                    k3 = -g  + dt / 2 + A / m + dt * k2 / 2;
                    k4 = -g  + dt + A / m + dt * k3;
                    neway = olday + dt / 6 * (k1 + 2 * k2 + 2 * k3 + k4);
                    neway = (-m * g + A) / m;
                    t = t + dt;
                    Vy = Vy + (olday + neway) / 2 * dt;
                    y = y + Vy * dt;
                    ny = -olday / g;
                    //chart1.Series[0].Points.AddXY(ny, y);
                    //chart2.Series[0].Points.AddXY(y, t);
                    //chart3.Series[0].Points.AddXY(Vy, t);
                    //chart4.Series[0].Points.AddXY(neway, y);
                    ///////////////////
                    olday = neway;
                    //dataGridView1[0, i].Value = t.ToString();
                    //dataGridView1[1, i].Value = y.ToString();
                    //dataGridView1[2, i].Value = Vy.ToString();
                    //dataGridView1[3, i].Value = neway.ToString();
                //    label3.Text = olday.ToString() + ", " + Vy.ToString() + ", "+ y.ToString();

                }
            }

            [STAThread]
            public static void Main() // вот добавил старт программы :3
            {
                var f = new Form();
                f.Text = "Hello World";
                Application.Run(f);
            }
        }
    }

По этому вот запущенная ваша программа (замете предупреждения которые говорят что вы не используете переменные) :

comp@comp0:~/Qt_project/TCP_IP$ mcs tehn5.cs -r:System.Windows.Forms.dll -r:System.Data.dll -r:System.Drawing.dll
tehn5.cs(19,20): warning CS0649: Field `SkyFall.Form1.C' is never assigned to, and will always have its default value `0'
tehn5.cs(19,23): warning CS0649: Field `SkyFall.Form1.m' is never assigned to, and will always have its default value `0'
tehn5.cs(19,29): warning CS0649: Field `SkyFall.Form1.ay' is never assigned to, and will always have its default value `0'
tehn5.cs(19,33): warning CS0414: The private field `SkyFall.Form1.ny' is assigned but its value is never used
Compilation succeeded - 4 warning(s)
comp@comp0:~/Qt_project/TCP_IP$ mono tehn5.exe 
Gtk-Message: 17:33:35.874: Failed to load module "canberra-gtk-module"

введите сюда описание изображения

Также посоветую вам книгу (очень хорошая сам ей пользуюсь) : Программирование на C# для начинающих. Основные сведения. Алексей Васильев

→ Ссылка