How to suppress the output of an mfile?
1 回表示 (過去 30 日間)
古いコメントを表示
When I run the code below the line in the for loop 'Unfortunately you didn''t win anything today, try again tomorrow!' isn't suppressed for some reason and is always displayed on the command window. Was wondering how to stop this.
%National Lottery number selector
Player_Numbers = [4 23 27 45 21 33];
numbers = [1:1:59];
Winning_Numbers = numbers(randperm(59,6))
similarity = intersect(Player_numbers,Winning_Numbers);
money = numel(similarity);
if money == 3
fprintf('Congratulations 3 of your numbers matched, you win £25!\n');
elseif money == 4
fprintf('Congratulations 4 of your numbers matched, you win £100!\n');
elseif money == 5
fprintf('Congratulations 5 of your numbers matched, you win £1000!\n');
elseif money == 6
fprintf('Congratulations all of your numbers matched! You win \n todays jackpot of £1,000,000!\n');
else
fprintf('Unfortunately you didn''t win anything today, try again tomorrow!\n');
end
0 件のコメント
回答 (2 件)
Fangjun Jiang
2019 年 2 月 21 日
Nothing is wrong except the variable "Player_Numbers" mismatches "Player_numbers" with the capital "N".
That is why it is so hard to win the lottery, right?
What do you mean by "isn't suppressed"? The message is printed out on purpose.
7 件のコメント
Fangjun Jiang
2019 年 2 月 22 日
31.6 cents per run?! Great, Walter! I shall keep running the program and waiting for some bit coins coming out :)
1.05 percent of chance to match 3 numbers seems high. What is the formula? I am out of practice on probability. Is it nchoosek(6,3)/nchoosek(59,6)? Which is way samller than 1.05 percent.
Fangjun Jiang
2019 年 2 月 22 日
編集済み: Walter Roberson
2019 年 2 月 22 日
All right, you are right, Walter!
Walter Roberson
2019 年 2 月 22 日
The best approach is to rewrite the code to total the prizes instead of printing each individual success.
But if for some reason you cannot do that, then you can evalc() running the script, capturing the result into a variable. Then count the number of substrings '3 of', '4 of', '5 of', 'all of' and multiply the counts by the relative prizes and total.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!