C#用于字符串中的大小写(简单)

C#用于字符串中的大小写(简单),c#,C#,所以我有这个代码。我需要生成一个for循环来检查字符串中的所有字符,并检查它们是否都有效(所以数字从0->7)。但我不知道怎么写,我尝试了一些东西,但没有成功。以下是示例:用户输入:77,代码有效,用户输入99,代码不起作用,用户输入5,代码不起作用,等等 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;

所以我有这个代码。我需要生成一个for循环来检查字符串中的所有字符,并检查它们是否都有效(所以数字从0->7)。但我不知道怎么写,我尝试了一些东西,但没有成功。以下是示例:用户输入:77,代码有效,用户输入99,代码不起作用,用户输入5,代码不起作用,等等

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

namespace NALOGA1
{
    class Program
    {
        static string decToOct(int stevilo)//v mojon primere 7
        {
            string izhod = "";
            //7>0 DRŽI
            while (stevilo > 0)
            {
                //izhodi se dodeli ostanek deljenja z 8 keri se spremeni v string
                izhod = (stevilo % 8) + izhod;
                //7/8;
                stevilo /= 8;
            }
            return izhod;
        }

        static int Octtodesetisko(string stevilo)
        {

            double vsota = 0; 
            for (int i = stevilo.Length - 1; i >= 0; i--)
            {

                int stevka = stevilo[i] - '0';
                vsota += (stevka * Math.Pow(8, i));
            }

            return (int)vsota;
        }


        static void Main(string[] args)
        {



            //3 podprogram-in progress


            string prvastevilka = Console.ReadLine();



            int prvasprememba = Int32.Parse(prvastevilka);
            if (prvasprememba > 0)
            {
                Console.WriteLine(decToOct(prvasprememba));

            }
            else
            {
                Console.WriteLine("Napaka");
            }


                string drugastevilka = Console.ReadLine();
                int drugasprememba = Octtodesetisko(drugastevilka);
                foreach (char znak in drugastevilka)
                {
                    if(znak!=1 || znak!=2 || znak!=3 || znak!=4 || znak!=5 || znak!=6 || znak!=7)
                    {
                        Console.WriteLine("Napaka");
                    }
                    else
                    {
                        Console.WriteLine("dela :D");
                    }
                }
                Console.ReadKey();
        }
    }

}

就我个人而言,我会利用这种方法以一种非常简洁易读的方式来表达这一点:

if (str.Any() && str.All(c => c >= '0' && c <= '7'))
{
    Console.WriteLine("good");
}
else
{
    Console.WriteLine("bad");
}

首先,线路上似乎有一个错误

if(znak!=1 || znak!=2 || znak!=3 || znak!=4 || znak!=5 || znak!=6 || znak!=7)
我想应该是这样的

if(znak!='1' || znak!='2' || znak!='3' || znak!='4' || znak!='5' || znak!='6' || znak!='7')
应该压缩到

if (znak >= '0' && znak <= '7')
但最好的解决方案可能是使用正则表达式:

Regex regex = new Regex("^[0-7]+$");
if (regex.IsMatch(drugastevilka))
    Console.WriteLine("dela :D");
else
    Console.WriteLine("Napaka");

编辑:显示的linq解决方案接受空字符串,正则表达式(如图所示)至少需要1个字符。将
+
*
交换,它也将接受空字符串。但是我认为您不想接受空字符串。

您弄乱了数据类型 你能试试下面的代码吗

static string decToOct(int stevilo)//v mojon primere 7
        {
            int izhod = 0;
            //7>0 DRŽI
            while (stevilo > 0)
            {
                //izhodi se dodeli ostanek deljenja z 8 keri se spremeni v string
                izhod = (stevilo % 8) + izhod;
                //7/8;
                stevilo /= 8;
            }
            return (izhod.ToString());
        }

像这样的怎么样

class Program
{
    static void Main(string[] args)
    {
        string someString = "1234567";
        string someOtherString = "1287631";
        string anotherString = "123A6F2";

        Console.WriteLine(IsValidString(someString));
        Console.WriteLine(IsValidString(someOtherString));
        Console.WriteLine(IsValidString(anotherString));

        Console.ReadLine();
    }

    public static bool IsValidString(string str)
    {
        bool isValid = true;

        char[] splitString = str.ToCharArray(); //get an array of each character

        for (int i = 0; i < splitString.Length; i++)
        {
            try
            {
                double number = Char.GetNumericValue(splitString[i]); //try to convert the character to a double (GetNumericValue returns a double)

                if (number < 0 || number > 7) //we get here if the character is an int, then we check for 0-7
                {
                    isValid = false; //if the character is invalid, we're done.
                    break;
                }
            }
            catch (Exception) //this will hit if we try to convert a non-integer character.
            {

                isValid = false;
                break;
            }                
        }

        return isValid;
    }
}
类程序
{
静态void Main(字符串[]参数)
{
string someString=“1234567”;
string someOtherString=“1287631”;
字符串另一个字符串=“123A6F2”;
Console.WriteLine(IsValidString(someString));
Console.WriteLine(IsValidString(someOtherString));
Console.WriteLine(IsValidString(另一个字符串));
Console.ReadLine();
}
公共静态bool IsValidString(字符串str)
{
bool isValid=true;
char[]splitString=str.ToCharArray();//获取每个字符的数组
for(int i=0;i7)//如果字符是int,那么我们在这里检查0-7
{
isValid=false;//如果字符无效,则结束。
打破
}
}
catch(Exception)//如果尝试转换非整数字符,则会遇到此问题。
{
isValid=false;
打破
}                
}
返回有效;
}
}
IsValidString()
获取
字符串
,将其转换为
字符数组
,然后检查每个值:

获取数值
检查该值是否在0-7之间

GetNumericValue
将在非整数字符上失败,因此我们将其包装为一个
try/catch
——如果遇到异常,我们知道
isValid=false
,因此我们将
中断
。 如果我们得到一个有效的数字,并且它不在
0-7
之间,我们也知道
isValid=false
,所以我们
break

如果我们一直在列表中,则字符串是有效的

上述样本返回:

IsValidString(someString)=true

IsValidString(someOtherString)=false


IsValidString(另一个字符串)=false

Define可以工作,也不能清楚地工作。虽然从技术上讲,只需查看逻辑就可以遵循程序流程,但如果您将所有内容翻译成英语,这将非常有帮助。为试图提供帮助的人减少一吨的开销<代码>如果(znak>='0'&&znak实际上,如果(znak!=1 | znak!=2 | znak!=3 | znak!=4 | znak!=5 | znak!=6 | znak!=7),那么这个逻辑似乎是错误的是不是?考虑<代码> ZNAK!=1ΩZNAK!= 2代码< >代码> 1,然后<代码> ZNAK!= 2 < /代码>是代码>真的< /代码>。如果<代码> ZNAK < /代码>是1以外的任何数字,那么<代码> ZNAK!=1 < /代码>是“代码>真的< /代码>。因此,<代码> 1→ZnAK!=2 < /COD>始终是<代码>真< /代码>,用于<代码>的所有值。因为它永远都是真的>znak@averagejoex我已经猜到了。我更喜欢正则表达式,但如果您的任务包括使用for循环,则至少使用
if(znak>='0'&&znak Ok,没有liq。但是LINQ呢?:)哇,上面的方法是有效的,但是空字符串是什么呢?这取决于。如果你有一个空字符串,你想做什么?你认为它是有效的还是无效的?用这个来把八进制数字转换成十进制。但是输入只能是八进制数字,所以从0到7HELL,然后你只需添加一个特定情况的检查。我编辑了我的答案。
static string decToOct(int stevilo)//v mojon primere 7
        {
            int izhod = 0;
            //7>0 DRŽI
            while (stevilo > 0)
            {
                //izhodi se dodeli ostanek deljenja z 8 keri se spremeni v string
                izhod = (stevilo % 8) + izhod;
                //7/8;
                stevilo /= 8;
            }
            return (izhod.ToString());
        }
class Program
{
    static void Main(string[] args)
    {
        string someString = "1234567";
        string someOtherString = "1287631";
        string anotherString = "123A6F2";

        Console.WriteLine(IsValidString(someString));
        Console.WriteLine(IsValidString(someOtherString));
        Console.WriteLine(IsValidString(anotherString));

        Console.ReadLine();
    }

    public static bool IsValidString(string str)
    {
        bool isValid = true;

        char[] splitString = str.ToCharArray(); //get an array of each character

        for (int i = 0; i < splitString.Length; i++)
        {
            try
            {
                double number = Char.GetNumericValue(splitString[i]); //try to convert the character to a double (GetNumericValue returns a double)

                if (number < 0 || number > 7) //we get here if the character is an int, then we check for 0-7
                {
                    isValid = false; //if the character is invalid, we're done.
                    break;
                }
            }
            catch (Exception) //this will hit if we try to convert a non-integer character.
            {

                isValid = false;
                break;
            }                
        }

        return isValid;
    }
}