不能将'十进制'类型转换为'int'。存在显式转换(您是否错过了演员?) [英] Cannot implicity convert type 'decimal' to 'int'. An explicit conversion exists (are you missing a cast?)

查看:91
本文介绍了不能将'十进制'类型转换为'int'。存在显式转换(您是否错过了演员?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先我要说的是,在你抨击我的代码有多糟糕之前我是非常的老将,这是我写过的第一个程序,我今天开始学习。 我遇到的错误是在第27行



如果我可以在我当前的问题中提出另一个问题。放置全局变量的正确位置在哪里?如你所见,我认为我把一个放在form1中。我不知道还能把它放在哪里。我正在阅读和观看一些教程,但并非所有问题都得到了解答。 :/



First I want to say that I am very noob before you go bashing at how bad my code is, this is the first program I have ever written and I started learning just today. The error I have is on row 27

Also if I could ask another question in my current question. Where is the correct place to put global variables? As you see I put one inside form1 I think. I''m not sure where else to put. I am reading and watching some tutorials but not all my questions are answered. :/

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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        int currentmana = 200;
        public Form1()
        {
            InitializeComponent();
        }

        
        private void button1_Click(object sender, EventArgs e)
        {
            if (currentmana >= manareq.Value && currentmana == manaactive.Value)
            {
                label1.Text = spell.Text;
                currentmana = currentmana - manareq.Value;
            }
            else if (currentmana != manareq.Value || manaactive.Value > currentmana)
            {
               MessageBox.Show("You don't have enough mana");
            }

        }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            label2.Text = (currentmana.ToString());

        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            currentmana = 1 + currentmana;
        }
    }
}

推荐答案

如果没有转换,您不能将一种类型设置为另一种类型显性演员。我假设在行:

You cannot set one type to another without a conversion or explicit cast. I assume that in the line:
currentmana = currentmana - manareq.Value;



manareq.Value 是十进制类型,因此需要将其转换为整数:


that manareq.Value is the decimal type, so you need to cast it to integer thus:

currentmana = currentmana - (int)manareq.Value;



参见 http://msdn.microsoft.com/en-us/library/yht2cx7b(VS.80).aspx [ ^ ]有关显式转换的更多信息。


See http://msdn.microsoft.com/en-us/library/yht2cx7b(VS.80).aspx[^] for further information on explicit casting.


显式转换是需要将十进制转换为整数。

使用 Convert.ToInt [ ^ ]。



Eg currentmana = currentmana - Convert.ToInt32(manareq.Value);
An explicit conversion is required to convert decimal to integer.
Use Convert.ToInt[^].

E.g. currentmana = currentmana - Convert.ToInt32(manareq.Value);


这篇关于不能将'十进制'类型转换为'int'。存在显式转换(您是否错过了演员?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆