为什么WinForms中的textBox不能显示双倍?

时间:2016-07-25 20:08:15

标签: c# windows forms types windows-forms-designer

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 P2_7_24_2016_ED_app
{
    public partial class Form1 : Form
    {
        int win;
        int loss;
        int winCounter;
        int lossCounter;
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonWin_Click(object sender, EventArgs e)
        {
            this.textBoxWin.Text = "";
            ++win;
            ++winCounter;
            this.textBoxWin.Text = win.ToString();
        }

        private void buttonLoss_Click(object sender, EventArgs e)
        {
            this.textBoxLoss.Text = "";
            ++loss;
            ++lossCounter;
            this.textBoxLoss.Text = loss.ToString();
        }

        private void buttonRate_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            double result = winCounter / (winCounter + lossCounter);
            textBox1.Text = result.ToString();
        }
    }
}

这是Windows窗体应用程序中的代码,它的简短,所以我不会解释所有内容。 问题是最终textBox1.Text = result.ToString(); 显示" 0",但它应该是一些数字。当win winCounter为1,而lossCounter为1时,它应插入1 /(1 + 1)= 0.5,但它表示" 0"。我尝试了我所知道的一切,请给我一个提示。

2 个答案:

答案 0 :(得分:1)

这是整数运算:

(winCounter/(winCounter+lossCounter))

结果不会像你期望的那样。

在进行除法之前,将winCounter(winCounter+lossCounter)转换为浮点类型(浮点数,双精度或小数,具体取决于您需要的速度/精度):

 (winCounter/(decimal)(winCounter+lossCounter))

 ((double)winCounter/(winCounter+lossCounter))

你会得到你期望的结果。

答案 1 :(得分:0)

如评论中所述,您需要 <fields> <!-- Valid attributes for fields: name: mandatory - the name for the field type: mandatory - the name of a previously defined type from the <types> section indexed: true if this field should be indexed (searchable or sortable) stored: true if this field should be retrievable multiValued: true if this field may contain multiple values per document omitNorms: (expert) set to true to omit the norms associated with this field (this disables length normalization and index-time boosting for the field, and saves some memory). Only full-text fields or fields that need an index-time boost need norms. Norms are omitted for primitive (non-analyzed) types by default. termVectors: [false] set to true to store the term vector for a given field. When using MoreLikeThis, fields used for similarity should be stored for best performance. termPositions: Store position information with the term vector. This will increase storage costs. termOffsets: Store offset information with the term vector. This will increase storage costs. default: a value that should be used if no value is specified when adding a document. --> <field name="signatureField" type="string" stored="true" indexed="true" multiValued="false" /> <field name="id" type="string" indexed="true" stored="true" required="true" /> <field name="sku" type="text_en_splitting_tight" indexed="true" stored="true" omitNorms="true"/> <field name="name" type="text_general" indexed="true" stored="true"/> <field name="alphaNameSort" type="alphaOnlySort" indexed="true" stored="false"/> <field name="manu" type="text_general" indexed="true" stored="true" omitNorms="true"/> <field name="cat" type="string" indexed="true" stored="true" multiValued="true"/> <field name="features" type="text_general" indexed="true" stored="true" multiValued="true"/> ... etc 您的结果。这应该有效:

cast