【发布时间】:2015-02-07 22:40:56
【问题描述】:
这是我的 cmets 代码。所以我无法退出循环。我知道它应该是 if(contd = true) 或其他东西,那么它应该退出,但如果他们输入 Y 继续前进,我该如何回到循环中? 另一件事是如何按 abc 顺序对其进行排序。然后我如何从 Z-A 做相反的事情。感谢您的帮助,这里是无知的初学者。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string lastName;
string [] lastName = new string[]; //array list of last names
int index;
string contd;
for (index=0;index++)
{
Console.Write("Enter a last name: ");
lastName[index] = Convert.ToString(Console.ReadLine());
Console.Write("Keep going? (Y/N): ");
//prompts user to keep inputting or exit out
contd = Convert.ToString(Console.ReadLine());
if (contd = "n"(Console.ReadLine());
{
//exit out of last name input
}
else contd = "y"(Console.ReadLine());
{
//go back into last name input
}
}
Console.WriteLine((index+1) + " last names entered");
//shows how many last names were entered
Console.WriteLine(); //spacing
//display last names
Console.WriteLine("Names in Acsending Order"); Console.WriteLine();
for(index=0; index++)
//shows the last names in order from A-Z (acsending)
{
Console.WriteLine(lastName[index]);
}
Console.WriteLine();
Console.WriteLine("Names in Descending Order"); Console.WriteLine();
for(index=0; index--)
//shows back last names in reverse order Z-A (descending)
{
Console.WriteLine(lastName[index]); Console.WriteLine();
}
Console.ReadLine();
}
}
}
【问题讨论】:
-
==不是=,作为旁注,使用 for 循环作为这样的 while 循环真的很奇怪 -
你应该一次问一个问题。
-
您真的想要没有结束条件的
for循环吗?顺便说一句,“abc order”在英文中被称为“alphabetical order”。 -
这还能编译吗?我的意思是有两个变量叫 lastName...
-
@MihaiCaracostea,这不可能编译..即使它编译了..它会在分配数组时抛出运行时错误..
标签: c# arrays loops if-statement alphabetical