2012-11-19 39 views
2

大家好,我一直有一些问题我已经设置输入数字或Q退出

任务

任务的第一部分是产出价格表如下规则:

高达50的价格是每个5英镑。在51到80之间,价格是每个4英镑,而81到100之间的价格是每个2.50英镑。 使用循环结构和选择语句(如..等),你的程序应该输出为10的倍数部件一个部件价格走势图高达100

我已经这样做了,不过任务的第二部分有我难倒 在表格输出后输入一些小部件。然后,您应该计算成本并输出价值。如果用户输入'q'或'Q',程序应该终止。

下面是完整的代码

using System; 
namespace w5Task3 
{ 
class Program 
    { public static void Main (string[] args) 
      { 

      double PriceEach1 = 5; 
      double PriceEach2 = 4; 
      double PriceEach3 = 2.50; 
      double Quantity = 10; 
      int UserOrder=0; 

      Console.WriteLine("\n\nBelow is the price chart:\n\n"); 
      Console.WriteLine("WidgetQuantity\t\t\tPrice\n"); 
      while (Quantity <=100) 

       { 

        double Price1 = PriceEach1*Quantity; 
        double Price2 = PriceEach2*Quantity; 
        double Price3 = PriceEach3*Quantity; 


         if (Quantity <=50)  
        { 
         Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price1); 
        } 


         if(Quantity >=51 && Quantity <=80)  
        { 
         Console.WriteLine("\t{0}\t\t\t{1:C}", Quantity, Price2); 
        } 


         if (Quantity >80 && Quantity <=100) 
        { 
         Console.WriteLine ("\t{0}\t\t\t{1:C}",Quantity, Price3);       
        } 
       Quantity +=10; 
       } 


      while (UserOrder >=0) 
       { 
        try 
         { 
         Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit"); 
         string temp = Console.ReadLine(); 
         if (temp =="q") break; 
         if (temp =="Q") break; 

         int.TryParse(temp, out UserOrder); 

         double UserPrice; 

         if (UserOrder <=50) 
          { 
           UserPrice = UserOrder*5; 
           Console.WriteLine("The price is {0:C}",UserPrice); 
          } 

         if (UserOrder >=51 && UserOrder <=80) 
          { 
           UserPrice = UserOrder*4; 
           Console.WriteLine("The price is {0:C}",UserPrice"); 
          } 

         if (UserOrder >80) 
          { 
           UserPrice = UserOrder*2.5; 
           Console.WriteLine("The price is {0:C}",UserPrice"); 
          } 


         } 
          catch(Exception) 
           { 
            Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit"); 
           } 
       } 



      } 
    } 

我有问题的部分是:

   while (UserOrder >=0) 
       { 
        try 
         { 
         Console.WriteLine("Enter the amount of widgets you would like to purchase or press q to quit"); 
         string temp = Console.ReadLine(); 
         if (temp =="q") break; 
         if (temp =="Q") break; 

         int.TryParse(temp, out UserOrder); 

         double UserPrice; 

         if (UserOrder <=50) 
          { 
           UserPrice = UserOrder*5; 
           Console.WriteLine("The price is {0:C}",UserPrice); 
          } 

         if (UserOrder >=51 && UserOrder <=80) 
          { 
           UserPrice = UserOrder*4; 
           Console.WriteLine("The price is {0:C}",UserPrice"); 
          } 

         if (UserOrder >80) 
          { 
           UserPrice = UserOrder*2.5; 
           Console.WriteLine("The price is {0:C}",UserPrice"); 
          } 


         } 
          catch(Exception) 
           { 
            Console.WriteLine("You have entered an incorrect value. Please enter a number or press q to quit"); 
           } 
       } 
} 

我可以让程序退出或做一个UserPrice但是当我需要使其根据订购的数量更改价格。

任何帮助或建议,非常感谢!

+2

我不知道(或者)哟你的问题是,如果你有一个请澄清。但我认为你要求我们为你做你的功课..=) – CodingGorilla

+2

如果你打算使用'int.TryParse',那么你不需要'try ... catch'块。 'TryParse'返回一个你应该测试的布尔值。如果它是假的,那么输入不能被解析。 –

+0

问题是当我编译时,我的if语句决定了它们的成本项目数量,我得到的消息换行符不变。对不起,任何困惑= P但是,这是一个任务设置,而在uni,但是我的讲义没有提及任何关于终止程序=( – Skittle

回答

2

第67行和第73行的常量表达式中有换行符!

修复这些,它都运行良好。

+0

非常感谢!不能相信我错过了现在已经注视了大约2个小时:(甚至错过了错误代码在微软网页上告诉我的内容,我认为这可能是更多咖啡的时间了 – Skittle

1

最简单的方法是在计算订单部分的价格后调整UserOrder。你可以去任何一个方向,但它基本上看起来像:

if (HasThisQuantityRange) 
{ 
    Total += ThisAmount * (Lower of: ThisQuantity or UserOrder); 
    UserOrder -= (Lower of: ThisQuantity or UserOrder); 
} 

if (UserOrder > 0 && HasAdjustedQuantityRange) 
{ 
    Total += ThisAmount * (Lower of: ThisQuantity or UserOrder); 
    UserOrder -= (Lower of: ThisQuantity or UserOrder); 
} 

等等。

显然,这都是伪代码,你需要自己实现它,但是这会让你指向正确的方向。

+1

你是怎么弄出这个问题的?:) –

+0

@trippino - 我找到了缺陷他的逻辑是基于既定的目标。虽然根据稍后添加的评论,我明确地指出了错误的问题。唉。 – Bobson

+0

而且,在进一步阅读中,我甚至不确定我对任务的解释是否准确。 – Bobson

0

我很难理解你到底是什么问题。您是否努力为订单得到正确的计算,或者退出行为是否不按照您期望的方式行事。

一般来说,我认为它的糟糕的形式使用像你的while循环条件的瞬态变量。当我想要一个控制台应用程序重复我通常会建立专门用于循环条件这样的变量:

bool isRunning = true; 
while (isRunning) 
{ 
    .... 

然后,当您检查您输入

if (temp.ToUpper() == "Q") 
{ 
    isRunning = false; 
    break; 
} 
+0

只需阅读您对有关换行符的评论错误通常在StackOverflow中,当你在标题和原始文章中发布特定错误或问题时,它们会有很大帮助,因为它们指向问题的根源.Josh Greifer的答案会告诉你要修复它,并且它无事可做用你的退出逻辑或订单价格逻辑 –

+0

感谢提示将在未来考虑到它,我只想知道很多信息,我认为人们会需要在上下文中看到问题......原来我只是盲目的 – Skittle

2

我觉得你的问题是报价UserPrice上排64和70 ..

Console.WriteLine("The price is {0:C}",UserPrice"); 

后删除

+0

好的地方,第一章。 –