function mytip1 x=input('please input a string:','s'); //要求输入一串字符,并保在变量x中if x=='class'//如果x等于'class'disp('Your input is class');//显示'Your input is class'elseif x=='name'//如果x等于'name'disp('Your input is name');//显示'Your input is name'elseif x=='No.' //如果x等于'No.'disp('Your input is No.');//显示'Your input is No.'else //如果以上条件都不符合disp('Your input error');//显示你输入错误end//if语句结束////////// function mytip2 x=input('please input a string:','s');//请求输入一串字符,保存在变量x中switch x//依据x的值做选择case 'class'//如果x等于'class'disp('Your input is class');//显示'Your input is class'case 'name' disp('Your input is name'); case 'No.' disp('Your input is No.'); otherwise
//如果以上情况都不符合disp('Your input error'); //显示你输入错误end//switch语句结束两个函数功能相同,都是输入一串字符后和已经设定好的一些值进行比较并显示比较结果,只是第一个用if语句实现,第二个用switch语句实现.
|