读取和处理输入

本文关键字:输入 处理 读取 | 更新日期: 2023-09-27 17:52:36

编写一个程序,计算并显示一个委托销售员工扣除后的实得工资。员工获得总销售额的7%作为他或她的总工资。他或她的联邦税率是18%。他或她向自己的退休计划缴纳10%,向社会保障缴纳6%。使用下面步骤2中提供的处理逻辑作为指导。程序的输出应该像这样:

<>之前每周销量:28,000总销售额:28,000.00美元工资总额(7%):1,960美元联邦税:352.80美元社会保障金:117.60美元退休供款:$196扣除总额:666.40美元实得工资:1293.60美元按任意键继续。之前

//这是我的代码,我有麻烦输入

        //Declarations
        int weeklySales;
        double grossPay = weeklySales * .07;
        double fedTax = grossPay * .18;
        double retirement = grossPay * .1;
        double socSecurity = grossPay * .06;
        double totDeductions = socSecurity + retirement + fedTax;
        double takeHomePay = grossPay - totDeductions;
        //Promt user for Input
        System.Console.WriteLine("What is your weekly Sales?");
        weeklySales = Console.Read();
        System.Console.ReadLine();


         //Output Display
        System.Console.Write("'nYour Weekly Sale amount is :'t't" + weeklySales+ "'n'nGross Pay:'t't't't" + grossPay+ "'n'nFed Tax 't't't't" + fedTax + "'n'nRetirement't't't't"+ retirement + "'n'nSocial Security:'t't't" + socSecurity + "'n'nTotal Deductions:'t't't" + totDeductions + "'n'nMaking your take home pay:'t't" + takeHomePay);
        System.Console.ReadLine();

读取和处理输入

  weeklySales = Console.Read();

不会像你想象的那样。它将返回第一个字符的ASCII值。

:

  string weeklySalesText = Console.ReadLine();
  weeklySales = int.Parse(weeklySalesText );  // may need more processing.

正确的方法:

Int32 weeklySales = 0;
while (weeklySales.Equals(0))
{
    Console.WriteLine("What are your weekly sales?");
    String input = Console.ReadLine();
    try
    {
        weeklySales = Int32.Parse(input);
    }
    catch
    {
        Console.WriteLine("There is an error in your input, try again!");
    }
}
Double grossPay = weeklySales * .07;
Double fedTax = grossPay * .18;
Double retirement = grossPay * .1;
Double socSecurity = grossPay * .06;
Double totDeductions = socSecurity + retirement + fedTax;
Double takeHomePay = grossPay - totDeductions;
Console.Write("'nYour Weekly Sale amount is :" + weeklySales + "$'nGross Pay: " + grossPay + "$'nFed Tax: " + fedTax + "$'nRetirement: " + retirement + "$'nSocial Security: " + socSecurity + "$'nTotal Deductions: " + totDeductions + "$'nMaking your take home pay: " + takeHomePay + "$");
Console.Read();

简单方法:

Console.WriteLine("What are your weekly sales?");
try
{
    Int32 weeklySales = Int32.Parse(Console.ReadLine());
    Double grossPay = weeklySales * .07;
    Double fedTax = grossPay * .18;
    Double retirement = grossPay * .1;
    Double socSecurity = grossPay * .06;
    Double totDeductions = socSecurity + retirement + fedTax;
    Double takeHomePay = grossPay - totDeductions;
    Console.Write("'nYour Weekly Sale amount is :" + weeklySales + "$'nGross Pay: " + grossPay + "$'nFed Tax: " + fedTax + "$'nRetirement: " + retirement + "$'nSocial Security: " + socSecurity + "$'nTotal Deductions: " + totDeductions + "$'nMaking your take home pay: " + takeHomePay + "$");
    Console.Read();
}
catch
{
    Console.WriteLine("There is an error in input. Program will be terminated.");
}

您试图在变量中放入任何东西之前使用变量计算结果,这不起作用。

Console.Read()并不像你想象的那样。它获取第一个新字符,并将其作为int返回。它不会读整行。此外,之后的Console.ReadLine()调用是完全没有意义的,因为您没有对结果做任何事情。

我认为你想让用户写一行,然后捕获该行的内容,检查它是否和int,如果它不是写一个错误消息,再试一次,如果是,用它做东西。

这样做:

while (!int.TryParse(Console.ReadLine(), out weeklySales))
{
    Console.WriteLine("Input was not a number.");
}
代替

weeklySales = Console.Read();
System.Console.ReadLine();

在此之后,您可以使用weeklySales来计算所有其他所需的变量。

同样,开始时的定义也不应该工作,因为weeklySales在开始时没有初始化。


EDIT:作为我的方法如何工作的一个小解释:你正在调用int.TryParse(string, out int),如果给定的字符串不是数字,则返回false,否则将其解析为int并返回true

要解析为整数的是控制台输入,因此,第一个参数应该是Console.ReadLine()weeklySales显然是您想要将string转换为的数字,因此作为out参数提供(如果您不知道那是什么,请查找)。

如果int.TryParse返回false(因此已经失败),我们运行while循环通知用户,并再次请求。