简体   繁体   English

C#-用除法和余数分隔整数/字符串

[英]C# - Separate integers/strings with division and remainder

I have a homework that I just can't figure out how to do. 我有一个作业,我根本不知道该怎么做。 I'm not sure if the teacher wrote it wrong(he is a bad typer) or if there is a way to accomplish the task. 我不确定老师是否写错了(他是一个不好的打字员)还是有办法完成任务。

I work in Visual C# 2010 Express - Console Application 我在Visual C#2010 Express-控制台应用程序中工作

My task is to: 我的任务是:

Read a four digit integer, such as 5893, from the keyboard and display the digits separated from one another by a tab each. 从键盘上读取一个四位数的整数,例如5893,并显示每个数字,每个数字之间用一个制表符分隔。 Use both integer division and modulus operator % to pick off each digit. 使用整数除法和模数运算符%可以选择每个数字。 If the user enters 4567, the output looks like: 如果用户输入4567,则输出如下所示:

4567 4567

4 5 6 7 4 5 6 7

Sure I know how to separate the numbers by using \\t as well as reading the input and displaying it to the user. 当然,我知道如何使用\\t以及读取输入并将其显示给用户来分隔数字。 But how am I supposed to 'pick off' each digit with division and the remainder operators? 但是我应该如何用除法运算符和其余运算符“提取”每个数字? Maybe he means something else, but not sure. 也许他还有其他意思,但不确定。

And another question... 还有一个问题

How do I make sure that what the user types in is a number and not a letter? 如何确保用户输入的是数字而不是字母? Do I have to use Char.IsLetter , because I couldn't figure out how to use it on a parsed string. 我是否必须使用Char.IsLetter ,因为我不知道如何在已解析的字符串上使用它。

Example: 例:

        string number1;
        int x;
        Console.Write("Please enter a number to calculate: ");
        number1 = Console.ReadLine();
        x = Int32.Parse(number1);

What method am I supposed to use and where do i put it in? 我应该使用什么方法?应该在哪里使用? Because now i only get an error and the application shuts down if I try to enter e letter. 因为现在我只收到一个错误,并且如果我尝试输入e字母,应用程序将关闭。

The first question is really more about maths than programming. 第一个问题实际上是关于数学而不是编程。 You know what the division and modulus operators do. 您知道除法和模运算符的作用。 Think about how you could use them to get the last (least significant) digit. 考虑如何使用它们来获取最后一位(最低有效位)。 Once you've done that you can apply a similar technique for the 2nd digit (tens) and then the same for hundreds and thousands and so on. 完成后,您可以对第二个数字(十位数)应用类似的技术,然后对数十万个数字应用相同的技术,依此类推。

You've already found Char.IsLetter , but there is also Char.IsDigit , which does what you want more directly. 您已经找到了Char.IsLetter ,但是还有Char.IsDigit ,它可以更直接地满足您的需求。 You can do this check for one character and what you have is a string of characters. 您可以检查一个字符,然后检查一串字符。 Have a look at a foreach loop. 看一下foreach循环。

转换String时,System.Int32.TryParse()可能是更好的选择。

Yes, your assignment makes sense. 是的,您的分配很有意义。 Say you have an integer x = 4567. To pick out the digit at position A you would use: 假设您有一个整数x =4567。要提取位置A处的数字,请使用:

result = (int)((x / (A * 10)) % 10);

The first part of this (x / (A * 10)) "shifts" x to the right. 此(x /(A * 10))的第一部分将x右移。 So a value of A = 1 would divide 4567 by 10 which results in 456.7. 因此,A = 1的值会将4567除以10,得出456.7。 You then use the modulo operator "%" to pick out the unit part of that number. 然后,您可以使用模运算符“%”来选择该数字的单位部分。 Finally you convert to an int to remove the fractional part. 最后,您将其转换为int以删除小数部分。

Ok, I won't give the whole solution (after all, this is homework ;)). 好的,我不会给出完整的解决方案(毕竟,这是作业;)。

This code would check if there's four characters in input: 此代码将检查输入中是否包含四个字符:

        string input;
        do
        {
            input = Console.ReadLine();
            if (input.Length != 4)
                Console.WriteLine("You have to enter FOUR digits");
        } while (input.Length != 4);

This could be one way of checking the digits: 这可能是检查数字的一种方法:

        bool isOk = true;
        foreach (char c in input.ToArray())
        {
            if (!Char.IsDigit(c))
            {
                Console.WriteLine("You have to enter four DIGITS");
                isOk = false;
                break;
            }
        }

This approach doesn't deal with math but you could do that just as well: 这种方法不涉及数学,但您也可以这样做:

int num = int.Parse(input);
int tensOfThousands = num / 10000;
int thousands = (num - tensOfThousands) / 1000;
int lastDigit = num % 10;
//the rest of the digits are up to you ;)

For everybody new to C#, the solution can be simpler. 对于C#新手来说,解决方案可以更简单。

        // Variables
        int number = 1234;
        int remainder;
        string s = "";

        while (number > 0) {
            remainder = number % 10;
            s = remainder + "\n" + s;
            number /= 10;
        }
        Console.Write (s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM