【问题标题】:How can I display my results in C#?如何在 C# 中显示我的结果?
【发布时间】:2009-09-26 08:05:47
【问题描述】:

我有一个程序见下文

我创建了方法,但我想在控制台中显示它 而不是像console.writeline(str.length)这样的简单方法。我想用我做的方法。

有人可以帮帮我吗

提前致谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "dit is een test 1,2,3";
            Console.WriteLine(str);
        }

        public int CountAllNumbersAndChar(string str) 
        { 
            return str.Length; 
        }

    }
}

更新:

我现在有以下程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "this is a test 1,2,3";
            int length = CountAllNumbersAndChar(str);
            Console.WriteLine(str);
            Console.WriteLine(length);// met de methode maar kan handiger met onderstaand voor beeld
      // Console.WriteLine(str.Length);
       // int numbers = str.Count(Char.IsNumber); // de makelijkste makelijke manier 
        //Console.WriteLine(numbers);
        int countnumber = CountNumbers(str) ;
        Console.WriteLine(countnumber);
        int countwords = words(str);
        Console.WriteLine(countwords);


    }

    public static int CountAllNumbersAndChar(string str)
    {
        return str.Length;
    }
    public static int CountNumbers(string str)
    {
        return str.Count(Char.IsNumber);
    }
    public static int words(string str)
    {
        int words = str.Split().Count(str => str.All(Char.IsLetter));
    }
}

}

但是还是不行 有人能告诉我我必须改变什么吗?

【问题讨论】:

    标签: c# console.writeline


    【解决方案1】:

    这是你想要的吗?

    Console.WriteLine(CountAllNumbersAndChar(str));
    

    【讨论】:

      【解决方案2】:

      这就是你的做法。请注意以下代码中的public static int CountAllNumbersAndChar(string str)。如果不声明为static,则不能从Main 调用CountAllNumbersAndChar

      using System; 
      using System.Collections.Generic; 
      using System.Linq;
      using System.Text;
      
      namespace ConsoleApplication3
      {
          class Program
          {
              static void Main(string[] args)
              {
                  string str = "this is a test 1,2,3";
                  int length = CountAllNumbersAndChar(str);    
      
                  Console.WriteLine(length);
              }
      
              public static int CountAllNumbersAndChar(string str) 
              { 
                  return str.Length; 
              }
          }
      }
      

      【讨论】:

        【解决方案3】:

        您可以将 LINQ 用于所有这些任务。虽然我不确定你是否熟悉它。不过这真的很简单,所以看看代码,看看你是否可以遵循。

        string str = "dit is een test 1,2,3";
        
        // Length of the string
        int chars = str.Length;
        
        // LINQ: Count all characters that is a number
        int numbers = str.Count(Char.IsNumber);
        
        // LINQ: Split the string on whitespace and count the 
        // elements that contains only letters
        int words = str.Split().Count(s => s.All(Char.IsLetter));
        
        Console.WriteLine(chars); // -> 21
        Console.WriteLine(numbers); // -> 3
        Console.WriteLine(words); // -> 4
        

        当然,我计算单词的方式并不完美,但它应该能让你开始。有关更准确的方法,您应该谷歌它,因为那里有数百个示例。

        【讨论】:

        • 你是否包含了用于 System.Linq 的内容?
        • 请务必参考 System.Core。代码确实有效,我不知道你为什么这么说。
        • 我找到了问题,但它与任何程序集无关,自动完成不显示字符串的扩展方法计数,但是当我写它时,它显示工具提示!!!奇怪
        • 但您的代码再次计算错误,因为大于 9 的数字会导致问题
        【解决方案4】:

        我认为您想计算字符串中的数字数量

              public int CountAllNumbersAndChar(string str)         
               {
                   return  str.Split(new char[]{' ',','},
                 StringSplitOptions.RemoveEmptyEntries).Count    
                 (
                   x=>
                   {
                       int d;
                       return int.TryParse(x,out d);
                   }
                );   
               }
        

        【讨论】:

        • 这比str.Count(Char.IsNumber) 好在哪里?另外,请格式化您的代码以使其更具可读性。
        • str.Count 将遍历每个字符,例如 19 将被计为 2 !!!
        • 确实如此。在这种情况下,您确实必须使用类似于您的示例的内容。
        猜你喜欢
        • 2017-12-25
        • 1970-01-01
        • 2021-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-03
        相关资源
        最近更新 更多