我正在处理这个获取用户输入的应用程序,并将它们放入ListBox中,并找到这些数字的总和(一切正常)。我的问题是,每次用户输入一个新号码,该号码正在窗口中显示(我不想要),而我只想在窗口上显示所有ListBox号码的当前总和。因此,如果用户输入新号码,则应在窗口上显示新的总和。请帮我。非常感谢你的进步。这是我的代码运行得很好....
private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
{
int sum = 0;
//Hides InputBox and takes input text from user.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Ensuring that input from user is a integer number
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result))
{
//Adding number of coins to CoinListBox
CoinListBox.Items.Add(result);
}
else
{
MessageBox.Show("Please enter a number of coins");
}
sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));
if(sum > 30)
{
//Removing last coin in case number of coins exceeds 30
CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
// Resets InputBox.
InputTextBox.Text = String.Empty;
}
答案 0 :(得分:1)
您的主要问题是在方法中声明sum
。
只需使用:
int sum = 0;
private void ClickToAddMoreCoins(object sender, RoutedEventArgs e)
{
if (sum+(int) (InputTextBox.Text) > 30)
{
//Removing last coin in case number of coins exceeds 30
CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
//Hides InputBox and takes input text from user.
InputBox.Visibility = System.Windows.Visibility.Collapsed;
// Ensuring that input from user is a integer number
string number = InputTextBox.Text;
int num;
if(int.TryParse(number,out num))
{
sum += num;
try { CoinListBox.Items.RemoveAt(0);
} catch
{}
CoinListBox.Items.Add(sum);
}
InputTextBox.Text = string.Empty;
}
古德勒克。
答案 1 :(得分:1)
Slashy的答案不完整,因为它允许添加负数。
如果您将InputTextBox的可见性设置为Collapsed,那么您将如何获得用户输入?什么是使用InputTextBox的意义?
如果您只想显示sum,则可以通过将其可见性设置为Collapsed来隐藏ListBox。所以,现在你的代码看起来像:
...
CoinListBox.Visibility = System.Windows.Visibility.Collapsed;
...
sum = CoinListBox.Items.Cast<object>().Sum(x => Convert.ToInt32(x));
if (sum > 30)
{
sum -= result; // removing excess coin
//Removing last coin in case number of coins exceeds 30
CoinListBox.Items.RemoveAt(CoinListBox.Items.Count - 1);
MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
您应该检查负数的更好的代码版本如下:
List<int> coinList = new List<int>();
private void ClickToAddMoreCoins2(object sender, RoutedEventArgs e)
{
int sum = 0;
//Hides InputBox and takes input text from user.
//InputTextBox.Visibility = System.Windows.Visibility.Collapsed;
// Ensuring that input from user is a integer number
String input = InputTextBox.Text;
int result = 0;
if (int.TryParse(input, out result) && result > 0)
{
//Adding number of coins to CoinListBox
coinList.Add(result);
}
else
{
MessageBox.Show("Please enter a valid number of coins");
}
sum = coinList.Sum();
if (sum > 30)
{
sum -= result;
//Removing last coin in case number of coins exceeds 30
coinList.RemoveAt(coinList.Count - 1);
MessageBoxResult answer = MessageBox.Show("You cannot enter more than 30 coins. Do you want to end?", "Message", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (answer == MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
tbSum.Text = "Sum = " + sum.ToString();
// Resets InputBox.
InputTextBox.Text = String.Empty;
InputTextBox.Focus();
}