操作用户输入(数学)

本文关键字:数学 输入 用户 操作 | 更新日期: 2023-09-27 18:35:14

我正在使用C#。我试图允许用户输入一个数值(即:你想要多少?),然后取该值并计算税和总计。我不知道该怎么做,想知道是否有人可以给我看?我目前的脚本如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to Infinate Happiness Ranch.'nPlease enter your order information bellow. ");
            Console.WriteLine();
            Console.WriteLine("Please enter your first and last name:");
            string FirstName = Console.ReadLine();
            Console.WriteLine("Please enter your street address:");
            string Address = Console.ReadLine();
            Console.WriteLine("Please enter your city:");
            string City = Console.ReadLine();
            Console.WriteLine("Please enter your two letter state abbreviation:");
            string StateCode = Console.ReadLine();
            Console.WriteLine("Please enter your zip code:");
            string ZipCode = Console.ReadLine();
            Console.WriteLine("Please enter the number of Tribbles 'nyou wish to purchase for $29.99 plus tax");
            string NumberOrdered = Console.ReadLine();
            Console.WriteLine("Invoice 'nName {0}", FirstName);
            Console.WriteLine("Address {0}", Address);
            Console.WriteLine("City {0}", City);
            Console.WriteLine("StateCode {0}", StateCode);
            Console.WriteLine("ZipCode {0}", ZipCode);
            Console.WriteLine("NumberOrdered {0}", NumberOrdered);
            // PROGRAM WORKS UNTIL HERE.
            NumberOrdered = m;
            TotalBeforeTax = m * 29.99; //'n' is total b4 tax
            o = n * 0.9;// 'o' is total tax due
            p = o + n; // 'p' is total due
            Console.WriteLine("Your total is {0} {1}", n);
            Console.WriteLine("Your tax is {0}", o);
            Console.WriteLine("Your total charge is {0}", p);
            Console.WriteLine("Thank you for your order");
            Console.WriteLine();
            //Console.WriteLine("Name:" + FirstName);
            Console.Read();    
        }
    }
}

操作用户输入(数学)

你应该

int ordered = int.Parse(NumberOrdered);

并继续用这个整数计算。

由于用户以字符串形式输入信息,因此应将排序的数字转换为整数。此外,为了保留您的小数,您需要将数字存储为双精度值,以用于此类数量。

int numOrdered = Convert.ToInt32(NumberOrdered);
double TotalBeforeTax = numOrdered * 29.99; 
double beforeTax = TotalBeforeTax * 0.9;
double afterTax = beforeTax + TotalBeforeTax;
Console.WriteLine("Your total is {0}", TotalBeforeTax);
Console.WriteLine("Your tax is {0}", beforeTax);
Console.WriteLine("Your total charge is {0}", afterTax);
Console.WriteLine("Thank you for your order");

你忘了声明一些变量并分配一些值。

试试这个:

static float m;
static float n;
static float o;
static float p;
static float TotalBeforeTax;
static void Main(string[] args)
{
    Console.WriteLine("Welcome to  Infinate Happiness Ranch.'nPlease enter your order information bellow. ");
    Console.WriteLine();
    Console.WriteLine("Please enter your first and last name:");
    string FirstName = Console.ReadLine();
    Console.WriteLine("Please enter your street address:");
    string Address = Console.ReadLine();
    Console.WriteLine("Please enter your city:");
    string City = Console.ReadLine();
    Console.WriteLine("Please enter your two letter state abreviation:");
    string StateCode = Console.ReadLine();
    Console.WriteLine("Please enter your zip code:");
    string ZipCode = Console.ReadLine();
    Console.WriteLine("Please enter the number of Tribbles 'nyou wish to purchase for $29.99 plus tax");
    string NumberOrdered = Console.ReadLine();
    Console.WriteLine("Invoice 'nName {0}", FirstName);
    Console.WriteLine("Address {0}", Address);
    Console.WriteLine("City {0}", City);
    Console.WriteLine("StateCode {0}", StateCode);
    Console.WriteLine("ZipCode {0}", ZipCode);
    Console.WriteLine("NumberOrdered {0}", NumberOrdered);
    //PROGRAM WORKS UNTIL HERE ? HELP ? ? ? ? ?
    //NumberOrdered = m;
    m = float.Parse(NumberOrdered);
    TotalBeforeTax = m * 29.99f; //'n' is total b4 tax
    n = TotalBeforeTax;
    o = n * 0.9f;//'o' is total tax due
    p = o + n; //'p' is total due
    Console.WriteLine("Your total is {0}", n);
    Console.WriteLine("Your tax is {0}", o);
    Console.WriteLine("Your total charge is {0}", p);
    Console.WriteLine("Thank you for your order");
    Console.WriteLine();
    Console.Read();
}

希望这有帮助!

只是一些建议,你可以在 Console.WriteLine() 中包含非字符串变量,如下所示:

Console.WriteLine("Your tax is " + o);

这是大多数专业开发人员所做的。 不需要复杂的 C/C++ 解析风格。

另外,您似乎没有声明变量 o 和 p.试试这个:

double o = Convert.toDouble(n * 0.9);

双 o = (双倍)(n * 0.9);