需要帮助尝试为相同的输入输入和运行两个循环

本文关键字:输入 运行 循环 两个 帮助 | 更新日期: 2023-09-27 18:14:02

我在Try中放入了一个循环。解析这样,如果用户输入一个十进制数字,程序将要求他们输入另一个数字,直到他们输入一个整数,然后程序将继续执行下一部分。然而,我正在努力的是把另一个循环,使它的用户只能输入1和100之间的数字,如果他们不应该有一个错误信息,循环,直到他们进入这个。我想让它们同时运行,我有这个,但我想让它也检查它是否在范围内,我不确定如何做到这一点。我是编程新手,所以我不是很擅长这个。提前感谢!

        string inputcost;
        string inputmoney;
        int validcost;
        int validmoney;
        int changereq;
        Console.Write("Please Enter The Cost, In Pennies, Of The Item You Have Purchased: ");
        inputcost = Console.ReadLine();
        bool result = int.TryParse(inputcost, out validcost);
        while (!int.TryParse(inputcost, out validcost))
        {
            if (result == true)
            {
                Console.Write("Valid Value");
            }
            if (result == false)
            {
                Console.Write("Please Enter A Valid Integer Value");
                Console.WriteLine();
                inputcost = Console.ReadLine();
            }
        }

需要帮助尝试为相同的输入输入和运行两个循环

这里的问题是,您正在查看的result变量只写入一次:在循环之外。考虑尝试下面的伪代码(do-while循环的工作原理与while循环完全相同,除了它总是在检查条件之前执行一次):

bool validInput;
do
{
    // If you set it true to begin with, you can set it false on any unmet conditions
    // If it doesn't get set false, you've got a valid input and can exit the loop.
    validInput = true;
    Read input from user
    Check if it's a valid integer, if not print message and validInput = false
    Check if it's between 1-100, if not print message and validInput = false;
} while (!validInput);

如果你想处理一些更高级的东西,看看continue关键字。

试试这样做。

string inputcost;
    string inputmoney;
    int validcost;
    int validmoney;
    int changereq;       
    while (true)
    {
        Console.Write("Please Enter The Cost, In Pennies, Of The Item You Have Purchased: ");
        inputcost = Console.ReadLine();
        if (!(valuecost >=1 && valuecost <=100))
        {
            Console.Write("Please enter value between 1 and 100.");
        }
        bool result = int.TryParse(inputcost, out validcost);                      
        if (result == true)
        { 
            Console.Write("Valid Value");
        }
        if (result == false)
        {
            Console.Write("Please Enter A Valid Integer Value");
        }        
    }

使用一个方法并执行如下操作会很有用:

Console.Write("Please Enter The Cost, In Pennies, Of The Item You Have Purchased: ");
inputcost = Console.ReadLine();
bool result = false;
while (!result)
{
  result = checkNumber();
}
public static bool checkNumber()
{
  if(inputcost < 1 || inputcost > 100)
  {
    Console.Write("Please Enter a valid value: ");
    inputcost = Console.ReadLine();
    return false;
  }
  else
    return true;
} 

希望这对你有帮助。

string inputcost;
string inputmoney;
int validcost = 0;
int validmoney = 0;
Console.Write("Please Enter The Cost, In Pennies, Of The Item You Have Purchased: ");
inputcost = Console.ReadLine();
// only accept integers
while (!int.TryParse(inputcost, out validcost))
{
  Console.Write("Please Enter An Integer Value: ");
  inputcost = Console.ReadLine();
}
// valid integer input in variable validcost 
bool done = false;
while (!done)
{
  Console.Write("Enter an integer between 1 and 100: ");
  inputmoney = Console.ReadLine();
  if (int.TryParse(inputmoney, out validmoney))
  {
    if (validmoney > 0 && validmoney < 101)
    {
      done = true;
    }
    else
    {
      Console.WriteLine("Invalid input: " + inputmoney);
    }
  }
  else
  {
    Console.WriteLine("Invalid input: " + inputmoney);
  }
}
  //validcost is an integer and validmoney is an integer between 1 and 100
  Console.WriteLine("validcost: " + validcost + " validmoney: " + validmoney);
  Console.ReadKey();