代码之家  ›  专栏  ›  技术社区  ›  clownzxy

字符串似乎没有返回

  •  0
  • clownzxy  · 技术社区  · 3 年前

    中的字符串 NewString 似乎没有回到 UserType() 作用在那里声明那个变量是错误的吗?我想把值返回给 UserType 所以我可以在另一个函数中使用它,比如 Vowel 作用

    
    using System;
    
    namespace prob1{
        class Pogram{
            static void Main(string[] args){
                UserType();
            }
    
            static void Menu(){
                Console.WriteLine("\nChoice of Operation:");
                Console.WriteLine("1. Enter new/another string");
                Console.WriteLine("2.  Count vowels in string and display result");
                Console.WriteLine("3. Count consonants in string and display result");
                Console.WriteLine("4. Convert string to uppercase letters and display");
                Console.WriteLine("5. Convert string to lowercase letters and display");
                Console.WriteLine("6. Count number of words in the string");
                Console.WriteLine("7. Exit Program");
            }
    
            static void UserType(){
                string mainString = System.String.Empty;
                Menu();
                int menuChoice;
                menuChoice = Int32.Parse(Console.ReadLine());
                
                     switch (menuChoice)
                {
                    case 1:
                        NewString(mainString);
                        
                        UserType();
                        break;
                    case 2:
                        Vowel(mainString);
                        UserType();
                        break;
                    default:
                        break;
                }
                
            }
    
            static string NewString(string mainString){
                Console.WriteLine("Enter a new string: ");
                mainString = Console.ReadLine().ToLower();
          
                return mainString;
            }
    
            static void Vowel(string mainString){
                int total = 0;
                var vowels = new HashSet<char> { 'a', 'e', 'i', 'o', 'u' };
                
        
                for (int finder = 0; finder < mainString.Length; finder++)
                {
                    if (vowels.Contains(mainString[finder]))
                    {
                    total++;
                    }
                }
    
                Console.WriteLine("total: " + total);
                
                Console.ReadKey(true);
    
            }
        }
    }
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Yong Shun    3 年前

    你打电话 NewString(mainString) 方法,但未将方法返回的值赋回 mainString 。所以在 UserType 方法 支柱环 不会根据的结果更新值 新闻字符串(主字符串)

    case 1:
        NewString(mainString);
        Console.WriteLine("test menu string: "+ mainString);
        UserType();
        break;
    

    您需要的是分配结果 新闻字符串(主字符串) 返回到 支柱环

    mainString = NewString(mainString);
    
        2
  •  0
  •   DP3PO    3 年前

    您需要添加默认值为string的string参数。Empty()或“”,类似于这样的用户类型-

    static void UserType(string inputString = ""){...}
    

    检查inputString是否为NullOrEmpty,如果为NullOrEmpty,则显示主菜单。如果它不是空的,则将其传递到元音()中,并将其返回值保存到一个变量中以供再次使用-

    var userString = Vowel(inputString);
    

    然后在switch语句中,当值返回到userString时,可以像这样将其传递到UserType-

    UserType(userString)