2015-02-07 86 views
-3

这里是我的代码与评论。所以我很难退出循环。我知道如果(contd = true)应该退出,但如果他们输入Y继续前进,我该如何回到循环中? 另一件事是如何按abc顺序排序。然后我怎么做Z-A的反向。感谢您的帮助,这里是无知的初学者。字符串数组以abc顺序排序

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(); 







    } 
} 
} 
+1

'=='不是'=',作为一个方面说明,它真的很奇怪使用for循环作为一个while循环,如 – Sayse 2015-02-07 22:43:31

+0

你应该问一个问题我。 – 2015-02-07 22:43:32

+1

你真的*想'没有结束条件'循环吗?顺便提一句,“abc order”在英语中被称为“字母顺序”。 – 2015-02-07 22:46:24

回答

0

你在做任务而不是比较,而且你还用分号结束了你的if语句行。分配正义的设置contdn而不是检查,如果这是其当前值,并在最后的分号就会完全俯瞰下面的括号像下面..

if(contd == "n") 
    ; 

这是有效的代码(不知道为什么你我想这样做)。下面是你仿佛在寻找,但我仍然认为你的for循环有点奇怪

contd = Console.ReadLine(); // Already a string.. 
if (contd == "n") 
{ 
    //exit out of last name input 
    break; 
} 
else 
{ 
    //go back into last name input 
} 

这些名字的字母顺序进行排序,你会是最好的寻找到排序算法如冒泡排序。你总是可以使用LINQ,但我想这是你的类范围之外

lastName = lastName.OrderBy(x => x).ToArray(); 
lastName = lastName.OrderByDescending(x => x).ToArray(); 
+0

@Downvoter?.... – Sayse 2015-02-07 22:51:02

1

可以使用dowhile循环:

string d = string.Empty; 

do 
{ 
    Console.WriteLine("What's your answer?"); 
    d = Console.ReadLine(); 
} 
while (d != "Y"); 

为排列,您可以使用Array.Sort

Array.Sort(array);