我已被任务创建一个C#Windows表单应用程序,该应用程序包含一个循环,在其中将用户输入上限和下限和输出从10开始,并在上限之前结束。我的程序不会输出任何内容,我不确定怎么了。这是我拥有的:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loops
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int lowerBounds, upperBounds, num;
private void TextBoxUpperBounds_TextChanged(object sender, EventArgs e)
{
upperBounds = Convert.ToInt32(Console.ReadLine());
}
private void BtnOutputValues_Click(object sender, EventArgs e)
{
for (num = lowerBounds; num < upperBounds; num++)
{
if (num % 10 == 0)
{
MessageBox.Show(num.ToString());
}
else { }
}
}
private void TextBoxLowerBounds_TextChanged(object sender, EventArgs e)
{
lowerBounds = Convert.ToInt32(Console.ReadLine());
}
}
}
您的代码仅输出10的倍数,该倍数低于上限。
使您的代码输出下限和上限之间的数字大于10,您应该使用以下循环:
for (num = lowerBounds; num < upperBounds; num++)
{
if (num >10 )
{
MessageBox.Show(num.ToString());
}
else { }
}
atomsmasher是完全正确的。这是执行我需要执行的操作的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Loops
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int lowerBounds, upperBounds, num;
private void TextBoxUpperBounds_TextChanged(object sender, EventArgs e)
{
upperBounds = Convert.ToInt32(textBoxUpperBounds.Text);
}
private void BtnOutputValues_Click(object sender, EventArgs e)
{
for (num = lowerBounds; num < upperBounds; num++)
{
if (num % 10 == 0)
{
MessageBox.Show(num.ToString());
}
else { }
}
}
private void TextBoxLowerBounds_TextChanged(object sender, EventArgs e)
{
lowerBounds = Convert.ToInt32(textBoxLowerBounds.Text);
}
}
}