can some xxplain what the following program does? Thank youuuu!: peso=5000; while (peso > 0) disp (‘I order another coffee’); dollar = dollar – 250; end disp (‘I finished drinking

2 ビュー (過去 30 日間)
Auldrey Laurente
Auldrey Laurente 2020 年 2 月 29 日
回答済み: Les Beckham 2020 年 2 月 29 日
can someone explain what the following program does? Thank youuuu!
peso=5000;
while (peso > 0)
disp (‘I order another coffee’);
dollar = dollar – 250;
end
disp (‘I finished drinking coffee’)
x = sym('x')
length(char(x))
for k = 1:10
x =sqrt (1+x)
length(char(x))
end
  2 件のコメント
Les Beckham
Les Beckham 2020 年 2 月 29 日
編集済み: Les Beckham 2020 年 2 月 29 日
This script does pretty much nothing other than generate a lot of errors when you run it. I'm not sure if it was a result of how you pasted it into the forum or if it is in the original code, but the single quote characters are not the simple apostrophe characters expected by Matlab (they are the "fancy" ones that might be inserted by a word processor, where the open and close quotes are different).
After replacing those, we get down to the real errors in the actual code.
On the fourth line of code you decrement 'dollar' which is a variable which was not previously defined. This generates an error.
If I replace 'dollar' on this line with 'peso' we get a little bit further and get a result that starts to make sense. What it does (after the previously described changes) is repeatedly reduce the value of 'peso' by 250 until it is not greater than zero, printing ‘I order another coffee’ on each pass through the loop. When 'peso' becomes less than or equal to zero, the loop exits and ‘I finished drinking coffee’ is displayed.
The second part of your code where you start using symbolics appears to be totally unrelated to the code before that. It pretty much makes no sense and I don't have the symbolic toolbox, so I won't even try to comment further on that part of the code.
Stephen23
Stephen23 2020 年 2 月 29 日
+1 Les Beckham: that should be an answer.

サインインしてコメントする。

回答 (1 件)

Les Beckham
Les Beckham 2020 年 2 月 29 日
Per Stephens comment, I'm repeating my comment as an answer (with some additions).
This script does pretty much nothing other than generate a lot of errors when you run it. I'm not sure if it was a result of how you pasted it into the forum or if it is in the original code, but the single quote characters are not the simple apostrophe characters expected by Matlab (they are the "fancy" ones that might be inserted by a word processor, where the open and close quotes are different).
After replacing those, we get down to the real errors in the actual code.
On the fourth line of code you decrement 'dollar' which is a variable which was not previously defined. This generates an error.
If I replace 'dollar' on this line with 'peso' we get a little bit further and get a result that starts to make sense. What it does (after the previously described changes) is repeatedly reduce the value of 'peso' by 250 until it is not greater than zero, printing ‘I order another coffee’ on each pass through the loop. When 'peso' becomes less than or equal to zero, the loop exits and ‘I finished drinking coffee’ is displayed.
Note that, to be more realistic (assuming coffee costs 250 pesos), you should probably change the while condition to 'while (peso >= 250)'. In your specific example it doesn't make a difference since your initial value of 5000 is evenly divisible by 250. I thought I would mention this anyway.
The second part of your code where you start using symbolics appears to be totally unrelated to the code before that. It pretty much makes no sense and I don't have the symbolic toolbox, so I won't even try to comment further on that part of the code.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by