我需要找到一个代码,以便从列表框中的最小数字中减去最大数字。我添加了属性中的列表框项目,如果有人知道任何事情,请帮助我,我尝试了这段代码
int lowestattendance = attenadances.Max();
string lowestname = "";
int index = 0;
while (index < attenadances.Count) ;
{
if (attenadances[index] < lowestattendance)
{
lowestattendance = attenadances[index];
lowestname = Names[index];
}
index++;
}
string message = string.Format(
"the person with the lowest attendance is {0}" + "and his attendance is { 1}",
lowestname,
lowestattendance);
MessageBox.Show(message, " lowest attendance");
它从未显示任何内容。
答案 0 :(得分:0)
您正在寻找ArgMin
功能;即使 Linq 没有这样的东西,你也可以在Aggregate
的帮助下轻松实现它:
using System.Linq;
...
//TODO: typo - attendances, not attenadances
var lowest = attenadances
.Select((value, index) => new {
attendance = value,
name = Names[index], })
.Aggregate((s, a) => s.attendance < a.attendance ? s : a);
//DONE: { 1} should be {1}
//DONE: can a woman be the top truant? - his/her
string message = string.Format(
"the person with the lowest attendance is {0} and his/her attendance is {1}",
lowest.name,
lowest.attendance);
//DONE: leading space eliminated
MessageBox.Show(message, "lowest attendance");
如果您不想 Linq 但循环实施,我建议使用for
而不是while
:
int lowestattendance = 0;
string lowestname = null;
for (int i = 0; i < Attenadances.Count; ++i)
// change min: on the very 1st item; if next item is less than current min
if (i == 0 || attenadances[i] < lowestattendance) {
lowestattendance = attenadances[i];
lowestname = Names[i];
}
string message = string.Format(
"the person with the lowest attendance is {0} and his/her attendance is {1}",
lowestname,
lowestattendance);
MessageBox.Show(message, "lowest attendance");
答案 1 :(得分:0)
根据您发布的代码,格式化邮件时会收到FormatException
。原因是您的占位符{ 1}
不允许使用前导空格。
请更换格式化代码行
string message = string.Format(
"the person with the lowest attendance is {0}" +
"and his attendance is {1}", // change here from { 1} to {1}
lowestname, lowestattendance);
如果您有其他问题,请在您的问题中更具体。
答案 2 :(得分:0)
DonationsListFragment
作为public void refreshDonations() {
apiClient.donationCurrentDonations();
refreshLayout.setRefreshing(true);
}
循环可以更好地工作,就像这样
<?php
include("misc.inc");
session_start();
$connect = mysqli_connect($host,$user,$password,$database) or die ("Couldn't connect database");
$sql = "SELECT * FROM ad ORDER BY training_start desc";
$result = mysqli_query($connect,$sql) or die("Couldn't execute query.");
echo "<table cols='4' cellspacing='25'>";
echo "<th>ID</th>";
echo "<th>Training Name</th>";
echo "<th>Trainer Name</th>";
echo "<th>Training Dates</th>";
echo "<th>Registration Starts</th>";
while($row=mysqli_fetch_assoc($result))
{
extract($row );
echo "<tr><td >$id</td>";
echo "<td ><a href='ad_details.php'>$training_name</a></td>";
echo "<td >$trainer_name</td>";
echo "<td >$training_start - $training_end</td>";
echo "<td >$reg_start</td></tr>";
}
echo "</table>";
?>
请注意我是如何删除分号的?
while
变成for
这是因为多余的分号计为循环结束,因此计算机正在看
for (int i = 0; i < attenadances.Count; i++)
{
if (attenadances[i] < lowestattendance)
{
lowestattendance = attenadances[i];
lowestname = Names[i];
}
}
。因此,它永远不会做任何事情!