如何将字符串转换为int?

时间:2017-05-02 14:23:57

标签: c# xamarin random

我正在乱用xamarin,并遇到了这个问题。我正在用C#制作骰子滚动应用程序。但是,我无法弄清楚如何将Android.Widget.EditText转换为int,以便我可以运行diceRollNorm方法。

using Android.App;
using Android.Widget;
using Android.OS;
using System;

namespace TesticleApp
{
    [Activity(Label = "Dice Roller", MainLauncher = true, Icon = "@drawable/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Add new content
            // Turn the design widgets into a variable that you can manipulate
            EditText diceSize = FindViewById<EditText>(Resource.Id.diceSizeNum);
            EditText numOfRolls = FindViewById<EditText>(Resource.Id.numOfRolls);
            CheckBox averageBox = FindViewById<CheckBox>(Resource.Id.checkBox1);
            CheckBox totalBox = FindViewById<CheckBox>(Resource.Id.checkBox2);
            CheckBox TAABox = FindViewById<CheckBox>(Resource.Id.checkBox3);
            TextView Output = FindViewById<TextView>(Resource.Id.textView1);

            Output = ToString(diceRollNorm(diceSize));
        }

        /// <summary>
        /// This is the function that actual "Rolls the dice", it takes DSize as the how many sides the "Dice" have.
        /// The numRoll is the amount of times you would roll the die
        /// </summary>
        /// <param name="DSize"></param>
        /// <param name="numRoll"></param>
        /// <returns></returns>
        public int diceRollNorm(int DSize, int numRoll)
        {
            Random rnd = new Random();
            Array Total;

            for (int i = 1; i <= numRoll; i++)
            {
                int dice = rnd.Next(1, DSize + 1);
                // Find way to insert output of the dice
                return dice;
            }
            return 0;
        }

        /// <summary>
        /// The overload function for diceRollNorm()
        /// </summary>
        /// <param name="Dsize"></param>
        /// <returns></returns>
        public int diceRollNorm(int Dsize)
        {
            Random rnd = new Random();

            int dice = rnd.Next(1, Dsize + 1);

            return dice;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

int sz = int.Parse(diceSize.Text);    
Output.Text = diceRollNorm(sz).ToString();