2013-11-22 113 views
0

林只是要跳過代碼 1)爲什麼程序繼續?

function program() 

choice=input('What is your choice?','s') 
runProgram(choice) 
disp('the program will now end') 

在該函數的不相關部分我只是應該1和5

function runProgram(choice) 

if choice==1 
    loadfile 
elseif..... 
end 

該函數只是確定哪些之間鍵入一個數字我們將運行的5個子功能。

function loadfile() 

filename=input('Write the name of the file here: ','s') 
loadfile(filename) 

其中loadfile是另一個子函數,但我不需要詳細介紹。因爲現在我的問題是這樣的: 我運行'程序'功能並鍵入一個數字,但是它只是立即跳到結束消息,程序結束。該程序不應該先通過子功能嗎?

回答

2

您正在以字符串形式檢索輸入。所以比較失敗。

只需使用

input('What is your choice?') 

無 'S'

2

choice您的choice變量是一個字符串,但您將其與一個整數(if choice==1)進行比較。您需要將其與字符串進行比較(使用strcmp)或將其轉換爲數字(使用str2num)。

相關問題