【问题标题】:How to get sum of all the digits in a string - Visual Basic 2010 (Module)如何获取字符串中所有数字的总和 - Visual Basic 2010(模块)
【发布时间】:2013-07-30 15:16:27
【问题描述】:

如何在 Visual Basic 中获得字符串中所有数字的总和?

---例如---

Dim userWord As String
userWord = Console.ReadLine()

用户输入:“Fox jumped over the 12 moon”

输出显示:3

【问题讨论】:

  • (抱歉,习惯了网站中的整个格式设置)'Dim userWord As String Console.WriteLine("请输入一个句子,以便程序可以使用它。") Console.WriteLine( "按 ENTER 结束程序") userWord = Console.ReadLine() Console.WriteLine("字符串中所有数字的总和:") //这是我卡住的部分 Dim num As Char() = {" 0”、“1”、“2”、“3”、“4”、“5”、“6”、“7”、“8”、“9”} Dim i As Integer Dim total As Integer If (Integer .TryParse(num, i)) Then Console.WriteLine(total = i + i) End If'

标签: vb.net string


【解决方案1】:

杰伦说的:

    Dim mytext As String = "123a123"
    Dim sum As Integer = 0
    For Each ch As Char In mytext
        Dim i As Integer
        If Integer.TryParse(ch, i) Then
            sum += i
        End If
    Next

【讨论】:

  • +1 - 我测试了这个工作.. 我意识到 Convert.ToInt32 不是要走的路。
  • 确实如此,但我宁愿不提供工作代码,因为这可能是学校作业:p
【解决方案2】:
  1. 使用 foreach 遍历字符串中的字符
  2. 尝试将它们解析为 int
  3. 保留一个有总数的变量,如果是整数则相加

【讨论】:

    【解决方案3】:
        Dim mytext As String = "Fox jumped over the 12 moon"
        Dim i As Integer = 0
        For Each ch As Char In mytext
             Dim temp As Integer = 0
             If Integer.TryParse(ch, temp) Then
                  i += temp;
             End If
        Next
    

    【讨论】:

    • 这是我目前在代码中的内容:' Dim userWord As String Dim myChars() As Char = userWord.ToCharArray() Dim i As Integer = 0 For Each ch As Char In myChars If Char.IsDigit(ch) Then i += Convert.ToInt32(ch) End If Next Console.WriteLine("字符串中所有数字的总和:") Console.WriteLine(i) ' 用户输入:Fox12 输出:99 我想要输出为 3 (1 + 2 = 3)。你能详细说一下1 + 2 = 99吗?我错过了什么吗?
    • 设置断点并调试。 2 次查看 Char.IsDigit(ch) 是否为真
    • 这不起作用。它是如何获得两票的?问题是不理解 Convert.ToInt32
    • @dbasnett : 请立即尝试
    • @dumass 我看到你修复了它,它看起来很像我发布的代码。 Dim myChars() As Char = mytext.ToCharArray() 不需要。
    【解决方案4】:

    你可以使用 Linq

    Dim input As String = "Fox jumped over the 12 moon"
    Dim sum As Integer = input.Where(Function(c) [Char].IsDigit(c))
                              .Sum(Function(x) Integer.Parse(x.ToString()))
    

    这会提取所有数字字符 ([Char].IsDigit]),然后将这些字符转换为整数 (Integer.Parse),同时对它们求和。

    【讨论】:

      【解决方案5】:

      到目前为止,这是我的代码中的内容:

          Dim userWord As String
      
      
          Dim myChars() As Char = userWord.ToCharArray()
                      Dim i As Integer = 0
                      For Each ch As Char In myChars
                          If Char.IsDigit(ch) Then
                              i += Convert.ToInt32(ch)
                          End If
                      Next
      
          Console.WriteLine("Sum of all digits in the String: ")
                      Console.WriteLine(i)
      

      用户输入:Fox12 输出:99

      我希望输出为 3 (1 + 2 = 3)。你能详细说一下 1 + 2 = 99 是怎么回事吗?

      我错过了什么吗?

      【讨论】:

      • 我知道我不能在 cmets 中回复“谢谢”。但是我应该如何确认我的问题已得到回答?对不起,我有点新。但我最终会了解这里的一切如何运作。总之谢谢各位!我想我了解如何从字符串中提取数字。 :)
      • @Matt - 欢迎来到 SO :) 要接受答案,您可以单击其左侧的复选标记(就在向上和向下箭头下方)。这将表明您的问题已得到解答,并为回答的人打分。
      猜你喜欢
      • 1970-01-01
      • 2015-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      相关资源
      最近更新 更多