我不知道为什么我的程序要求多个输入

时间:2019-11-30 18:05:50

标签: c#

所以我正在尝试解决以下问题:

小女孩Tanya正在学习如何将数字减一,但是她用两个或多个数字组成的数字却做错了。 Tanya通过以下算法从数字中减去一个:

如果数字的最后一位数字不为零,则将数字减一; 如果数字的最后一位数字为零,则她将数字除以10(即删除最后一位数字)。 您会得到一个整数?。 Tanya将从中减去1次。您的任务是在所有减去?之后打印结果。

保证结果将为正整数。

输入 输入的第一行包含两个整数?和?(2≤?≤109,1≤?≤50)-Tanya将要减去的数字和相应的减数。

输出 打印一个整数-将decreasing减少1倍的结果。

保证结果将为正整数。

示例

inputCopy

512 4

outputCopy

50

inputCopy

1000000000 9

outputCopy

1

注意 第一个示例对应于以下顺序:512→511→510→51→50。

(问题来自:https://codeforces.com/problemset/problem/977/A

我的解决方案:

/*
    Wrong subtraction solution
    Alienator5000  
*/  
namespace WrongSubtractionAlienator5000
{
    class Program
    {
        static void Main(string[] args)
        {
            Math m = new Math();
            m.doingMath();
            Console.Write(m.forReturning);

            Console.ReadKey();
        }
    }                                                                                                

    class Math
    {
        private string inputValue;
        private int inputValue2;

        public int forReturning;

        public int doingMath()
        {

            inputValue = Console.ReadLine(); //n
            inputValue2 = Convert.ToInt32(Console.ReadLine()); //k

            while (inputValue2 > 0 && inputValue != null)
            {
                string checkValue = inputValue.Remove(0, inputValue.Length - 1);

                if (checkValue != "0")
                {
                    int newInputValue = Convert.ToInt32(inputValue);
                    newInputValue = newInputValue - 1;
                    inputValue2++; //k
                    forReturning = newInputValue;
                }
                else if (checkValue == "0")
                {
                    int newInputValue = Convert.ToInt32(inputValue);
                    newInputValue = newInputValue % 10;
                    inputValue2++; //k
                    forReturning = newInputValue;
                }
                else
                {
                    Console.Write("Incorrect Input");
                }
            }
            return forReturning;
        }
    }
}

我的调试器无法正常工作,因此我无法真正继续执行我的代码以查看它是否正确,但是我只是想知道我该怎么做才能确保仅输入2个输入而不会更多。谢谢!

2 个答案:

答案 0 :(得分:0)

这是基本的“要求输入,直到用户开始理解为止”的代码:

int inputValue;
bool repeat = true;

do{
  Console.WriteLine("Please enter first Number");
  string input = Console.ReadLine();

  bool validInput = int.TryParse(input, out inputValue);

  if(validInput)
    repeat = false;
  else
    Console.WriteLine("Invalid Input. Press any key to try again");
    Console.ReadKey();
}while(repeat);

对于每个值,只需执行一次。循环之后,您知道变量已设置为有效的输入整数。可以将其他检查添加到while循环中(例如它们需要为正数)。

答案 1 :(得分:0)

要确保仅获得两个输入,必须先获得第一个输入,然后对其进行验证,然后如果满足所有要求,则可以得到第二个输入。限制的一种方法是使用循环,将用户保持在该循环中,直到用户输入有效输入为止,然后从循环中断开以继续执行代码重置。

为使事情简单明了,Wrong Subtraction问题需要两个输入,且具有以下要求。

  • [输入1] n 应该是2到1,000,000,000之间的有效整数
  • [输入2] k 应该是1到50之间的有效整数
  • [结果]应为正整数。

因此,如果我们接受第一个输入,则可以在验证输入时使用while循环将用户保持在该循环中,如果我们获得有效的输入,则只要用户输入,我们就可以从此循环中断开无效的输入,我们要求用户重新输入一个有效的整数。

示例:

int n;

// Get the first input, keep the user in this loop until you get a vaild integer
while (true)
{

    if (!int.TryParse(Console.ReadLine(), out n))
    {
        // not a valid integer 
        Console.WriteLine("(n) Incorrect Input, please try again!");
    }
    else if (!(n >= 2 && n <= 1000000000))
    {
        // (n) should be between 2 and 1,000,000,000 
        Console.WriteLine("(n) please choose integer between 2 and 1,000,000,000‬");
    }
    else
    {
        // it's a valid integer and between 2 and 1,000,000,000
        break;
    }
}
在示例中可以看到

,我强迫用户进行此验证,直到满足两个条件,用户才能继续。

int.TryParse(Console.ReadLine(), out n),它将尝试解析用户输入,如果它是有效整数,则将其存储在n变量中,并返回true。如果为无效整数,它将返回false,并且由于它在循环内,因此将要求用户再次重新输入有效整数。我们可以对k输入执行相同的操作。

这是问题解决方案的完整示例:

class Math
{
    private int n;

    private int k;

    public int DoingMath()
    {


        // Get the first input, keep the user in this loop until you get a vaild integer
        while (true)
        {
            if (!int.TryParse(Console.ReadLine(), out n))
            {
                // not a valid integer 
                Console.WriteLine("(n) Incorrect Input, please try again!");
            }
            else if (!(n >= 2 && n <= 1000000000))
            {
                // (n) should be between 2 and 1,000,000,000 
                Console.WriteLine("(n) please choose integer between 2 and 1,000,000,000‬");
            }
            else
            {
                // it's a valid integer and between 2 and 1,000,000,000
                break;
            }


        }

        // Get the second input, keep the user in this loop until you get a vaild integer
        while (true)
        {
            if (!int.TryParse(Console.ReadLine(), out k))
            {
                // not a valid integer 
                Console.WriteLine("(k) Incorrect Input, please try again!");
            }
            else if (!(k >= 1 && k <= 50))
            {
                // (k) should be between 1 and 50           
                Console.WriteLine("(k) please choose integer between 1 and 50.");
            }
            else
            {
                // it's a valid integer and between 1 and 50 
                break;
            }
        }

        // now, do the subtraction logic 
        for (int x = 0; x < k; x++)
        {
            if (n < 10) // if n has one digit, then decrease the number
            {
                n--;
            }
            else // if n has more than one digit 
            {
                var lastDigit = int.Parse(n.ToString().Substring(n.ToString().Length - 1));

                if (lastDigit > 0)
                    n--;
                else
                    n = n / 10;
            }
        }

        return System.Math.Abs(n); // always returns positive number 
    }
}

用法:

class Program
{
    static void Main(string[] args)
    {
        Math m = new Math();


        // Optional loop, if you want to keep the application running until the user close it.
        while(true)
        {
            var result = m.DoingMath();

            Console.WriteLine(result);

            Console.WriteLine();

            Console.WriteLine("Do you want to continue ? [Yes/No]");

            var doContinue = Console.ReadLine();

            if (doContinue.ToLower().Trim().Equals("no"))
                break;
        }

    }
}