如何正确地构造/重写此代码? [英] How to struct/rewrite this code properly ?

查看:87
本文介绍了如何正确地构造/重写此代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class Program
    {
        static void Main(string[] args)
        {
            char c = 'x';
            Console.Write("Input string: ");
            string str = Console.ReadLine();
            for (int i = 0; i < str.Length; i++)
            {
                c = str[i];
                if (System.Char.IsSymbol(c) || System.Char.IsSeparator(c) || System.Char.IsWhiteSpace(c))
                {
                    Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
                }
            }
            int a;
            bool check = int.TryParse(str, out a);
            if (check == true)
            {
                Console.WriteLine("The input string is an integer.");
            }
            if (check == false)
            {
                Console.WriteLine("The input string is an identifier.");
            }
        }
    }
}





我尝试了什么:



问题是:创建一个方法,检查输入字符串是否为整数或标识符。我的代码似乎不对,因为当我输入 abc abc $ abc 作为字符串时输出为:



整数和标识符不能是符号,空格或包含它们

输入的字符串是标识符。

按任意键继续。 。 。



What I have tried:

The problem is : " Create a method, which checks if input string is an Integer or Identifier." My code seems to be not right because when I input "abc abc" or "$abc" as a string the output is :

Integers and identifiers can not be symbols, space or contain them
The inputed string is an identifier.
Press any key to continue . . .

推荐答案

abc 作为字符串,输出为:



整数和标识符不能是符号,空格或包含它们

输入字符串是标识符。

按任意键继续...


问题是,如果您在字符串中发现问题,除了告诉用户之外,您不会对其进行任何处理。

返回添加到 if 代码:

The problem is that if you find a problem in your string, you don't do anythign about it, other than tell the user.
Add a return to the if code:
c = str[i];
 if (System.Char.IsSymbol(c) || System.Char.IsSeparator(c) || System.Char.IsWhiteSpace(c))
 {
     Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
     return;
 }

它应该工作。

但是我要么使用 foreach 循环而不是for:

And it should work.
But I'd either use a foreach loop instead of the for:

foreach (char c in str)
   {
   ...
   }

或者使用内置方法Any(但可能有点在这个阶段为你提前):

Or use the the built in method Any (but that may be a bit advanced for you at this stage):

if (str.Any(c => char.IsSymbol(c) || char.IsSeparator(c) || char.IsWhiteSpace(c)))
    {
    Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
    return;
    }


尝试这样的事情(未经测试)

Try something like this (untested)
ass Program
{
    static void Main(string[] args)
    {
        char c = 'x';
        Console.Write("Input string: ");
        string str = Console.ReadLine();
                
		if ValidInput(str)
		{
			int a;
			bool check = int.TryParse(str, out a);
			if (check)
			{
				Console.WriteLine("The input string is an integer.");
			}
			else
			{
				Console.WriteLine("The input string is an identifier.");
			}
		}
		else
		{
			Console.WriteLine("Integers and identifiers can not be symbols, space or contain them");
		}
    }
	bool ValidInput(string x)
	{
		ValidInput = true;
		for (int i = 0; i < str.Length; i++)
        {
            c = str[i];
            if (System.Char.IsSymbol(c) || System.Char.IsSeparator(c) || System.Char.IsWhiteSpace(c))
            {
                ValidInput = false;
				exit for;
            }
        }
	}
}



注意你不需要 = true for bools。如果一个bool不是真的那么必须是假的 - 所以如果


Note you do not need = true for bools. And if a bool is not true then it must be false - so no need for the second if

这篇关于如何正确地构造/重写此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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