C# 使用循环显示一个盒子可以容纳多少个立方体?
这是我的任务:为了方便起见,他们只想知道每个盒子能装多少个立方体。此外,假设每个立方体都整齐地放入盒子中;我们不必处理部分分配的行。(讲师将在课堂上对此进行解释。) 我们的任务是修改第一个体积计算器,以显示盒子中容纳多少立方体的额外结果。此分配需要使用循环和方法。虽然教授知道这个计算器可以在不使用任何循环的情况下进行编码,但本作业的一点是帮助理解循环结构。表单的UI不会改变,因为我们仍然必须接受长度、宽度和高度 此外,此分配必须提供异常处理。不要假设用户只会键入有效的数字!(提示,其中一个用户不会)最后,让MessageBox以表单的形式显示结果:一个长度为l、宽度为w、深度为d的框的体积为x,可以容纳n个立方体 我不知道如何通过循环计算这个。这就是我目前所拥有的C# 使用循环显示一个盒子可以容纳多少个立方体?,c#,winforms,C#,Winforms,这是我的任务:为了方便起见,他们只想知道每个盒子能装多少个立方体。此外,假设每个立方体都整齐地放入盒子中;我们不必处理部分分配的行。(讲师将在课堂上对此进行解释。) 我们的任务是修改第一个体积计算器,以显示盒子中容纳多少立方体的额外结果。此分配需要使用循环和方法。虽然教授知道这个计算器可以在不使用任何循环的情况下进行编码,但本作业的一点是帮助理解循环结构。表单的UI不会改变,因为我们仍然必须接受长度、宽度和高度 此外,此分配必须提供异常处理。不要假设用户只会键入有效的数字!(提示,其中一个用户不
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 检查无效输入是好的。给出错误消息并执行。选择All后,使用
return;
要离开OnCalculateClick函数。在几乎所有情况下,您都会输入一个函数,验证输入值,如果它们不可用,则报告并退出该函数
我唯一看到循环的地方就是替换
volume = length * width * depth;
与:
int volume=0;
对于(int l=0;l
我将使用一种方法进行转换和验证
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个维度执行此操作,并将三个计数相乘
当然,把这个计算放到另一个方法中。然后您可以使用不同的参数调用它三次三次。用户不应该在某个地方输入多维数据集的大小吗?
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);
}
}