【问题标题】:Using a loop to display how many cubes can fit into a box?使用循环显示一个盒子可以容纳多少个立方体?
【发布时间】:2015-10-11 19:44:03
【问题描述】:

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

我们的任务是修改第一个体积计算器,以显示盒子可以容纳多少立方体的额外结果。此分配将需要使用循环和方法。虽然教授知道这个计算器可以在不使用任何循环的情况下进行编码,但这项任务的一个重点是帮助理解循环结构。表单的 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;
        }
    }

【问题讨论】:

  • 用户不应该在某处输入立方体的大小吗?

标签: c# winforms


【解决方案1】:

计算机科学老师不...在互联网上使用计算机吗?也许甚至使用 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
}

【讨论】:

    【解决方案2】:

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

    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。当计数器乘以立方体大小小于或等于其中一个框尺寸时重复循环。在每个循环中将计数器增加一。对所有 3 个维度执行此操作,然后将三个计数相乘。

    当然,把这个计算放到另一种方法中。然后你可以用不同的参数调用它3次3次。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多