在C#中将字符串转换为int [英] Converting strings to int in C#

查看:92
本文介绍了在C#中将字符串转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#相当陌生,我想做一个纸牌游戏.我所拥有的是一张卡片(字符串)列表,其名称类似于 c6,s3,h11,d13 ,其中 character代表颜色 number代表值.按下按钮时,程序将从列表中获取随机字符串,并将其显示在文本框中.从那里开始,游戏的目的是猜测下一张随机卡比前一张卡具有更高的价值还是更低的价值.

im pretty new to C# and i want to make a cardgame. What i have is a list of cards (strings) with names like c6, s3, h11, d13 where the character represents the colour and the number represents the value. When pressing a button the program takes a random string from the list and displays it in a textbox. From there the point of the game is to guess if the next random card will have a higher value or a lower value then the previous card.

我想要做的是从文本框中获取字符串并将其转换为整数,以便可以将上一张卡的值与新卡的值进行比较.唯一的问题是如何摆脱 c6 中的 c ,以便可以使用 parse 进行转换.

What i want to do is to take the string from the textbox and turn it into a int so that i can compare the value of the previous card with the new one. Only problem is how do i get rid of the c in c6 so i can convert it by using parse.

这是我的代码.

public partial class MainWindow : Window
{
    static Random rndmlist = new Random();
    Random rndm = new Random();
    List<string> deck = new List<string>();
    int score = 0;
    public MainWindow()
    {
        InitializeComponent();
    }

    private void test_Click(object sender, RoutedEventArgs e)
    {
        //disregard this
        foreach (string j in deck)
        {
            testbox.Text += j + ", ";
        }
    }

    private void btnstart_Click(object sender, RoutedEventArgs e)
    {
        //this is where i add all the cards to the list
        for (int i = 1; i <= 13;)
        {
            deck.Add("c" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("s" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("h" + i);
            i++;
        }
        for (int i = 1; i <= 13; )
        {
            deck.Add("d" + i);
            i++;
        }
    }

    private void btnbegin_Click(object sender, RoutedEventArgs e)
    {
        //this is where i take a random card from the list and display it in textBox2
        int r = rndmlist.Next(deck.Count);
        textBox2.Text = ((string)deck[r]);

        //disregard this
        testbox.Text += ((string)deck[r]) + ", ";
        deck.Remove((string)deck[r]);
    }

    private void btnhigh_Click(object sender, RoutedEventArgs e)
    {
        //this is where i want to compare the cards.
    }
}

预先感谢您阅读此内容.(:

Thank you in advance for reading this. (:

推荐答案

尝试一下,

 string SubString = MyString.Substring(1);

但是请注意,如果字符串为空,则为错误情况.

But take care if the string is empty, it is an error case.

这篇关于在C#中将字符串转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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