C#while循环不起作用[关闭]

问题描述 投票:-5回答:2

我正在努力建立一个刺激所得税计算器。我想要取得的进展是:

  1. 打印出“你的总收入是多少:”的消息,要求用户以正数字输入他们的收入。
  2. 阅读他们的输入
  3. 制作循环:如果用户输入字符串,则打印“输入您的收入作为整数美元数字”,然后返回步骤1.如果用户输入负数,则打印“您的收入不能为负数”,并且回到第1步。

4.如果用户已成功输入正数字,请执行步骤5。

  1. 打印出一条消息“你有几个孩子?”
  2. 读取用户的输入。
  3. 制作循环:如果用户输入字符串,则打印“您必须输入有效数字”,然后返回步骤5.如果用户输入负数,请打印“您必须输入正数”,然后返回第5步。
  4. 如果用户成功输入正数字,则进入步骤9。
  5. 将收入和子女的数字纳入税收计算公式。如果totalTax <= 0,则打印“您欠税”。如果总税> 0,则打印“您必须支付[税额]税”。结束

我试图使用下面的代码,但它根本不起作用。

//Record user's income into double list "income"
double i;

//Record user's number of children into double list "kid"
double k;


//if...elif statement to calculate the toatal tax of the user
bool incomeOK = false;
bool kidOK = false;

do
{
    Console.Write("What is your total income : ");
    incomeOK = double.TryParse(Console.ReadLine(), out i);

    if (!incomeOK || i < 0)
    {
        if (!incomeOK)
        {
            Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
        }
        else if (i < 0)
        {
            Console.WriteLine("Your income cannot be negative.");
        }
    }
    else
    {
        Console.Write("How many children do you have: ");
        kidOK = double.TryParse(Console.ReadLine(), out k);

        while (!kidOK || k < 0)
        {
            Console.Write("How many children do you have: ");
            kidOK = double.TryParse(Console.ReadLine(), out k);

            if (kidOK && k >= 0)
            {
                //Calculate the total payable tax of the user
                double totalTax = (i - 10000 - (k * 2000)) * 0.02;

                if (totalTax <= 10000)
                {
                    Console.WriteLine("You owe no tax.");
                    Console.WriteLine("\n\n Hit Enter to exit.");
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("You owe a total of $ " + totalTax + " tax.");
                    Console.WriteLine("\n\n Hit Enter to exit.");
                    Console.ReadLine();
                }
            }

            if (!kidOK || k < 0)
            {
                if (!kidOK)
                {
                    Console.WriteLine("You must enter a valid number.");
                }
                else if (k < 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                }
            }
        }
    }
}
while (!incomeOK || !kidOK);

但输出:

你的总收入是多少:sfd 输入您的收入作为一个整数美元的数字。

按任意键继续...

c# while-loop
2个回答
1
投票

您需要将其拆分为多个部分。

第一:获得正数

double i;
double k;

Console.Write("What is your total income : ");

while (!double.TryParse(Console.ReadLine(), out i) || i < 0)
{
    if (i < 0)
    {
        Console.WriteLine("Your income cannot be negative.");
    }
    else 
    {
        Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
    }
}

第二:获得孩子数量

Console.Write("How many children do you have: ");

while (!double.TryParse(Console.ReadLine(), out k) || k < 0)
{
    if (k < 0)
    {
        Console.WriteLine("You must enter a positive number.");
    }
    else 
    {
        Console.WriteLine("You must enter a valid number.");
    }
}

第三:计算

double totalTax = (i - 10000 - (k * 2000)) * 0.02;

if (totalTax <= 10000)
{
    Console.WriteLine("You owe no tax.");
    Console.WriteLine("\n\n Hit Enter to exit.");
    Console.ReadLine();
}
else
{
    Console.WriteLine("You owe a total of $ " + totalTax + " tax.");
    Console.WriteLine("\n\n Hit Enter to exit.");
    Console.ReadLine();
}

每当你输入错误时,while循环会强制你留在里面。当你离开时,你可以确定输入是正确的。

PS:我会把int用于孩子的数量,因为没有任何意义,你会有2.3个孩子。


-1
投票

在您的代码中,您在成功解析时不会循环,但它是负数。

由于你的两个循环基本上是相互独立的,所以我不会将它们包装在一个循环中(除非你打算多次询问,但那只是另一个循环重复外部)。保持简单,更容易看到错误。我改成了这个:

do
{
    Console.Write("What is your total income : ");
    incomeOK = double.TryParse(Console.ReadLine(), out i);

    if (!incomeOK) 
    {
        Console.WriteLine("Enter your income as a whole-dollar numeric figure.");
        continue;
    }

    if (i < 0)
    {
        Console.WriteLine("Your income cannot be negative.");
        incomeOK = false; // You need to manually set this to false to indicate it is not ok for it to loop.
    }
} while (!incomeOk)

do
{
    Console.Write("How many children do you have: ");
    kidOK = double.TryParse(Console.ReadLine(), out k);

    if (!kidOK)
    {
        Console.WriteLine("You must enter a valid number.");
        continue;
    }

    if(k < 0)
    {
        Console.WriteLine("You must enter a positive number.");
        kidOK = false; // Same deal as before
        continue;
    }

    /// ... Your calculations
} while (!kidOK);

代码现在分为两个循环,即收入输入和子输入。而且,真的没有必要先检查它是否是(!incomeOK || i <0)。如果不正常,则输出消息并循环。如果它是否定的,输出您的消息,并确保它循环。否则只是继续正常。

© www.soinside.com 2019 - 2024. All rights reserved.