好吧,我的任务是创建一个显示3个主要功能的应用程序:汽车的年份,品牌和速度。年份和品牌输入文本框,速度从0开始。
每次按下时都会有一个加速按钮,加速按钮和速度加5,制动按钮每次按下时减速5。
我无法一起使用该类和表单来显示结果。我需要在消息框中显示make,year和speed。我一直坐在这里几个小时,我无处可去。我收到了错误"当前环境中不存在速度"并且"汽车在当前环境中不存在"在我的按钮下。我不确定如何解决这个问题。
非常感谢任何和所有帮助。如果这是一团糟,我很抱歉。我以前从未上过课。
以下是表格:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Car_Class_BBrantley
{
public partial class Form1 : Form
{
private Car myCar;
public Form1()
{
myCar = new Car;
InitializeComponent();
}
private void GetCarData(Car car)
{
try
{
myCar.Make = txtMake.Text;
myCar.Year = int.Parse(txtModel.Text);
myCar.Speed = 0;
}
catch (Exception ex)
{
MessageBox.Show(string.Concat("Must enter a valid make and year model for the car. ", ex.Message, "\r\n", ex.StackTrace));
}
}
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData();
myCar.AccSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
private void btnBrake_Click(object sender, EventArgs e)
{
GetCarData();
myCar.DecSpeed(5);
MessageBox.Show(" Your car is a " + myCar.Year + myCar.Make + " and it is traveling " + myCar.Speed + " mph. ");
}
}
}
如果你想看课:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Car_Class_BBrantley
{
class Car
{
private int year;
private string make;
private int speed;
public Car()
{
this.year = 1994;
this.make = "Ford";
this.speed = 0;
}
public Car(string make, int year, int speed)
{
this.year = year;
this.make = make;
this.speed = speed;
}
public string Make
{
get { return make; }
set { make = value; }
}
public int Year
{
get { return Year; }
set { Year = value; }
}
public int Speed
{
get { return speed; }
set { speed = value; }
}
public void AccSpeed(int speedIncrement)
{
//Add check for speed limit ranges
Speed += speedIncrement;
}
public void DecSpeed(int speedDecrement)
{
//Add check for speed limit ranges
Speed -= speedDecrement;
}
}
}
答案 0 :(得分:0)
您必须将车辆存放在表格中的某个位置。如果你需要使用更多的汽车,你应该有一些收集,根据一些参数选择正确的汽车。
一个简单的例子是:
public partial class Form1 : Form
{
private Car _car;
public Form1()
{
_car = new Car();
InitializeComponent();
}
...
然后在表格中使用已经创建的汽车实例,即
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData(_car);
_car.Accelerate(); //note the way you were using won't work on this line
MessageBox.Show(" Your car is a " + _car.year + _car.make + " and it is traveling " + speed + " mph. ");
}
答案 1 :(得分:0)
您尚未创建Car
类的实例。
在这种方法中:
private void btnAcc_Click(object sender, EventArgs e)
{
GetCarData(car);
car.Accelerate += speed;
MessageBox.Show(" Your car is a " + car.year + car.make + " and it is traveling " + speed + " mph. ");
}
您正在尝试使用car
(GetCarData(car)
)做一些事情,但......什么车?
在表单级别创建Car类的实例:
public partial class Form1 : Form
Car car = new Car();
.
.
答案 2 :(得分:0)
在我看来,您可能需要添加另一个按钮来设置汽车的初始设置,在该事件处理程序方法中,您可以通过调用GetCarData()来初始化汽车的年份,速度和模型信息。然后,从DecSpeed()和AccSpeed()中删除GetCarData(),因为您不希望每次加速或减速时重新初始化。