Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加硬币时需要计算_C# - Fatal编程技术网

C# 添加硬币时需要计算

C# 添加硬币时需要计算,c#,C#,我正在建造一台自动售货机,我一直在添加硬币 当我点击指定的按钮时,硬币应该会自动计算,但我只是在文本框中获取值,这是代码 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;
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 VendingMachine
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void operator_Click(object sender, EventArgs e)
        {
              new Form2().Show();
              this.Hide();
        }



        private void fiveP_Click(object sender, EventArgs e)
        {

            balance.Text = ((double)balance.Text + 0.05).ToString();

        }

        private void tenP_Click(object sender, EventArgs e)
        {
            balance.Clear();
            balance.Text = balance.Text + "0.10";

        }

        private void twentyP_Click(object sender, EventArgs e)
        {
            balance.Clear();
            balance.Text = balance.Text + "0.20";

        }

        private void fiftyP_Click(object sender, EventArgs e)
        {
            balance.Clear();
            balance.Text = balance.Text + "0.50";

        }

        private void onePound_Click(object sender, EventArgs e)
        {
            balance.Clear();
            balance.Text = balance.Text + "1.00";

        }

        private void twoPound_Click(object sender, EventArgs e)
        {
            balance.Clear();
            balance.Text = balance.Text + "2.00";

        }

    }
}
硬币等级

using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Serialization;

namespace VendingMachine
{
    [Serializable]
    internal class Coin : ISerializable, IComparable
    {
        public string coinName = "";
        public double coinValue = 0.0;
        public int coinBalance = 0;

        public string Name
        {
            get { return this.coinName; }
            set { this.coinName = value; }
        }

        public double Value
        {
            get { return this.coinValue; }
            set { this.coinValue = value; }
        }

        public int Balance
        {
            get { return this.coinBalance; }
            set { this.coinBalance = value; }
        }

        public Coin(string coin_name)

        { this.coinName = coin_name; }

        public Coin(SerializationInfo info, StreamingContext ctxt)
        {
            this.coinValue = (double)info.GetValue("CoinValue", typeof(double));
            this.coinName = (string)info.GetValue("CoinName", typeof(string));
        }

        public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
        {
            info.AddValue("CoinValue", this.coinValue);
            info.AddValue("CoinName", (object)this.coinName);
        }

        public int CompareTo(object obj)
        {
            if (obj is Coin)
                return this.Value.CompareTo(((Coin)obj).Value);
            else
                throw new ArgumentException("object is not a Coin");
        }

        public static IComparer sortByCoinName()
        {
            return (IComparer)new Coin.sortByCoinNameHelper();
        }

        private class sortByCoinNameHelper : IComparer
        {
            int IComparer.Compare(object a, object b)
            {
                return ((Coin)a).Name.CompareTo(((Coin)b).Name);
            }
        }
    }
}

您正在添加字符串而不是数字。您需要将字符串转换为数字以将它们相加,并且不应首先清除结果

例如:

private void fiveP_Click(object sender, EventArgs e)
        {
            // balance.Clear();    <- You don't need this.
            balance.Text = ((double)balance.Text + 0.05).ToString();
        }
private void fiveP_Click(对象发送方,事件参数e)
{

//balance.Clear();如果balance是您正在使用的文本框,则您将使用按钮清除它,删除按钮按下之前在其中的内容,因此它将始终是
“+
任何内容。此外,您不能添加这样的字符串,
“1.00”+“2.00”==“1.002.00”,“3.00”

这是因为您正在使用字符串类型进行计算。计算内容和显示内容之间存在差异。简而言之:

float value = 0;

void print()
{
   balance.Text = string.Format("{0:0.00}", value);
}

private void fiveP_Click(object sender, EventArgs e) 
{ 
    value += 0.05f;
    print();
}

private void tenP_Click(object sender, EventArgs e)
{
    value += 0.10f;
    print();

}

private void twentyP_Click(object sender, EventArgs e)
{
    value += 0.20f;
    print();

}

private void fiftyP_Click(object sender, EventArgs e)
{
   value += 0.50f;
    print();

}

private void onePound_Click(object sender, EventArgs e)
{
    value += 1;
    print();

}

private void twoPound_Click(object sender, EventArgs e)
{
    value += 2;
    print();

}

在代码中不添加数字,例如: 您有以下代码:

private void tenP_Click(object sender, EventArgs e)
{
    balance.Clear();
    balance.Text = balance.Text + "0.10";

}
余额的值。Text=“0.10”

然后,如果执行下一个代码:

private void fiftyP_Click(object sender, EventArgs e)
{
    balance.Clear();
    balance.Text = balance.Text + "0.50";
}
余额的值。Text=“0.50”


关键是您正在尝试添加字符串而不是数字,并且您正在清除以前的值

您知道您正在进行字符串添加(串联)没有算术运算在这个代码中你在哪里引用
硬币
类?你的硬币类如何适应这个?它没有出现在你的问题中。什么类型是
余额
?余额不能是
int
int
没有
.Text
属性我看到你添加了
硬币
cl屁股,但我还是回到我的原始观点,按钮的代码根本没有与之交互。我尝试过这种方法,但仍然不起作用。错误无法将类型“string”转换为“double”,那么字符串中有什么?如果不能将其转换为数字,则可能无法将其相加。这些是变量我的硬币类别的私有字符串coinName=“”;private-double-coinValue=0.0;private-int-coinBalance=0;这些与您发布的代码有什么关系?要么您没有发布完整的代码示例,要么您没有引用它们,如果它们是私有的,您无论如何都无法在Coin类之外访问它们。我想与其尝试将字符串转换为double,@FuzzyBear应该使用
double.Parse(balance.Text)