如何制作'while'数字之间的循环计数

本文关键字:循环 while 何制作 数字 之间 | 更新日期: 2023-09-27 18:11:42

我有一个程序,它有两个文本框用于输入。它们是变量1和变量2。我不能算出我需要在for循环中放入的表达式来显示这些数字和它们之间的数字。例如,如果我把1和11放在一起,那么它们之间需要2-10。这是我目前所掌握的…我知道我还没有else表达式

namespace loops
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void whileButton_Click(object sender, EventArgs e)
        {
            double variableOne = 0;
            double variableTwo = 0;
            int i = 0;

            if (double.TryParse(variableOneText.Text, out variableOne))
            {
                if(double.TryParse(variableTwoText.Text, out variableTwo))
                {
                    while(i<= (variableOne) && (variableTwo))
                    {
                        i++;
                    }
                    outputLabel.Text = i.ToString();
                }
            }
        }
    }
}

如何制作'while'数字之间的循环计数

仍然不清楚你想要实现什么。

我猜一下:

INPUT:两个有理数。输出:中间的所有自然数。

<0, 0, 0

下面是代码

StringBuilder output = new StringBuilder();
double variableOne = 8.1;
double variableTwo = 14.9;
double start = 0;
double end = 0;
bool isShowingInitialVariable = true;

//if (double.TryParse(variableOneText.Text, out variableOne))
//{
//    if (double.TryParse(variableTwoText.Text, out variableTwo))
//    {

//take allways the smaller number to start and the greater for end
start = Math.Min(variableOne, variableTwo); 
end = Math.Max(variableOne, variableTwo);
//build your output
output.AppendLine(string.Format("first variable {0}, second variable {1}", variableOne, variableTwo));
output.AppendLine(string.Format("min", start));
output.AppendLine(string.Format("max", end));
output.AppendLine(string.Format("difference {0}", end - start));

//all the natural numbers in between.
for (int i = (int)Math.Ceiling(start); i < Math.Floor(end); i++)
{
    output.AppendLine(i.ToString());
}
Console.WriteLine(output.ToString());
//        outputLabel.Text = output.ToString();
//    }
//}