[英]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
变量应用数学运算,因此您需要int
或double
类型。 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.ToInt32
将TextBox.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
变量,将它们替换为数值( int
、 decimal
、 double
等在适当的情况下),这些值通过使用.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.