编程中国 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛  
 
全能 ASP / PHP / ASP.NET 主机,支持月付专业 MSSQL 数据库空间,支持月付专业 MySQL 数据库空间,支持月付学习型 ASP/PHP/ASP.NET 主机 30元/年
发新话题
打印

[求助]简单的 switch、case语句,总是不能判断条件??

[求助]简单的 switch、case语句,总是不能判断条件??

简单的Switch、case语句,总是不能判断条件

clear;clc;
s=input('请输入数字');
switch s
case s==10
r='十环';
case s<10&s>=0
r='不好';
otherwise
r='不可能';
end,s,r


当我运行,并且输入10,给出结果:不可能 ,请教各位,哪里写错了?

[此贴子已经被作者于2006-4-23 19:21:06编辑过]

TOP

input得到的是字符

TOP

那怎么才能得到数字?

TOP

swith case 用的不对
看help switch

TOP

clear
clc
prompt={'请输入环数'};
def={'10'};
dlgTitle='打靶评价';
lineNo=1;
tem=inputdlg(prompt,dlgTitle,lineNo,def);
s=str2num(tem{1});
if s==10
disp('十环')
elseif s>0 & s<10
disp('不太好')
else
disp('不可能')
end

TOP

看来没有学过C啊,switch是不能当if的理解去用的,尽管可以和if等价
给出switch版本,但是可以明显的看到switch语句用在此场合下没有if语句好
记得要用break;
clear;clc;
s=input('请输入数字');
switch s
case 0,
case 1,
case 2,
case 3,
case 4,
case 5,
case 6,
case 7,
case 8,
case 9,
r='不好';
break;
case 10
r='十环';
break;
otherwise
r='不可能';
end,s,r

TOP

[QUOTE]input得到的是字符[/QUOTE]
首先应该查看input的help而不是信口开河
INPUT Prompt for user input.
R = INPUT('How many apples') gives the user the prompt in the
text string and then waits for input from the keyboard.
The input can be any MATLAB expression, which is evaluated,
using the variables in the current workspace, and the result
returned in R. If the user presses the return key without
entering anything, INPUT returns an empty matrix.

R = INPUT('What is your name','s') gives the prompt in the text
string and waits for character string input. The typed input
is not evaluated; the characters are simply returned as a
MATLAB string.

The text string for the prompt may contain one or more '\n'.
The '\n' means skip to the beginning of the next line. This
allows the prompt string to span several lines. To output
just a '\' use '\\'.

See also KEYBOARD.
默认情况下input返回的是double的matrix
如果要返回string类型,可以用
s = input('input your option','s');

TOP

发新话题