c# - 为什么 char.Equals() 方法不适用于“.”、“+”、“-”、“/”或“*”
问题描述
我正在尝试使用 WPF 在 C# 中构建一个计算器,我需要知道显示器(System.Windows.Controls.TextBlock 对象)中是否有一些运算符,如 +、-、/ 或 *。
我命名为“Display”的 TextBlock:
<TextBlock Name="Display" Text="0" FontFamily="Consolas" FontSize="60"
HorizontalAlignment="Right" VerticalAlignment="Bottom"
Padding="15" Height="120"></TextBlock>
当我调用 TextBlock.Text(返回一个字符串)属性并尝试将该字符串(一个字符)的某个索引与另一个字符(.、+、-、/、*)与 char.Equals( ) 方法:
private void Dot_Click(object sender, RoutedEventArgs e)
{
if (Display.Text.Length < 20 & !".".Equals(Display.Text[Display.Text.Length - 1])) Display.Text += ".";
}
我希望当显示中的最后一个字符是一个点时,用户不能写更多的点。像这样:
但刚刚发生了这样的事情:
当我输入 +、-、* 或 / 时也会出现同样的问题
感谢您的帮助!
解决方案
您应该与 char 进行比较。您正在尝试比较字符串和字符。"."
是一个字符串并且'.'
是一个字符。
private void Dot_Click(object sender, RoutedEventArgs e)
{
if (Display.Text.Length < 20 & !'.'.Equals(Display.Text[Display.Text.Length - 1])) Display.Text += ".";
}
推荐阅读
- google-apps-script - Folder.searchFiles() 方法引发“无效参数:q”错误
- javascript - 给定一个预定义的变量,我如何在 jQuery 中计数到某个数字
- azure - 使用 Google ID(例如联合)的 Azure B2B 是否仍需要在 Azure AD 中创建对象
- java - 找不到错误我对makeBricks问题java的解决方案
- java - spring boot maven编译包不存在
- javascript - 访问控制允许来源 Apache SVN 端点
- networking - 为什么我在使用wireshark 捕获包时收到了不止一个客户端的问候?
- sql-server - 使用 xs:element 命名空间标签选择 xml 格式的 SQL Server 表内容
- python - WSO2 api 管理器未将上传的多个文件发送到后端服务器
- sql-server - SQL Server 组行号