c#编程错误问题

本文关键字:问题 错误 编程 | 更新日期: 2023-09-27 18:19:02

我正在上基本的c#编程课,直到我不得不做下面的练习之前,我已经理解了所有的东西。如果有人能检查我的代码并帮助我,那就太好了。在这段代码中,我得到了三个不同的错误(CS1620、CS0266和CS1955)。我有一个同学想帮我,但结果不太好。

下面是练习提示符:

计件工人按件计酬。产出较多的工人往往得到较高的报酬。

表单:使用文本框获取人的姓名和已完成的条目数。包括一个计算命令按钮来显示所赚取的金额。您将需要一个Summary按钮来显示总件数、总支付和每人平均支付。清除按钮应该清除当前员工的姓名和件数。

包括验证以检查丢失的数据。如果用户单击Calculate按钮而没有首先输入部件的名称和数量,则显示一个消息框。此外,您需要确保在输入任何数据之前不显示摘要;如果没有计算项目,就不能计算平均值。您可以在Summary事件过程中检查员工人数,或者禁用Summary命令按钮,直到计算出第一笔订单。

Pieces completed     Price paid per piece for all pieces
      1-199                    .50
    200-399                    .55
    400-599                    .60
    600 or more                .65
代码:

public partial class pieceworkForm : Form
{
    //Declare variables.
    int tpiece = 0;
    int numemp = 0;
    float tpay = 0;
    public pieceworkForm()
    {
        InitializeComponent();
    }
    private void exitButton_Click(object sender, EventArgs e)
    {
        //Closes form.
        this.Close();
    }
    private void printButton_Click(object sender, EventArgs e)
    {
        //Prints form.
        printForm1.PrintAction = System.Drawing.Printing.PrintAction.PrintToPreview;
        printForm1.Print();
    }
    private void calcButton_Click(object sender, EventArgs e)
    {
        //Converts pieces to integer.
        int pieces = 0;
        pieces = int.Parse(piecesTextBox.Text.Trim());
        //Calculates pay based on number of pieces completed. 
        float pay = calcButton(pieces);
        //Display the formatted text in the pay label
        payMaskedTextBox.Text = string.Format("C", pay);
        payMaskedTextBox.Visible = true;
        //Counts employees on click.
        numemp = numemp + 1;
        //Total pieces made.
        tpiece = tpiece + pieces;
        //Total pay.
        tpay += pay;
        //Enable summary button.
        sumButton.Enabled = true;
    }
    private void clearButton_Click(object sender, EventArgs e)
    {
        //Clears form.
        empTextBox.Text = string.Empty;
        piecesTextBox.Text = string.Empty;
        payMaskedTextBox.Text = string.Empty;
    }
    private void callButton_Click(object sender, EventArgs e)
    {
        //Confirm clear.
        if ((MessageBox.Show("Do you want to clear this form?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes))
        {
            // Call the regular "Clear" button handler, as if it had been clicked as well.
            clearButton_Click(sender, e);
            //Reset everything to zero.
            tpiece = 0;
            tpay = 0;
            numemp = 0;
            // Make summary groupbox invisible.
            summaryGroupBox.Visible = false;
            // And disable summary button until new pay is entered.
            sumButton.Enabled = false;
            }
        }
    private void empTextBox_TextChanged(object sender, EventArgs e)
    {
        //Show message if field is empty.
        if (string.IsNullOrWhiteSpace(empTextBox.Text))
        {
            MessageBox.Show("Please enter an employee name.");
        }
    }
    private void piecesTextBox_TextChanged(object sender, EventArgs e)
    {
        //Show messgae if field is empty.
        if (string.IsNullOrWhiteSpace(piecesTextBox.Text))
        {
            MessageBox.Show("Please enter the number of pieces completed.");
        }
    }
    private float calc(int pieces)
    {
        float pay = 0;
        switch (pieces)
        {
            case 0:
                pay = 0;
                break;
            case 1: // 1 to 199
                pay = pieces * 0.5;
                break;
            case 2: // 200 to 399
                pay = pieces * 0.55;
                break;
            case 3: // 400 to 599
                pay = pieces * 0.6;
                break;
            default:
                pay = pieces * 0.65;
                break;
        }
        return pay;
    }
    private void SetcalcButtonState()
    {
        // Assume false 
        calcButton.Enabled = false;
        // Check for non-empty text
        if (((empTextBox.Text.Trim().Length > 0) & (piecesTextBox.Text.Trim().Length > 0)))
        {
            int pieces = 0;
            // TryParse will return true if the text is good as a number
            if ((int.TryParse(piecesTextBox.Text.Trim(), pieces)))
            {
                calcButton.Enabled = true;
            }
        }
    }
    private void sumButton_Click(System.Object sender, System.EventArgs e)
    {
        //Show total pieces nd pay.
        tpieceMaskedTextBox.Text = string.Format("{0}", tpiece);
        tpayMaskedTextBox.Text = string.Format("C", tpay);
        //Calculate and show average pay per employee.
        avgMaskedTextBox.Text = string.Format("C", tpiece / numemp);
        // Make the whole summary box visible
        summaryGroupBox.Visible = true;
    }
}

具体问题区域如下:

CS1955不可调用的成员'pieceworkForm。calcButton'不能像方法一样使用。

float pay = calcButton(pieces);

CS0266不能隐式地将类型"double"转换为"float"。存在显式转换(您是否缺少强制类型转换?)

private float calc(int pieces)
{
        float pay = 0;
        switch (pieces)
        {
            case 0:
                pay = 0;
                break;
            case 1: // 1 to 199
                pay = pieces * 0.5;
                break;
            case 2: // 200 to 399
                pay = pieces * 0.55;
                break;
            case 3: // 400 to 599
                pay = pieces * 0.6;
                break;
            default:
                pay = pieces * 0.65;
                break;
        }
        return pay;
}

CS1620参数2必须与'out'关键字一起传递。

 if ((int.TryParse(piecesTextBox.Text.Trim(), pieces)))
 {
     calcButton.Enabled = true;
 }

c#编程错误问题

CS1955不可调用的成员'pieceworkForm。calcButton'无法使用像一个方法。

不是调用方法,而是调用事件处理程序。你需要做的是:

float pay = calc(pieces);

CS0266不能隐式地将类型"double"转换为"float"。一个显式的存在转换(您是否缺少强制类型转换?)

你需要将实数指定为浮点数,以便将它们保存到浮点变量中,因为它们默认是双精度类型。

private float calc(int pieces)
{
    float pay = 0f;
    switch (pieces)
    {
        case 0:
            pay = 0f;
            break;
        case 1: // 1 to 199
            pay = pieces * 0.5f;
            break;
        case 2: // 200 to 399
            pay = pieces * 0.55f;
            break;
        case 3: // 400 to 599
            pay = pieces * 0.6f;
            break;
        default:
            pay = pieces * 0.65f;
            break;
    }
    return pay;
}

CS1620参数2必须与'out'关键字一起传递。

通过引用传递任何值必须使用out关键字显式定义。你可以在MSDN上阅读。

if ((int.TryParse(piecesTextBox.Text.Trim(), out pieces)))
        {
            calcButton.Enabled = true;
        }