我的控制台应用程序(C#)出现问题。我想这样做,当你输入一个汽车品牌时,它将返回我已成功完成的所有那些汽车的总价值。我遇到麻烦的是,我希望他们能够输入另一个汽车品牌,程序会在没有关闭的情况下再次返回总金额。现在它会在第二次有人点击进入时关闭。请帮忙! 我的代码如下。
namespace Cars
{
class Program
{
static void Main(string[] args)
{
List<Car> myCars = new List<Car>() {
new Car() { Make="BMW", Model="550i", Color=CarColor.Blue, StickerPrice=55000, Year=2009 },
new Car() { Make="Toyota", Model="4Runner", Color=CarColor.White, StickerPrice=35000, Year=2010 },
new Car() { Make="BMW", Model="745li", Color=CarColor.Black, StickerPrice=75000, Year=2008 },
new Car() { Make="Ford", Model="Escape", Color=CarColor.White, StickerPrice=28000, Year=2008 },
new Car() { Make="BMW", Model="550i", Color=CarColor.Black, StickerPrice=57000, Year=2010 }
};
Console.WriteLine("Type \"all\" to see total value of all cars");
Console.WriteLine("Type \"bmw\" to see total value of all bmws");
Console.WriteLine("Type \"toyota\" to see total value of all Toyotas");
Console.WriteLine("Type \"ford\" to see total value of all Fords");
string userValue = Console.ReadLine();
if (userValue == "all")
{
var _orderedCars = myCars.OrderByDescending(p => p.Year);
foreach (var car in _orderedCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _orderedCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
Console.ReadLine();
}
else if (userValue == "bmw")
{
var _bmws = myCars.Where(car => car.Make == "BMW");
foreach (var car in _bmws)
{
Console.WriteLine("{0} {1} - {2}- {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _bmws.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
Console.ReadLine();
}
else if (userValue == "toyota")
{
var _toyotaCars = myCars.Where(car => car.Make == "Toyota");
foreach (var car in _toyotaCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _toyotaCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
Console.ReadLine();
}
else if (userValue == "ford")
{
var _fordCars = myCars.Where(car => car.Make == "Ford");
foreach (var car in _fordCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _fordCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
Console.ReadLine();
}
else
{
Console.WriteLine("Error");
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double StickerPrice { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
}
答案 0 :(得分:2)
在用户输入第一个值并显示结果后,您的程序将被写入终止。你需要在主程序周围采用某种循环机制来保持它的运行。有点像:
bool readyToExit = false;
while (!readyToExit)
{
/*
* ...
* Display your menu here.
* ...
*/
String userInput = Console.ReadLine();
ProcessInput(userInput); // Move all your main logic into a new ProcessInput method.
readyToExit = userInput.Equals("quit", StringComparison.CurrentCultureIgnoreCase);
}
答案 1 :(得分:1)
首先在while循环中包装所有内容。然后,使用逻辑来分隔语句以便于阅读。最后,当用户输入除有效选择之外的任何内容或其他内容时,跳出while循环。
namespace Cars
{
class Program
{
static void Main(string[] args)
{
List<Car> myCars = new List<Car>() {
new Car() { Make="BMW", Model="550i", Color=CarColor.Blue, StickerPrice=55000, Year=2009 },
new Car() { Make="Toyota", Model="4Runner", Color=CarColor.White, StickerPrice=35000, Year=2010 },
new Car() { Make="BMW", Model="745li", Color=CarColor.Black, StickerPrice=75000, Year=2008 },
new Car() { Make="Ford", Model="Escape", Color=CarColor.White, StickerPrice=28000, Year=2008 },
new Car() { Make="BMW", Model="550i", Color=CarColor.Black, StickerPrice=57000, Year=2010 }
};
while (true)
{
Console.WriteLine("Type \"all\" to see total value of all cars");
Console.WriteLine("Type \"bmw\" to see total value of all bmws");
Console.WriteLine("Type \"toyota\" to see total value of all Toyotas");
Console.WriteLine("Type \"ford\" to see total value of all Fords");
string userValue = Console.ReadLine();
Console.WriteLine();
if (userValue == "all")
{
var _orderedCars = myCars.OrderByDescending(p => p.Year);
foreach (var car in _orderedCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _orderedCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
}
else if (userValue == "bmw")
{
var _bmws = myCars.Where(car => car.Make == "BMW");
foreach (var car in _bmws)
{
Console.WriteLine("{0} {1} - {2}- {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _bmws.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
}
else if (userValue == "toyota")
{
var _toyotaCars = myCars.Where(car => car.Make == "Toyota");
foreach (var car in _toyotaCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _toyotaCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
}
else if (userValue == "ford")
{
var _fordCars = myCars.Where(car => car.Make == "Ford");
foreach (var car in _fordCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _fordCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
{
Console.WriteLine("Error");
}
}
else
{
Console.WriteLine("Exit program (Y/N)?");
var answer = Console.ReadLine();
if (answer.ToString().ToUpper() != "N")
{
break;
}
}
Console.WriteLine();
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double StickerPrice { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
}
答案 2 :(得分:0)
你需要一段时间来封装你的主要逻辑。这样会输入,比如输入q,否则它会再循环通过你当前的主。
class Program
{
static void Main(string[] args)
{
List<Car> myCars = new List<Car>() {
new Car() { Make="BMW", Model="550i", Color=CarColor.Blue, StickerPrice=55000, Year=2009 },
new Car() { Make="Toyota", Model="4Runner", Color=CarColor.White, StickerPrice=35000, Year=2010 },
new Car() { Make="BMW", Model="745li", Color=CarColor.Black, StickerPrice=75000, Year=2008 },
new Car() { Make="Ford", Model="Escape", Color=CarColor.White, StickerPrice=28000, Year=2008 },
new Car() { Make="BMW", Model="550i", Color=CarColor.Black, StickerPrice=57000, Year=2010 }
};
bool ohPleaseLetMeDoItAgain = true;
while (ohPleaseLetMeDoItAgain)
{
Console.WriteLine("Type \"all\" to see total value of all cars");
Console.WriteLine("Type \"bmw\" to see total value of all bmws");
Console.WriteLine("Type \"toyota\" to see total value of all Toyotas");
Console.WriteLine("Type \"ford\" to see total value of all Fords");
Console.WriteLine("Type \"q\" to quit");
string userValue = Console.ReadLine();
if (userValue == "all")
{
var _orderedCars = myCars.OrderByDescending(p => p.Year);
foreach (var car in _orderedCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _orderedCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
}
else if (userValue == "bmw")
{
var _bmws = myCars.Where(car => car.Make == "BMW");
foreach (var car in _bmws)
{
Console.WriteLine("{0} {1} - {2}- {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _bmws.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
}
else if (userValue == "toyota")
{
var _toyotaCars = myCars.Where(car => car.Make == "Toyota");
foreach (var car in _toyotaCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _toyotaCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
}
else if (userValue == "ford")
{
var _fordCars = myCars.Where(car => car.Make == "Ford");
foreach (var car in _fordCars)
{
Console.WriteLine("{0} {1} - {2} - {3:C}", car.Make, car.Model, car.Year, car.StickerPrice);
}
var sum = _fordCars.Sum(p => p.StickerPrice);
Console.WriteLine("{0:C}", sum);
}
else
{
ohPleaseLetMeDoItAgain = !userValue.Equals("q");
}
}
}
class Car
{
public string Make { get; set; }
public string Model { get; set; }
public int Year { get; set; }
public double StickerPrice { get; set; }
public CarColor Color { get; set; }
}
enum CarColor
{
White,
Black,
Red,
Blue,
Yellow
}
}
答案 3 :(得分:0)
将所有代码包裹一段时间,一段时间或一段时间。您还可以使用函数将循环与循环分开。
答案 4 :(得分:0)
如果它适用于一个并且您希望它执行许多操作,则需要在代码中要重复的部分添加一个循环...
答案 5 :(得分:0)
绕过你的逻辑圈,然后在你的主要终止之前放一个读函数
static void Main(string[] args)
{
for(int i=0;i<5;i++)
{
// add item to list
}
Console.Read()
}