所以我使用Visual Studio,我有4个文本框,每个文本框中都有1到5之间的数字,然后我有一个计算按钮,一旦点击将计算这些数字的百分比,奖项以图像形式输出(金星(90%或更高),银星(90-75%),青铜星(75-60%)或无图像。该奖项输出到图像框中。我对如何做到这一点并没有任何指示,所以欢迎所有的帮助。
这是我目前的代码
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
double dPercentage = Convert.ToDouble(((Convert.ToInt32(txtScore1.Text) + Convert.ToInt32(txtScore2.Text) + Convert.ToInt32(txtScore3.Text) + Convert.ToInt32(txtScore4.Text)) * 20) / 100);//calculates the percentage
if(dPercentage > 89)
{
imgAward.Source = new BitmapImage(new Uri(@"Images/gold.png", UriKind.RelativeOrAbsolute));
}
else if(dPercentage > 74 && < 90)
{
imgAward.Source = new BitmapImage(new Uri(@"Images/silver.png", UriKind.RelativeOrAbsolute));
}
else (dPercentage > 59 && < 74)
{
imgAward.Source = new BitmapImage(new Uri(@"Images/bronze.png", UriKind.RelativeOrAbsolute));
}
}
}}
答案 0 :(得分:0)
首先你的逻辑错了
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
double dPercentage = Convert.ToDouble(((Convert.ToInt32(txtScore1.Text) + Convert.ToInt32(txtScore2.Text) + Convert.ToInt32(txtScore3.Text) + Convert.ToInt32(txtScore4.Text))) / 20.0) * 100;//calculates the percentage
if(dPercentage >= 90)
{
//Percentage is greater than 89
MessageBox.Show("Eligible For the Gold Award");
imgAward.Source = new BitmapImage(new Uri("pack://application:,,,/Images/gold.png", UriKind.Absolute));
}
else if(dPercentage >= 75)
{
//(90-75%)
MessageBox.Show("Eligible For the Silver Award");
imgAward.Source = new BitmapImage(new Uri("pack://application:,,,/Images/silver.png", UriKind.Absolute));
}
else if (dPercentage >= 60)
{
// (75 - 60%)
MessageBox.Show("Eligible For the Bronze Award");
imgAward.Source = new BitmapImage(new Uri("pack://application:,,,/Images/bronze.png", UriKind.Absolute));
}else{
MessageBox.Show("Not Eligible For the Award");
}
}}
答案 1 :(得分:0)
除了决策中的逻辑错误,百分比计算是错误的。试试这个......
private void btnCalculate_Click(object sender, RoutedEventArgs e)
{
double dPercentage = Convert.ToDouble(((Convert.ToInt32(txtScore1.Text) + Convert.ToInt32(txtScore2.Text) + Convert.ToInt32(txtScore3.Text) + Convert.ToInt32(txtScore4.Text))) / 20.0) * 100;//calculates the percentage
if (dPercentage >= 90)
{
imgAward.Source = new BitmapImage(new Uri(@"Images/gold.png", UriKind.RelativeOrAbsolute));
}
else if (dPercentage >= 75)
{
imgAward.Source = new BitmapImage(new Uri(@"Images/silver.png", UriKind.RelativeOrAbsolute));
}
else if (dPercentage >= 60)
{
imgAward.Source = new BitmapImage(new Uri(@"Images/bronze.png", UriKind.RelativeOrAbsolute));
}
else
{
imgAward.Source = null;
}
}