编写从命令行接受字符串并按字母顺序显示其字符的 Java 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5328113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:40:21  来源:igfitidea点击:

Write a Java code that accepts strings from the command line and displays its characters in alphabetical order

javastringcommandreverse

提问by hiren gamit

The word COMPUTERshould be display as CEMOPRTU.

该词COMPUTER应显示为CEMOPRTU

The string should be accepted from the command line-

应该从命令行接受字符串-

class sortstr
{


         public static void main(String args[])
         {

                  int len=args.length;


                  if(len==0)
                  {


                         System.out.println("No arguments are given ! ");

                         return;

                  }




                  char[] str=args[0].toCharArray();

                 char temp;




                  for(int i=0;i<len;i++)
                  {

                      for(int j=0;j<len-1;j++)
                      {

                             // Swap the characters


                             if(str.charAt(j)> str.charAt(j+1))
                             {

                                   temp=str[j];

                                   str[j]=str[j+1];

                                   str[j+1]=temp;
                             }

                       }
                  }



                  for(int i=0;i<len;i++)
                  {

                       System.out.print(str[i]);

                  }




       }

}

回答by Eyal Schneider

Since this is homework, I can only suggest you answer yourself the following questions:

由于这是作业,我只能建议您回答自己以下问题:

1) What standard Java class can be used for reading from the console?

1) 什么标准 Java 类可用于从控制台读取?

2) What data structure will you use for the input string, in order to sort the chars in it later?

2) 您将使用什么数据结构作为输入字符串,以便稍后对其中的字符进行排序?

3) What library method allows sorting the data structure in (2) ?

3)什么库方法允许对(2)中的数据结构进行排序?

Once you know the answers, all you have to do is let your fingers play on the keyboard...

一旦你知道答案,你所要做的就是让你的手指在键盘上弹奏......