我正在嘗試創建一個簡單的表格來計算速度、距離和旅行時間,但計算結果是錯誤的。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _2ndTask
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
double speed, time, distance, laiks, garums, atrums; // laiks = time2, garums = distance2, atrums = speed2
private void label1_Click_1(object sender, EventArgs e)
{
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
bool timeInput = double.TryParse(textBox3.Text, out laiks);
if (!timeInput || laiks < 0 || laiks > 9999)
{
// MessageBox.Show("Ignorēt ?o pazi?ojumu.");
}
// timeInput = float.Parse (textBox3.Text);
}
private void textBox2_TextChanged2(object sender, EventArgs e)
{
bool cGarumsInput = double.TryParse(textBox2.Text, out garums);
if (!cGarumsInput || garums < 0 || garums > 999999)
{
// MessageBox.Show("Ignorēt ?o pazi?ojumu.");
}
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
bool speedInput = double.TryParse(textBox3.Text, out atrums);
if (!speedInput || atrums < 0 || atrums > 9999)
{
// MessageBox.Show("Ignorēt ?o pazi?ojumu.");
}
}
private void button2_Click(object sender, EventArgs e)
{
time = (garums / atrums);
label4.Text = time.ToString();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
distance = atrums * laiks;
label4.Text = distance.ToString() " Km.";
}
private void button1_Click(object sender, EventArgs e)
{
speed = garums / laiks;
label4.Text = speed.ToString() " Km/h.";
}
}
}
在我添加 TryParse 之前,計算是正確的,但是只要從文本框中洗掉值,表單就會崩潰。
它應該在下面輸出 140(乘以時間和速度)而不是輸出 49。輸入其他數字也會給出錯誤的結果

uj5u.com熱心網友回復:
你有一個錯字。您正在將 textBox3 中的文本分配給 atrums 和 laiks。大概其中之一應該使用 textBox1 中的文本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/377586.html
