使用循环显示框中可以容纳多少个多维数据集?

时间:2015-10-11 19:44:04

标签: c# winforms

这是我的任务:为了方便起见,他们只想知道我们可以在每个盒子中放入多少个整个立方体。此外,假设每个立方体整齐地装入盒子;我们不必处理部分分配的行。 (导师将在课堂上解释这一点。)

我们的任务是修改第一个体积计算器,以显示框中适合多少个立方体的额外结果。此分配将需要使用循环和方法。虽然教授知道这个计算器可以在不使用任何循环的情况下进行编码,但这个任务的一个要点是帮助理解循环结构。表单的UI不会改变,因为我们仍然需要接受长度,宽度和高度。

此外,此分配必须提供异常处理。不要假设用户只打算输入有效数字! (提示,其中一个用户不会。)最后,让MessageBox以形式显示结果:长度为l,宽度为w,深度为d的框的体积为x,可以容纳n个立方体。

我无法弄清楚如何通过循环计算出来。这就是我到目前为止所做的。

using System;
using System.Windows.Forms;

namespace Project_2
{
    public partial class VolumeCalculator : Form
    {
        public VolumeCalculator()
        {
            InitializeComponent();

            Length.TextChanged += OnTextBoxTextChanged;
            Width.TextChanged += OnTextBoxTextChanged;
            Depth.TextChanged += OnTextBoxTextChanged;

            Length.KeyPress += OnTextBoxKeyPress;
            Width.KeyPress += OnTextBoxKeyPress;
            Depth.KeyPress += OnTextBoxKeyPress;

            Calculate.Click += OnCalculateClick;
        }

        void OnTextBoxKeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter && Calculate.Enabled)
            {
                OnCalculateClick(this, new EventArgs());
            }
        }

        void OnCalculateClick(object sender, EventArgs e)
        {
            double width;
            double length;
            double depth;
            double volume;

            if (!double.TryParse(Length.Text, out length))
            {
                MessageBox.Show("Invalid length entered.", "Volume Calculator",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Length.Focus();
                Length.SelectAll();
            }
            else if (!double.TryParse(Width.Text, out width))
            {
                MessageBox.Show("Invalid width entered.", "Volume Calculator",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Width.Focus();
                Width.SelectAll();
            }
            else if (!double.TryParse(Depth.Text, out depth))
            {
                MessageBox.Show("Invalid depth entered.", "Volume Calculator",
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Depth.Focus();
                Depth.SelectAll();
            }
            else
            {
                volume = length * width * depth;
                MessageBox.Show(string.Format("A box with length {0:0.0}, width {1:0.0}, and depth {2:0.0} has a volume of {3:0.00}.",
                    length, width, depth, volume));
            }
        }

        void OnTextBoxTextChanged(object sender, EventArgs e)
        {
            Calculate.Enabled = Length.Text.Trim().Length > 0 &&
                                Width.Text.Trim().Length > 0 &&
                                Depth.Text.Trim().Length > 0;
        }
    }

2 个答案:

答案 0 :(得分:0)

计算机科学教师不要......在互联网上使用电脑吗?也许甚至使用StackOverflow?

检查输入无效是好的。在您输入错误消息并执行.SelectAll后,请使用

return;

离开OnCalculateClick函数。几乎在所有情况下,您都输入一个函数,验证输入值,如果它们不可用,则报告并退出该函数。

我看到循环的唯一地方就是替换

volume = length * width * depth;

使用:

int volume = 0;
for (int l = 0; l < length; l++)
{
    // etc... for the student to figure out
}

答案 1 :(得分:0)

我会使用一种方法进行转换和验证

bool TryConvert(TextBox textBox, out double value)
{
    if (!Double.TryPare(textBox.Text, out value)) {
        string message = String.Format("Invalid {0} entered.", textBox.Name.ToLower());
        MessageBox.Show(message , "Volume Calculator", MessageBoxButtons.OK,
                        MessageBoxIcon.Warning);
        textBox.Focus();
        textBox.SelectAll();
        return false;
    }
    return true;
}

现在,OnCalculateClick变得更容易了:

void OnCalculateClick(object sender, EventArgs e)
{
    double width;
    double length;
    double depth;
    double volume;

    if (TryConvert(Length, out length) &&
        TryConvert(Width, out width) &&
        TryConvert(Depth, out depth))
    {
        volume = length * width * depth;
        string message = String.Format(
            "A box with length {0:0.0}, width {1:0.0}, and depth {2:0.0} has a volume of {3:0.00}.",
            length, width, depth, volume); 
        MessageBox.Show(message);
    }
}

您可以使用循环来查找多维数据集适合框尺寸的次数。我用文字解释:将计数器设置为0.重复循环,而计数器时间立方体大小小于或等于其中一个框尺寸。在每个循环中将计数器增加1。对所有3个维度执行此操作并将这三个计数相乘。

当然,将此计算放入另一种方法中。然后你可以用不同的参数调用它三次。