error:System.FormatException:'输入字符串的格式不正确 [英] error: System.FormatException: 'Input string was not in a correct format

查看:123
本文介绍了error:System.FormatException:'输入字符串的格式不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我在尝试创建一个表格时会出错,该表格将随机显示2个玩家的3个骰子值,并将比较数字和显示谁赢了。运行得很好但是在我放入IF语句后它开始显示错误。代码运行
但是当我点击"Play!"时它会返回错误。按钮。

I am getting a error when trying to create a Form that will randomly display 3 values of 3 Dice for 2 players and will compare the numbers and displays who won. The is running great but it started show me an error after I put the IF statement. The code runs but it returns the error when I click on "Play!" button.

问题是我在"错误列表"中没有收到任何错误。所以程序正在运行但是当我点击"播放"时它会冻结并弹出错误消息"System.FormatException:'输入字符串格式不正确"
在该行: 

The problem is that I am not getting any error in the "Error List" so the program is running but when I click on "Play" it freezes and pops up the error message "System.FormatException: 'Input string was not in a correct format" at the line: 

int P1R = Convert.ToInt32(Player1_Result.Text); 。我相信我的问题是在Int和String ...我运行调试器但没有到达任何地方。 

int P1R = Convert.ToInt32(Player1_Result.Text); . I believe that my issue is in Int and String... I ran the debugger but not getting anywhere. 

你能帮忙吗...谢谢

Dice1_input_Player1.Text = dice_Player1 [0] .ToString();

Dice2_input_Player1.Text = dice_Player1 [1] .ToString();

Dice3_input_Player1.Text = dice_Player1 [2] .ToString();



Dice1_input_Player2.Text = dice_Player2 [0] .ToString();

Dice2_input_Player2.Text = dice_Player2 [1] .ToString();

Dice3_input_Player2.Text = dice_Player2 [2] .ToString();



Player1_Result.Text = string.Format(" Player 1:{0} {1} {2}",Dice1_input_Player1.Text,Dice2_input_Player1.Text,Dice3_input_Player1.Text); < br style ="color:#666666; font-family:'Segoe UI',Arial,sans-serif; font-size:11px">
Player2_Result.Text = string.Format(" Player 2:{0} {1} {2}",Dice1_input_Player2.Text,Dice2_input_Player2.Text,Dice3_input_Player2.Text); < br style ="color:#666666; font-family:'Segoe UI',Arial,sans-serif; font-size:11px">


int P1R = Convert.ToInt32(Player1_Result.Text);

int P2R = Convert.ToInt32(Player2_Result.Text);



if(P1R> = P2R)

Console.Write(" Player 1 Wins!");

else

Console.Write(" Player 2 Wins!");

Dice1_input_Player1.Text = dice_Player1[0].ToString();
Dice2_input_Player1.Text = dice_Player1[1].ToString();
Dice3_input_Player1.Text = dice_Player1[2].ToString();

Dice1_input_Player2.Text = dice_Player2[0].ToString();
Dice2_input_Player2.Text = dice_Player2[1].ToString();
Dice3_input_Player2.Text = dice_Player2[2].ToString();

Player1_Result.Text = string.Format("Player 1: {0}{1}{2}", Dice1_input_Player1.Text, Dice2_input_Player1.Text, Dice3_input_Player1.Text);
Player2_Result.Text = string.Format("Player 2: {0}{1}{2}", Dice1_input_Player2.Text, Dice2_input_Player2.Text, Dice3_input_Player2.Text);

int P1R = Convert.ToInt32(Player1_Result.Text);
int P2R = Convert.ToInt32(Player2_Result.Text);

if (P1R >= P2R)
Console.Write("Player 1 Wins!");
else
Console.Write("Player 2 Wins!");

推荐答案

"Convert.ToInt32(Player1_Result.Text)"中的FormatException表示Player1_Result .Text没有包含可以解释为Int32的正确文本。常见的错误是将文本框留空。这不会被解释为零,
它会抛出错误。此外,其他任何不是整数(如字母,小数点或空格)将抛出formatException。使用调试器并在该行中放置一个断点并检查您要转换的文本以查看
中的内容。
A FormatException in "Convert.ToInt32(Player1_Result.Text)" means that Player1_Result.Text did not contain a correct text that could be interpreted as an Int32. A common mistake is to leave the textbox blank. This is NOT interpreted as zero, it throws an error. Also, anything else that is not an integer such as letters, a decimal point or spaces will throw a formatException. Use the debugger and place a breakpoint in that line and examine the text that you are trying to convert to see what's in there.


这篇关于error:System.FormatException:'输入字符串的格式不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆