[討論] 迴圈

看板MATLAB作者 (1000型電聯車)時間12年前 (2012/04/10 00:14), 編輯推噓4(402)
留言6則, 4人參與, 最新討論串1/1
我是要設計一個a+b 然後讓使用者輸入a+b=(一個答案) 然後去判斷是不是正確的 clear clc a = round(rand*9)+1 b = round(rand*9)+1 c = input('a+b=') while a+b>=0 if c~=a+b disp('No. Please try again!') elseif c=a+b disp('Very good!');break elseif c~=a+b disp('Wrong. Try once more') elseif c=a+b disp('Excellent!');break elseif c~=a+b disp('Do not give up!') elseif c=a+b disp('Nice work!');break elseif a+b>=0 disp('No. Keep trying!');break else c=a+b disp('Keep up the good work!');break end end;break 我按F5 但是 程式一直跟我說 The expression to the left of the equals sign is not a valid target for an assignment. 就是 我的c一直有問題 請問一下 我的c是哪裡出問題了? 謝謝各位!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.135.42.50

04/10 02:07, , 1F
把所有的c=a+b改成c==a+b
04/10 02:07, 1F
水喔!

04/10 13:55, , 2F
:判斷式重複;並且最後else 是上述條件都不合才會執行
04/10 13:55, 2F

04/10 13:56, , 3F
所以不須加任何判斷條件
04/10 13:56, 3F
但是我判斷條件刪掉後就不能跑了 Expression or statement is incomplete or incorrect. ※ 編輯: EMU1000 來自: 140.135.42.50 (04/10 14:12)

04/10 14:29, , 4F
有三行紅色錯誤c=a+b 改成 c==a+b 最後一個else改elseif
04/10 14:29, 4F

04/10 14:33, , 5F
第一個if 裡面y忘記加break;
04/10 14:33, 5F
嗯 但是我目前最大的瓶頸來了 假設a=1 b=2 我輸入一個錯的答案8好了 然後跳出 No. Please try again! 最大的問題就是 程式就這樣結束了 但是我還想要再輸入一次a+b= 然後繼續再輸入一個錯的答案 顯示:Wrong. Try once more ※ 編輯: EMU1000 來自: 140.135.42.50 (04/10 14:37)

04/10 17:11, , 6F
c = input('a+b='); 這行放在迴圈裡
04/10 17:11, 6F
文章代碼(AID): #1FWmhUZP (MATLAB)