我正在编写代码以在句子中显示正确的a / an。目前它有效,但似乎相当多余。我认为可以将一系列元音与第一个字母进行比较,但是在谷歌搜索时无法找到如何实现它。我怎么能这样做,还是有更好的方法?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CoreObjectsLibrary;
using BaseObjects;
namespace Deliverable5 {
/// <summary>
/// Interaction logic for frmItem.xaml
/// </summary>
public partial class frmItem : Window {
public frmItem(Item newItem) {
InitializeComponent();
//Create textblock to display found item's name
TextBlock itemInfo = new TextBlock();
itemInfo.TextWrapping = TextWrapping.Wrap;
//Proper a/an caps because name
if (newItem.Name[0] == 'A' || newItem.Name[0] == 'E' || newItem.Name[0] == 'I' || newItem.Name[0] == 'O' || newItem.Name[0] == 'U') {
itemInfo.Text = "You found an: " + newItem.Name;
stkItemDisplay.Children.Add(itemInfo);
}
else {
itemInfo.Text = "You found a: " + newItem.Name;
stkItemDisplay.Children.Add(itemInfo);
}
}
}
}
答案 0 :(得分:4)
您可以用这些行替换完整逻辑(如果是块)。
itemInfo.Text = string.Format("You found {0}: {1}", "aeiouAEIOU".IndexOf(newItem.Name[0]) >= 0? "an": "a" , newItem.Name);
stkItemDisplay.Children.Add(itemInfo);