如何编写一个程序,将平均成绩计算为双精度值,同时在消息框中显示成绩?
How to write a program that calculates an average grade as a double-value which also shows the grade in a message box?
我正在学习成为一名游戏开发人员,我使用 C#。现在其中一项作业如下(从荷兰语翻译而来):
Two students participate in a C#-exam. Their results (a whole number of points between 0 and 100) are assigned to two variables:
int outcomeStudent1 = 44;
int outcomeStudent2 = 51;
Write a program that calculates the average grade as a double
-value and shows this grade on screen. Check your answer with a calculator.
当我再看几页时,答案就在那里,上面写着:
The values from a, b, c and d are respectively 5, 7, 12 and 8
这是我在 MainWindow.xaml 中输入的内容:
- 两个标签内容:
- 成绩学生 1
- 成绩学生 2
- 两个文本框(已在属性中命名),因此我可以在其中输入这些值
- 带有内容计算成绩的按钮
我想要发生的是:
- 我在文本框中输入两个值,当我单击按钮时,我希望该消息框显示平均成绩
这是我到目前为止的全部代码(我只得到了一位同学的一点点帮助,因为她不在她的笔记本电脑旁):
public partial class MainWindow : Window
{
int outcomeStudent1 = 44;
int outcomeStudent2 = 51;
private void CalculateButton_Click(object sender, RoutedEventArgs e)
{
//outcomeStudent1 = 44
//outcomeStudent2 = 51
//Write a program that calculates the average grade as a double value
double variable1;
double variable2;
variable1 = 44;
variable2 = 51;
int StudentA = 44;
int StudentB = 51;
double average;
average = (double)StudentA + (double)StudentB / 2;
//2*44=88
//2*51=102
//88+102=190
//190/2=95
MessageBox.Show("Average grade = " + average);
}
public MainWindow()
{
InitializeComponent();
}
}
}
我现在迷路了,那么我该怎么做才能做到这一点?
您需要先从用户界面获取值。为此,您可以访问文本框中的值,如下所示。然后将字符串值转换为整数。
之后用 (x + y) / 2 计算平均值(你的公式是错误的)。
然后在消息框中显示结果。
下面的代码应该可以工作。请记住在 xaml 文件中创建两个文本框,分别命名为 textbox1
和 textbox2
(稍后可能会考虑更好的命名)
public partial class MainWindow : Window
{
private void CalculateButton_Click(object sender, RoutedEventArgs e)
{
int studentA = Convert.ToInt32(textbox1.Text);
int studentB = Convert.ToInt32(textbox2.Text);
double average = (studentA + studentB) / 2.0;
MessageBox.Show("Average grade = " + average);
}
public MainWindow()
{
InitializeComponent();
}
}
我正在学习成为一名游戏开发人员,我使用 C#。现在其中一项作业如下(从荷兰语翻译而来):
Two students participate in a C#-exam. Their results (a whole number of points between 0 and 100) are assigned to two variables:
int outcomeStudent1 = 44; int outcomeStudent2 = 51;
Write a program that calculates the average grade as a
double
-value and shows this grade on screen. Check your answer with a calculator.
当我再看几页时,答案就在那里,上面写着:
The values from a, b, c and d are respectively 5, 7, 12 and 8
这是我在 MainWindow.xaml 中输入的内容:
- 两个标签内容:
- 成绩学生 1
- 成绩学生 2
- 两个文本框(已在属性中命名),因此我可以在其中输入这些值
- 带有内容计算成绩的按钮
我想要发生的是:
- 我在文本框中输入两个值,当我单击按钮时,我希望该消息框显示平均成绩
这是我到目前为止的全部代码(我只得到了一位同学的一点点帮助,因为她不在她的笔记本电脑旁):
public partial class MainWindow : Window
{
int outcomeStudent1 = 44;
int outcomeStudent2 = 51;
private void CalculateButton_Click(object sender, RoutedEventArgs e)
{
//outcomeStudent1 = 44
//outcomeStudent2 = 51
//Write a program that calculates the average grade as a double value
double variable1;
double variable2;
variable1 = 44;
variable2 = 51;
int StudentA = 44;
int StudentB = 51;
double average;
average = (double)StudentA + (double)StudentB / 2;
//2*44=88
//2*51=102
//88+102=190
//190/2=95
MessageBox.Show("Average grade = " + average);
}
public MainWindow()
{
InitializeComponent();
}
}
}
我现在迷路了,那么我该怎么做才能做到这一点?
您需要先从用户界面获取值。为此,您可以访问文本框中的值,如下所示。然后将字符串值转换为整数。
之后用 (x + y) / 2 计算平均值(你的公式是错误的)。 然后在消息框中显示结果。
下面的代码应该可以工作。请记住在 xaml 文件中创建两个文本框,分别命名为 textbox1
和 textbox2
(稍后可能会考虑更好的命名)
public partial class MainWindow : Window
{
private void CalculateButton_Click(object sender, RoutedEventArgs e)
{
int studentA = Convert.ToInt32(textbox1.Text);
int studentB = Convert.ToInt32(textbox2.Text);
double average = (studentA + studentB) / 2.0;
MessageBox.Show("Average grade = " + average);
}
public MainWindow()
{
InitializeComponent();
}
}