@
输入姓,返回名
perl
#!/usr/bin/perl
%map=('Lebron'=> 'James','Paul'=> 'George'); #create hash
while(defined($line= <STDIN>)){ #将键盘输入保存为变量$line
if( $line eq "Lebron
" )
{printf "$map{'Lebron'}
"}
elsif( $line eq "Paul
" ) #注意回车符
{printf "$map{'Paul'}
"}
else
{printf "NO"}
}
shell
#!/bin/bash
#使用ifelse语句
echo "please enter the last name:"
while : #无限循环
do
read lastname
if [ $lastname == "Lebron" ];then #注意等号两边的空格
echo "James"
elif [ $lastname == "Paul" ];then
echo "George"
else
echo "none"
fi
done
#使用case语句
echo "please enter the last name:"
while :;do
read lastname
case $lastname in
Lebron) echo "James";;
Paul) echo "George";;
*) echo "None";; #输入不匹配上面列出的项时
esac
done
tcl
while {1} { #无限循环
set input [gets stdin] #从键盘读取输入
#puts $input
if {[string match "Lebron" $input]} { #注意}与下一个{之间的空格
puts "James"
} elseif {[string match "Paul" $input]} {
puts "George"
} else {
puts "None"
}
}
python
#!/usr/bin/env python
# -*- coding:utf-8 -*-
while 1: #无限循环
str=input("please enter last name:") #使用input函数读取键盘输入
if (str == "Lebron"):
print ("James")
elif (str == "Paul"):
print ("George")
else:
print ("None")