stringcompareexample.java

来自「java 完全探索的随书源码」· Java 代码 · 共 50 行

JAVA
50
字号
public class StringCompareExample{  // Default Constructor  public StringCompareExample()  {    super();  }  // Method that actually performs the comparison  public int compareStrings( String str1, String str2 )  {    return str1.compareTo( str2 );  }  // Main Method  public static void main( String[] args )  {    if ( args.length != 2 )    {      System.out.println( "Usage: java StringCompareExample <String1> <String2>" );      System.exit( 0 );    }    // Create an instance of the example    StringCompareExample example = new StringCompareExample();    // Get the strings passed in from the command line    String str1 = args[0];    String str2 = args[1];    // Inform the user what is going on    System.out.println( "Comparing string: '" + str1 + "' with: '" + str2 + "'");    // perform the comparison    int result = example.compareStrings( str1, str2 );    // check the result for a negative    if ( result < 0 )    {      System.out.println( "String 1 is less than String 2" );    }    else if ( result > 0 )    {      System.out.println( "String 1 is greater than String 2" );    }    else    {      System.out.println( "String 1 and String 2 are equal" );    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?