matlab 寫程式,能詢問是否再次執行

如何用matlab寫一個類似無窮迴圈的呢

如果想用

month=input('請輸入月份:');

if(month<1) | (month>12)

disp('月份錯誤');

end

date=input('請輸入日期:');

if(date>=32) | (date<=0)

disp('日期錯誤');

end

執行完後...想讓他詢問我是否要執行,如果打yes他就會重複再次執行上面的程式

如果打no停止了!

要怎寫呢ˊˋ~想破頭了..想不出來

1 Answer

Rating
  • 老師
    Lv 7
    1 decade ago
    Best Answer

    n=1;

    while n==1

    month=input('\nPlease input the month: ');

    if(month<1) | (month>12)

    disp('Input is wrong');

    end

    date=input('Please input date: ');

    if(date>=32) | (date<=0)

    disp('Input is wrong');

    end

    s=input('\nDo you want to continue, "y" or "n": ','s');

    if s=='n' n=2; end

    end

    ------------------------------------------

    存檔 date_e.m

    執行

    >> date_e

    Please input the month: 12

    Please input date: 5

    Do you want to continue, "y" or "n": y

    Please input the month: 5

    Please input date: 25

    Do you want to continue, "y" or "n": n

    >>

    請參考

    還有很多種寫法 例如使用 return 或 break

Still have questions? Get your answers by asking now.