嘿,我刚刚创建了一个小型控制台应用程序,用于写出需要申请的工作,位置和联系方式就像一个小练习。 看看我的代码,似乎不断写出Console.WriteLine语句是非常低效的,因为我已经使函数有助于提高效率。
我认为实现这一目标的最佳方法是使用循环,但我不确定如何使用不同的变量(例如phn1,phn2变量)使循环循环
谢谢你们!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace PlacestoApplyfor
{
class Program
{
static void Main(string[] args)
{
//Displays the actual program in descending order.
//Used functions as a faster method to change variable names on the fly
//Whilst keeping its format clean and in descending order
Console.WriteLine("--Name of Places--");
ApplyFor("EdwayApps" ,"Appxperts","Genie App Studio","Appster");
Console.WriteLine("--Name of Locations--");
LocationsInOrder("3/424 St kilda", "101/27 Little Collins St", "Contact Online only", "2/377 LonsDale ST");
Console.WriteLine("--Companies Numbers-- ");
PhoneNumbers(043990976,1300939225,0421336722,1800709291);
}
static void ApplyFor(string num1, string num2, string num3, string num4) {
//Literally just the job names
Console.WriteLine(num1);
Console.WriteLine(num2);
Console.WriteLine(num3);
Console.WriteLine(num4);
}
// Job Locations
static void LocationsInOrder(string loc1, string loc2, string loc3, string loc4) {
Console.WriteLine(loc1);
Console.WriteLine(loc2);
Console.WriteLine(loc3);
Console.WriteLine(loc4);
}
//Contact - references
static void PhoneNumbers(int phn1, int phn2, int phn3, int phn4) {
Console.WriteLine(phn1);
Console.WriteLine(phn2);
Console.WriteLine(phn3);
Console.WriteLine(phn4);
}
}
}
答案 0 :(得分:4)
params关键字是您的朋友:
static void PhoneNumbers(params int[] phn)
{
foreach(var num in phn)
Console.WriteLine(num);
}
然后你可以这样做:
PhoneNumbers(1,2,3,4,5,6,7,8,9);
甚至传入一个数组:
PhoneNumbers(new int[] {1,2,3,4,5,6,7,8,9});
答案 1 :(得分:1)
为什么你写这么多console.writeline() 而不是写下面的代码
在课堂上
static void Main(string[] args)
{
Console.WriteLine("--Name of Places--");
ApplyFor("EdwayApps", "Appxperts", "Genie App Studio", "Appster");
Console.WriteLine("--Name of Locations--");
LocationsInOrder("3/424 St kilda", "101/27 Little Collins St", "Contact Online only", "2/377 LonsDale ST");
Console.WriteLine("--Companies Numbers-- ");
PhoneNumbers(043990976, 1300939225, 0421336722, 1800709291);
Console.ReadLine();
}
static void ApplyFor(string num1, string num2, string num3, string num4)
{
//Literally just the job names
Console.WriteLine(num1 + "\n"+num2 + "\n" + num3 + "\n " + num4);
}
// Job Locations
static void LocationsInOrder(string loc1, string loc2, string loc3, string loc4)
{
Console.WriteLine(loc1 + "\n" + loc2 + "\n" + loc3 + "\n " + loc4);
}
//Contact - references
static void PhoneNumbers(int phn1, int phn2, int phn3, int phn4)
{
Console.WriteLine(phn1 + "\n" + phn2 + "\n" + phn3 + "\n " + phn4 +"\n");
}
答案 2 :(得分:0)
你可以只使用一个函数来编写所有行,并为换行符配置分隔符" \ n"或","对于带有逗号分隔值的一行。你甚至可以添加"主题"作为打印功能的第一个参数,它将在" - 主题 - "符号。
static void Main(string[] args)
{
//Displays the actual program in descending order.
//Used functions as a faster method to change variable names on the fly
//Whilst keeping its format clean and in descending order
Print("Name of Places", "EdwayApps", "Appxperts", "Genie App Studio", "Appster");
Print("Name of Locations", "3/424 St kilda", "101/27 Little Collins St", "Contact Online only", "2/377 LonsDale ST");
Print("Companies Numbers", 043990976, 1300939225, 0421336722, 1800709291);
Console.ReadKey();
}
static void Print(string topic, params object[] array)
{
Console.WriteLine("-- {0} -- \n\n {1}\n", topic, string.Join("\n ", array));
}
答案 3 :(得分:0)
使用params很酷,就像Lloyd所说的那样,但如果你试图优化Console.WriteLine(),就不要为此反复调用.WriteLine。请查看String.Format和String.Join方法
static void PhoneNumbers(params int[] phn)
{
Console.WriteLine(String.Join(Environment.NewLine, phn));
}
同样的事情只是空格分隔的值,在一行
static void PhoneNumbers(params int[] phn)
{
Console.WriteLine(String.Join(" ", phn));
}
或者如果你想用一定数量的值在一行中写下所有这些值,只需执行此操作
static void PhoneNumbers(params int[] phn)
{
Console.WriteLine(String.Format("{0} {1} {2} {3}", phn));
}