控制台在输入时关闭,按 C#
本文关键字:输入 控制台 | 更新日期: 2023-09-27 18:30:25
我的控制台应用程序(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
}
}
}
程序被编写为在用户输入第一个值并显示结果后终止。您需要围绕主程序的某种循环机制来保持其运行。大致如下:
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);
}
首先将所有
内容包装在 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
}
}
}
将所有
代码包装在一段时间内,一个 do while 或 a for。您还可以使用函数将周期与循环分开。
如果它适用于一个并且您希望它做很多事情,则需要在要重复的代码部分周围添加一个循环......
在你的逻辑周围放一个循环,在你的主终止之前放一个读取函数
static void Main(string[] args)
{
for(int i=0;i<5;i++)
{
// add item to list
}
Console.Read()
}
你需要
一段时间来封装你的主逻辑。 这 while 将通过键入 q 退出,否则它将再次循环遍历您当前的 main。
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
}
}