C# 修复在输入的用户输入循环与其在数组中的索引匹配之前不继续的用户输入
我遇到的问题是,当我从数组中输入一个元素时,用户输入会重置到循环的开始,例如,当我从数组中输入第三个元素时,用户输入问题会再重复两次以继续下一个用户输入问题,我如何解决这个问题?大家都知道我在这里可以调整什么。C# 修复在输入的用户输入循环与其在数组中的索引匹配之前不继续的用户输入,c#,C#,我遇到的问题是,当我从数组中输入一个元素时,用户输入会重置到循环的开始,例如,当我从数组中输入第三个元素时,用户输入问题会再重复两次以继续下一个用户输入问题,我如何解决这个问题?大家都知道我在这里可以调整什么。 while(true) { 字符串[]bgtype={“奶酪汉堡”、“tlc”、“bbq”}; int[]bgtyperprice={15,25,10}; int max=bgtype.Length; 字符串[]产品=新字符串[max]; 字符串[]类型=新字符串[max]; 整数[]
while(true)
{
字符串[]bgtype={“奶酪汉堡”、“tlc”、“bbq”};
int[]bgtyperprice={15,25,10};
int max=bgtype.Length;
字符串[]产品=新字符串[max];
字符串[]类型=新字符串[max];
整数[]数量=新整数[最大值];
int[]disc=新的int[max];
控制台。写线(“”);
对于(int i=0;i
if(bgtype[i].Contains(type[i]))
应更改为if(bgtype.Contains(type[i])
Array.Contains,验证项目是否包含在数组中,而不是数组值中。通过询问bgtype[i]
是否包含type[i]
,您是在询问cheesburger
是否包含tlc
,而它不包含tlc
,因此为什么它从一开始就开始了。通过验证bgtype
是否包含type[i]
,您会询问[“cheesburger”、“tlc”、“bbq”]
是否包含tlc
,它确实包含。我希望这足够清楚。那么,关于。。
while (true)
{
String[] bgtype = { "cheeseburger","tlc", "bbq" };
int[] bgtypeprice = { 15, 25, 10 };
int max = bgtype.Length;
String[] product = new string[max];
String[] type = new string[max];
int[] qty = new int[max];
int[] disc = new int[max];
Console.WriteLine("");
for (int i = 0; i < max; i++)
{
Console.Write("What PRODUCT would you like to buy ?: "); ;
product[i] = Console.ReadLine();
if (product[i].Equals("burger", StringComparison.CurrentCultureIgnoreCase))
{
{
Console.Write("What TYPE of product would you like to buy?: ");
type[i] = Console.ReadLine();
if (bgtype[i].Contains(type[i]))
{
Console.Write("Enter your discount % (5% for adults & 7% for minors): ");
disc[i] = Convert.ToInt32(Console.ReadLine());
Console.Write("How many will you buy? ");
qty[i] = Convert.ToInt32(Console.ReadLine());
float total = bgtypeprice[i]; total *= qty[i];
Console.WriteLine("Total cost of " + type[i] + " " + product[i] + " is: " + qty[i] + " pieces x P" + bgtypeprice[i] + "= P" + total);
float totaldisc = 0; totaldisc = (total * disc[i]) / 100;
Console.WriteLine("Total amount of discount: P " + totaldisc);
float totalamt = 0; totalamt = total - totaldisc;
Console.WriteLine("Total cost of order: P " + totalamt);
Console.WriteLine("-------------------ORDER CONFIRMATION-------------------");
}}}}}