相关疑难解决方法(0)

如何将String转换为Int?

我有一个TextBoxD1.Text,我想将其转换为将其int存储在数据库中.

我怎样才能做到这一点?

.net c# string int type-conversion

603
推荐指数
19
解决办法
142万
查看次数

在C#中将String转换为int

我正在尝试编写一个简单的程序,要求用户输入一个数字,然后我将使用该数字来确定票证的成本对于他们给定的年龄.我在尝试将字符串转换为int时遇到了麻烦.否则程序布局很好.有什么建议?谢谢

using System;

class ticketPrice
{    
    public static void Main(String[] args)
    {
        Console.WriteLine("Please Enter Your Age");
        int input = Console.ReadLine();
        if (input < 5)
        {
            Console.WriteLine("You are "+input+" and the admisson is FREE!");
        }
        else if (input > 4 & input < 18)
        {
            Console.WriteLine("You are "+input+" and the admission is $5");
        }
        else if (input > 17 & input < 56)
        {
            Console.WriteLine("You are "+input+" and the admission is $10");
        }
        else if (input > 55)
        {
            Console.WriteLine("You are …
Run Code Online (Sandbox Code Playgroud)

c# string int

4
推荐指数
1
解决办法
7017
查看次数

将字符串转换为整数

我的代码需要帮助.我想在我的文本框中只编写数字/整数,并希望在我的列表框中显示它.

我的代码是否按顺序排列?这似乎给出了一个错误.

    int yourInteger;
    string newItem;

    newItem = textBox1.Text.Trim();

    if (newItem == Convert.ToInt32(textBox1.Text))
    {
        listBox1.Items.Add(newItem);
    }
Run Code Online (Sandbox Code Playgroud)

====更新:

这就是我的代码现在的样子.我的问题是,listBox可以处理数据类型"long"吗?因为当我输入数字20,000,000时,我只得到一小时玻璃20分钟.但是当我用控制台尝试这个时,我得到了答案.所以我不确定哪种元素可以处理数据类型"long".

    string newItem;
    newItem = textBox1.Text.Trim();

    Int64 num = 0;
    if(Int64.TryParse(textBox1.Text, out num))
    {
        for (long i = 2; i <= num; i++)
        {
            //Controls if i is prime or not
            if ((i % 2 != 0) || (i == 2))
            {
                listBox1.Items.Add(i.ToString());
            }

        }
    }


    private void btnClear_Click(object sender, EventArgs e)
    {
        listBox1.Items.Clear();
    }
Run Code Online (Sandbox Code Playgroud)

.net c# string integer

3
推荐指数
1
解决办法
1万
查看次数

在C#中解析字符串时出现问题

我试图解析字符串的前三个字符.

public List<string> sortModes(List<string> allModesNonSorted)
{
     foreach (string s in allModesNonSorted)
     {
         char firstNumber  = s[0];
         char secondNumber = s[1];
         char thirdNumber  = s[2];

         char.IsDigit(firstNumber);
         char.IsDigit(secondNumber);
         char.IsDigit(thirdNumber);

         combinedNumbers = Convert.ToInt16(firstNumber) + Convert.ToInt16(secondNumber) + Convert.ToInt16(thirdNumber);
     }
     return allModesNonSorted;
}
Run Code Online (Sandbox Code Playgroud)

它正确识别每个字符,但增加了额外的值53或55.下面我添加数字时,包括53和55.为什么这样做?

c# char

0
推荐指数
1
解决办法
131
查看次数

标签 统计

c# ×4

string ×3

.net ×2

int ×2

char ×1

integer ×1

type-conversion ×1