简体   繁体   English

C# 错误运算符 * 不能应用于“字符串”和“字符串”类型的操作数

[英]C# ERROR Operator * cannot be applied to operands of type 'string' and 'string'

public partial class Form1 : Form
{
    string principal, rate, years;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        principal = loantextBox4.Text;
        rate = ratetextBox5.Text;
        years = yearworktextBox2.Text;

        outputlabel5.Text = principal * ((rate * (1 + rate) ^ years) / ((rate + 1) ^ years) - 1))));

What needs to be fixed I keep getting an error saying operator "*" cannot be applied to operands of type 'string' and 'string'.需要解决的问题 我不断收到错误消息,说运算符“*”不能应用于“字符串”和“字符串”类型的操作数。

You cannot apply mathematical operations on a string variable and thus you need an int or double type.您不能对string变量应用数学运算,因此您需要intdouble类型。 I have used int .我用过int Secondly When you try to get the TextBox data in the int variable it would not be possible because TextBox.Text would return a string.其次,当您尝试在 int 变量中获取 TextBox 数据时,这是不可能的,因为TextBox.Text将返回一个字符串。 Now You can convert the TextBox.Text to a int using Convert.ToInt32 .现在您可以使用Convert.ToInt32TextBox.Text转换为int If you try to put the values of the Textbox to int variable without conversion, it will give you an error stating如果您尝试将Textbox的值放入 int 变量而不进行转换,则会出现错误说明

Cannot implicitly convert type 'string' to 'int'无法将类型“string”隐式转换为“int”

and it would not even compile the program.它甚至不会编译程序。

Whereas letz say if the user input in not a number and it we try to Convert it.而 letz 说如果用户输入的不是数字,我们尝试转换它。 For example the user input is "Mohit" and when it tries to convert the Convert.ToInt32("Mohit");例如,用户输入是“Mohit”,当它尝试转换Convert.ToInt32("Mohit"); it will throw an exception.它会抛出异常。 stating陈述

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll mscorlib.dll 中发生类型为“System.FormatException”的未处理异常

You can avoid getting exception or to handle the wrongly input from the user you can use Try and Catch Block您可以避免出现异常或处理来自用户的错误输入,您可以使用Try and Catch Block

Hope this code will give you the idea希望这段代码能给你这个想法

public partial class Form1 : Form
{
    int principal, rate, years;
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        principal = Convert.ToInt32(loantextBox4.Text);
        rate = Convert.ToInt32(ratetextBox5.Text);
        years = Convert.ToInt32(yearworktextBox2.Text);

        outputlabel5.Text = (principal * ((rate * (1 + rate) ^ years) / ((rate + 1) ^ years) - 1)).ToString();

You can't multiply strings.你不能乘以字符串。 You can , however, multiply numbers.但是,您可以将数字相乘。 Convert your numeric strings to numbers.将数字字符串转换为数字。 For example:例如:

int years;
if (!int.TryParse(yearworktextBox2.Text, out years))
{
    // string value wasn't numeric, maybe show an error?
    // probably return from here as well, since the logic can't continue
}
// here the "years" value can be used for calculations

Remove the string variables you have, replace them with numeric values ( int , decimal , double , etc. where appropriate) which are populated by parsing the input strings using .TryParse() (and handling errors accordingly), then you can use those values in your calculations.删除您拥有的string变量,将它们替换为数值( intdecimaldouble等在适当的情况下),这些值通过使用.TryParse()解析输入字符串填充(并相应地处理错误),然后您可以使用这些值在你的计算中。

Because all of your inputs are strings.因为您的所有输入都是字符串。 You can't multiply a string.你不能乘以一个字符串。

What you need to do instead is change them to a number format.您需要做的是将它们更改为数字格式。 Probably just int should be fine.可能只是int应该没问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误:operator&#39;!=&#39;不能应用于&#39;char&#39;和&#39;string&#39;类型的操作数 - C# error: operator '!=' cannot be applied to operands of type 'char' and 'string' C#:operator&#39; - &#39;不能应用于&#39;string&#39;和&#39;int&#39;类型的操作数错误 - C#: operator '-' cannot be applied to operands of type 'string' and 'int' error 运算符&#39;&gt;&#39;和&#39;&lt;&#39;不能应用于类型为&#39;string&#39;和&#39;string&#39;的操作数C# - Operator '>' and '<' cannot be applied to operands of type 'string' and 'string' C# 运算符“+”不能应用于“字符串”和“无效”类型的操作数 C# WebMethod - Operator '+' cannot be applied to operands of type 'string' and 'void' C# WebMethod C# - 运算符&#39;==&#39;不能应用于&#39;char&#39;和&#39;string&#39;类型的操作数 - C# - Operator ‘==’ cannot be applied to operands of type ‘char’ and ‘string’ ASP.NET C#错误:运算符*不能应用于类型为&#39;int&#39;和&#39;string&#39;的操作数 - ASP.NET C# error: Operator * cannot be applied to operands of type 'int' and 'string' 运算符“-”不能应用于字符串和字符串类型的操作数 - Operator '-' cannot be applied to operands of type string and string 运算符“&amp;”不能应用于“字符串”和“字符串”类型的操作数 - Operator '&' cannot be applied to operands of type 'string' and 'string' 运算符&lt;=不能应用于string和string类型的操作数 - operator <= cannot be applied to operands of type string and string 运算符“&amp;&amp;”不能应用于“字符串”和“字符串”类型的操作数 - operator '&&' cannot be applied to operands of type 'string' and 'string'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM