简体   繁体   English

数字降序C#

[英]Descending Numbers C#

I was able to write this tiny segment of this program to display the numbers in Ascending order but i still cant get it to display it in Descending order. 我能够编写该程序的这一小段以升序显示数字,但是我仍然无法以降序显示它。

Basics of what this program should do is takes in two number values from the user in the form of "From" and "To" and displays it as a list in the listbox. 该程序应该做的基础是从用户那里以“发件人”和“发件人”的形式输入两个数字值,并将其显示为列表框中的列表。 The users choice of either ascending or descending order depends on which of the two Radio buttons he has selected. 用户选择升序还是降序取决于他选择了两个单选按钮中的哪个。

    private void btnCalc_Click(object sender, EventArgs e)
    {

        double fromNum, toNum, total = 0;

        fromNum = double.Parse(txtFrom.Text);
        toNum = double.Parse(txtTo.Text);

        lstResult.Items.Clear();
        lblResult.Text = "";

        if (radAsc.Checked)
        {
            while (fromNum <= toNum)
            {
                lstResult.Items.Add(fromNum);

                total = total + fromNum;

                fromNum++;
            }
        }

        else
        {
            while (fromNum >= toNum)
            {
                lstResult.Items.Add(fromNum);

                total = total + toNum;

                toNum--;
            }
        }

        lblResult.Text = total.ToString();
    }

Here's an image to what the program looks like. 这是程序外观的图像。 http://imgur.com/SVwN3Tx http://imgur.com/SVwN3Tx

Note:- I am completely new to C# and I've just started taking it in College. 注意:-我是C#的新手,我刚开始在大学学习。

I suggest using for loop instead of while which makes the code easy to implement: 我建议使用for循环而不是while来简化代码的实现:

    if (radAsc.Checked)
    {
        // num += 1: - I've seen odds/even switch on the screenshot
        // so you may want to change/add num += 1 into num += 2
        for (double num = fromNum; num <= toNum; num += 1) {
          lstResult.Items.Add(num);
          total = total + num;
        }
    }
    else
    {
        // Descending order: 
        //   - start from the bottom (toNum)
        //   - loop until the top (fromNum) 
        //   - descend by 1 (num -= 1)
        for (double num = toNum; num >= fromNum; num -= 1) {
          lstResult.Items.Add(num);
          total = total + num;
        }
    }

You're decrementing the wrong value 您正在减少错误的价值

while (fromNum >= toNum)
{
    lstResult.Items.Add(fromNum);

    total = total + toNum;
    toNum--;
}

So, here's what you're doing: 所以,这是你在做什么:

Say fromNum is 10, and toNum is 1. 说fromNum为10,toNum为1。

After your first iteration, fromNum is still 10 but toNum is 0. Decrement the fromNum instead of toNum and it should work accordingly. 第一次迭代后,fromNum仍然为10,但toNum为0。将fromNum而不是toNum减一,它应该可以正常工作。

EDIT 编辑

Couple things to take note. 几件事要注意。 If total is collecting the sum of all numbers, a neat way to write: 如果total正在收集所有数字的总和,那么一种简洁的书写方式是:

total = total + value;

is

total += value; .

You should also verify that the numbers will actually work before going into your logic. 在进入逻辑之前,您还应该验证这些数字是否确实有效。 So if the radio button is selected for Ascending order, you want to make sure fromNum is less than toNum, and maybe throw up a message box if they're not: 因此,如果为“升序”选择了单选按钮,则要确保fromNum小于toNum,如果不是,则抛出一个消息框:

if(fromNum < toNum)
    { run logic .... }
else
    { alert user ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM