Visual Basic的例子
这是一个用Visual Basic编写的简单程序。
1 Dim Input 2 Input = InputBox("How old are you??- Code") 3 If Not IsNumeric(Input) Then 4 MsgBox & Input & "That's not a number!" 5 ElseIf Input < 0 Then 6 MsgBox "你不能少于这个年龄!" 7 ElseIf Input > 100 Then 8 MsgBox "那是老了!" 9 Else 10 MsgBox "你是" & Input & " 岁。"11 End If
这个程序会询问用户的年龄,并根据用户输入的内容进行回答。如果用户输入的不是数字,程序就会这么说。如果用户输入的数字小于零,程序就会这么说。如果用户说他或她的年龄大于100岁,程序就会说:"真老!"如果用户输入了一个正确的年龄,程序就会对用户回说他或她的年龄。
Python的例子
这里有一个程序,它和上面的程序做同样的事情,但是是用Python做的。
try: age = int(raw_input("How old are you?")) except ValueError: print ("That's not a number!") else: if age < 0: print ("You cannot be less than zero!" elif age > 100: print ("That's old!") else: print( "You're %s years old." % age)
C#实例
和上面的程序一样,但用的是C#。
using System; public class Hello { public static void Main() { Console.WriteLine("你的年龄是多少?"); int age; if (! int.TryParse(Console.ReadLine(), out age)); int age.ReadLine(), out age)) Console.WriteLine("这不是一个数字!"); else if (age < 0) Console.WriteLine("你不能小于0!"); else if (age > 100) Console.WriteLine("真老!"); else Console.WriteLine("你已经{0}岁了。", age); }。}
Haskell的例子
同样的事情,但在Haskell中。
import Text.Read main = do putStrLn "你的年龄是多少?" input <- fmap readMaybe getLine putStrLn $ case input of Just age | age < 0 -> "你 不能小于零!" 。| age > 100 -> "那是老了!"| 否则 -> "你是" ++ 显示年龄 ++ " 岁。"Nothing -> "这不是一个数字!"