ch4_07.cs

来自「《c#技术内幕代码》」· CS 代码 · 共 50 行

CS
50
字号
using System;

class CH4_7
{
   public static int PrintTwo( String[] args )
   {
      Console.WriteLine("Argument 1 {0} Argument 2 {1}", args[0], args[1] );
      return 2;
   }
   public static int PrintOne( String[] args )
   {
      Console.WriteLine("Argument 1 {0}", args[0]);
      return 1;
   }
   public static int PrintNone()
   {
      Console.WriteLine("You didn't supply the right number of arguments!");
      return 0;
   }
      
   public static void Main(String[] args)
   {
      // Check for arguments. Use a nested if to handle
      // two special cases.
      if ( args.Length == 2 )
      {
         Console.WriteLine("Argument 1 {0} Argument 2 {1}", args[0], args[1] );
	 if ( args[0] == "Hello" )
	    Console.WriteLine("Well, hello yourself!");
      }
      
      // Alternatively, we could have a nested if-else statements
      if ( args.Length == 2 )
      {
         Console.WriteLine("Argument 1 {0} Argument 2 {1}", args[0], args[1] );
      }
      else
         if ( args.Length == 1 )
	    Console.WriteLine("Argument 1 {0}", args[0] );
	 else
	    Console.WriteLine("You didn't supply the right number of arguments!");
	    
      // We can do the same thing with a ternary operator. Warning: Its ugly!
      Console.WriteLine("Printing... {0}", args.Length == 2 ? PrintTwo(args) : 
                           args.Length == 1 ? PrintOne(args) : 
			      PrintNone() );
	 
   }
}

⌨️ 快捷键说明

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