【问题标题】:string array to sort in abc order [closed]要按 abc 顺序排序的字符串数组 [关闭]
【发布时间】: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


【解决方案1】:

您可以使用do while 循环:

string d = string.Empty;

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

至于排序可以使用Array.Sort

Array.Sort(array);

【讨论】:

    【解决方案2】:

    您正在执行分配而不是比较,并且您还用分号结束了 if 语句行。该赋值只是将contd 设置为n,而不是检查这是否是它的当前值,并且末尾的分号将完全忽略下面的大括号,如下所示..

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

    【讨论】:

      猜你喜欢
      • 2018-05-29
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多