如何将字符串转换为int?

格兰特霍普金斯

我在摆弄 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;
        }
    }
}
杰森
int sz = int.Parse(diceSize.Text);    
Output.Text = diceRollNorm(sz).ToString();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章