C# 代码在从 int 转换为 String 时返回奇怪的数字
C# code returning weird numbers when converting from int to String
我创建了一个时间 class 来跟踪小时、分钟和 am/pm。
出于某种原因,当我尝试输出标签中的文本并尝试将我的整数转换为字符串时,我得到了非常奇怪的结果。例如,当标签应显示数字 12
时,它会显示 -35791382
。我相信这与 toString()
方法有关,但我不能确定。有人知道为什么会这样吗? .toString()
和 Convert.ToString()
我都试过了。
protected void btngo_Click(object sender, EventArgs e)
{
int hours = Convert.ToInt16(ddl2.Text);
int minutes = Convert.ToInt16(ddl3.Text);
bool am;
if (ddl4.Text == "AM")
{
am = true;
}
else
{
am = false;
}
Time start = new Time(hours, minutes, am);
Time t1 = start;
if (ddl1.Text == "What time do you need to wake up?")
{
t1.hours -= 1;
t1.minutes -= 30;
t1.Clean();
}
lbl3.Text = t1.hours.ToString() + ":" + Convert.ToString(t1.minutes) + " " + Convert.ToString(t1.am);
}
public class Time
{
public int hours;
public int minutes;
public bool am;
public Time(int hours, int minutes, bool am)
{
this.hours = hours;
this.minutes = minutes;
this.am = am;
}
public void Clean(){
while (this.minutes > 59)
{
this.hours += 1;
this.minutes -= 60;
if (this.hours > 12)
{
if (this.am == true)
{
this.am = false;
}
else
{
this.am = true;
}
this.hours -= 12;
}
}
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes -= 60;
}
if (this.hours < 1)
{
if (this.am == true)
{
this.am = false;
}
else
{
this.am = true;
}
this.hours += 12;
}
}
如果进入此块,将抛出溢出异常:
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes -= 60;
}
...因为分钟数将永远减少 60。您是否在本示例中未显示的某个地方埋下了异常?我怀疑当超过 Int16 的最小允许值 (-32768) 时会引发异常。
它可能不能解释一切,但至少是这样的:
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes -= 60;
}
显然是错误的。我假设你想做
this.minutes += 60;
正在尝试纠正您其余的逻辑:
if (this.minutes > 59)
{
while (this.minutes > 59)
{
this.hours += 1;
this.minutes -= 60;
}
}
else if (this.minutes < 0)
{
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes += 60;
}
}
if (this.hours <= 0)
{
this.hours += 12;
this.am = !this.am;
}
else if (this.hours > 12)
{
this.hours -= 12;
this.am = !this.am;
}
我创建了一个时间 class 来跟踪小时、分钟和 am/pm。
出于某种原因,当我尝试输出标签中的文本并尝试将我的整数转换为字符串时,我得到了非常奇怪的结果。例如,当标签应显示数字 12
时,它会显示 -35791382
。我相信这与 toString()
方法有关,但我不能确定。有人知道为什么会这样吗? .toString()
和 Convert.ToString()
我都试过了。
protected void btngo_Click(object sender, EventArgs e)
{
int hours = Convert.ToInt16(ddl2.Text);
int minutes = Convert.ToInt16(ddl3.Text);
bool am;
if (ddl4.Text == "AM")
{
am = true;
}
else
{
am = false;
}
Time start = new Time(hours, minutes, am);
Time t1 = start;
if (ddl1.Text == "What time do you need to wake up?")
{
t1.hours -= 1;
t1.minutes -= 30;
t1.Clean();
}
lbl3.Text = t1.hours.ToString() + ":" + Convert.ToString(t1.minutes) + " " + Convert.ToString(t1.am);
}
public class Time
{
public int hours;
public int minutes;
public bool am;
public Time(int hours, int minutes, bool am)
{
this.hours = hours;
this.minutes = minutes;
this.am = am;
}
public void Clean(){
while (this.minutes > 59)
{
this.hours += 1;
this.minutes -= 60;
if (this.hours > 12)
{
if (this.am == true)
{
this.am = false;
}
else
{
this.am = true;
}
this.hours -= 12;
}
}
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes -= 60;
}
if (this.hours < 1)
{
if (this.am == true)
{
this.am = false;
}
else
{
this.am = true;
}
this.hours += 12;
}
}
如果进入此块,将抛出溢出异常:
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes -= 60;
}
...因为分钟数将永远减少 60。您是否在本示例中未显示的某个地方埋下了异常?我怀疑当超过 Int16 的最小允许值 (-32768) 时会引发异常。
它可能不能解释一切,但至少是这样的:
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes -= 60;
}
显然是错误的。我假设你想做
this.minutes += 60;
正在尝试纠正您其余的逻辑:
if (this.minutes > 59)
{
while (this.minutes > 59)
{
this.hours += 1;
this.minutes -= 60;
}
}
else if (this.minutes < 0)
{
while (this.minutes < 0)
{
this.hours -= 1;
this.minutes += 60;
}
}
if (this.hours <= 0)
{
this.hours += 12;
this.am = !this.am;
}
else if (this.hours > 12)
{
this.hours -= 12;
this.am = !this.am;
}