C# 操作用户输入(数学)
我用的是C。我试图允许用户输入一个数值(即:你想要多少?),然后取那个值并计算税额和总额。我不知道该怎么做,不知道是否有人能给我看?我当前的脚本如下:C# 操作用户输入(数学),c#,C#,我用的是C。我试图允许用户输入一个数值(即:你想要多少?),然后取那个值并计算税额和总额。我不知道该怎么做,不知道是否有人能给我看?我当前的脚本如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 { class Program { static void Main(string
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();
}
}
}
只是一点建议,您可以在Console.WriteLine()中包含非字符串变量,如: 这是大多数专业开发人员所做的。不需要复杂的C/C++风格的解析 而且,您似乎没有声明变量o和p。试试这个: 双o=转换为双(n*0.9) 或
双o=(双)(n*0.9) 您应该使用
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();
}
希望这有帮助 您的程序无法编译。永远不要说你的程序“不起作用”。解释你期望发生的事情和实际发生的事情。什么是
m
?我看不出它的定义。这是一个相当长的程序。也许您可以使用s等删除所有不必要的东西,例如?看起来您正在尝试使用string
s进行数学运算。他们不喜欢那样。将它们转换为int
,float
或其他任何形式。这是一个相当大的概括。。。没有理由引导人们远离字符串格式的方法。有很多。这是一种更直观的编码方式,更容易理解并减少人为错误的可能性,特别是在处理不同类型的多个变量时。糟糕的建议。没有回答这个问题。
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();
}