[英]C#: Is there a way to return a side by side array with commas after what you input on a list on the console window from top to bottom?
我是 C# 的新手。這是我的代碼。 當我運行這段代碼時,我將汽車名稱、里程、年份、品牌、model、顏色和車身類型輸入到如下所示的列表中
Name of Car: Car 1
Enter Mileage: 4000 (my input)
Enter year: 2022 (my input)
Enter make: ford (my input, etc.)
Enter model: fusion
Enter color: red
Enter bodytype: coupe
Name of Car: Car 2
Enter Mileage: 4000
Enter year: 2022
Enter make: ford
Enter model: fusion
Enter color: red
Enter bodytype: coupe
Name of Car: Car 3
Enter Mileage: 4000
Enter year: 2022
Enter make: ford
Enter model: fusion
Enter color: red
Enter bodytype: coupe
我想將我為汽車 1、汽車 2、汽車 3 輸入的內容返回為三個並排的 arrays,逗號如下所示?
car 1, 4000, 2022, ford, fusion, red, coupe
car 2, 4000, 2022, ford, fusion, red, coupe
car 3, 4000, 2022, ford, fusion, red coupe
下面是我寫的代碼,在這段代碼的最后是我想並排打印 arrays 的地方。應用對象和方法的概念,我應該寫什么才能在控制台上並排打印數組 window ?
using System;
using System.Management.Instrumentation;
using Assignment5;
namespace Assignment5
{
public class Car
{
//public:
public void Set_Name_of_Car(string name)
{
name_of_car = name;
}
public void Set_Body_Type(string Type) //methods that you can call
{
bodytype = Type;
}
public void Set_Make(string Color, string Make, string Model )
{
color = Color;
make = Make;
model = Model;
}
public void Set_Mileage(int Mileage, int Year)
{
mileage = Mileage;
year = Year;
}
public void Show()
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
}
}
}
namespace Program
{
internal class Program
{
static void Main(string[] args)
{
Car car1 = carInfo();
Car car2 = carInfo();
Car car3 = carInfo();
}
public static Car carInfo()
{
Console.WriteLine("Enter Name of Car: ");
string n_Aim = Console.ReadLine();
Console.WriteLine("Enter Mileage:");
int miles = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Year:");
int yr = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter Make:");
string meigh_k = Console.ReadLine();
Console.WriteLine("Enter Model:");
string mod_L = Console.ReadLine();
Console.WriteLine("Enter Color:");
string Col_R = Console.ReadLine();
Console.WriteLine("Enter Bodytype: ");
string Bdy_Typ = Console.ReadLine();
Car inputcar = new Car();
inputcar.Set_Name_of_Car(n_Aim);
inputcar.Set_Mileage(miles, yr);
inputcar.Set_Make(Col_R, meigh_k, mod_L);
inputcar.Set_Body_Type(Bdy_Typ);
return inputcar;
}
}
}
我想嘗試的是使用,但我認為我的導師不希望我們使用該方法,因為它太簡單了。 請問您還有其他建議嗎? 謝謝你:D
Console.WriteLine("car 1, 4000, 2022, ford, fusion, red, coupe");
Console.WriteLine("car 2, 4000, 2022, ford, fusion, red, coupe");
Console.WriteLine("car 3, 4000, 2022, ford, fusion, red coupe");
為所述 class 覆蓋 tostring function
namespace Assignment5
{
public class Car
{
//public:
public void Set_Name_of_Car(string name)
{
name_of_car = name;
}
public void Set_Body_Type(string Type) //methods that you can call
{
bodytype = Type;
}
public void Set_Make(string Color, string Make, string Model )
{
color = Color;
make = Make;
model = Model;
}
public void Set_Mileage(int Mileage, int Year)
{
mileage = Mileage;
year = Year;
}
public void Show()
{
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
}
public override string ToString()
{
return name_of_car + ", " + mileage + ", " + year + ", " + make + ", " + model + ", " + color + ", " + bodytype
}
}
}
然后只打印每輛車的字符串
static void Main(string[] args)
{
Car car1 = carInfo();
Car car2 = carInfo();
Car car3 = carInfo();
Console.WriteLine(car1.ToString());
Console.WriteLine(car2.ToString());
Console.WriteLine(car3.ToString());
}
您也可以這樣做,因為它會自動調用 ToString 方法
static void Main(string[] args)
{
Car car1 = carInfo();
Car car2 = carInfo();
Car car3 = carInfo();
Console.WriteLine(car1);
Console.WriteLine(car2);
Console.WriteLine(car3);
}
可以覆蓋每個class繼承自object
的Car
的ToString
方法,讓汽車顯示自己。 在class這輛車中,添加這個方法:
public override string ToString()
{
return $"{name_of_car}, {mileage}, {year}, {make}, {model}, {color}, {bodytype}";
}
您還最好將汽車添加到列表中。 這簡化了汽車的處理,因為您現在可以輕松處理任意數量的汽車,而無需添加更多汽車變量。
var cars = new List<Car>();
cars.Add(carInfo());
cars.Add(carInfo());
cars.Add(carInfo());
foreach (Car car in cars) {
Console.WriteLine(car);
}
請注意, Console.WriteLine
自動使用ToString
方法來顯示任何對象。
不再需要Car
中的Show
方法。 輸入/輸出( Console.ReadLine/WriteLine
)應該在數據 class 中完成。
您還可以讓用戶輸入汽車,直到他在不輸入名稱的情況下按Enter 。 為此,從輸入法返回null
:
public static Car carInfo()
{
Console.WriteLine("Enter Name of Car: ");
string n_Aim = Console.ReadLine();
if (n_Aim = "") {
return null;
}
Console.WriteLine("Enter Mileage:");
...
}
然后
var cars = new List<Car>();
while (true) {
Car car = carInfo();
if (car == null) {
break; // Exit the loop
}
cars.Add(carInfo());
}
foreach (Car car in cars) {
Console.WriteLine(car);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.