使用循环来显示一个盒子可以容纳多少个立方体
本文关键字:盒子 立方体 多少 一个 循环 显示 | 更新日期: 2023-09-27 18:17:22
这是我的任务:为了方便起见,他们只想知道每个盒子里可以装多少个完整的立方体。同时,假设每个立方体都能整齐地放进盒子里;我们不需要处理部分分配的行。(老师会在课堂上解释)
我们的任务是修改第一个体积计算器,以显示盒子中可以容纳多少个立方体的额外结果。这个赋值将需要使用循环和方法。虽然教授知道这个计算器可以在不使用任何循环的情况下进行编码,但这项作业的一个目的是帮助理解循环结构。表单的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;
}
}
不要计算机科学老师嗯…在互联网上使用电脑?也许甚至可以使用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
}
我将使用一个方法进行转换和验证
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次