I'm having problems creating a program that prompts a user to guess the colour.

I'm stuck because my while loop won't end although I wrote a false condition by using count?? The user is supposed to enter 5 characters. And the program is supposed to check whether there are how many correct guesses of colour and position and correct guesses of colour only. How do I do so? Pls help Thanks Im new to matlab

回答 (3 件)

Walter Roberson
Walter Roberson 2015 年 12 月 5 日
You have
while 1
That will not exit by itself; you need to have a "break" statement inside that loop in order to exit it.
You could be testing count in your "while", such as
while count <= 12345678
but if you do that, be sure to initialize count before the loop, not inside the loop.

6 件のコメント

Natalia Wong
Natalia Wong 2015 年 12 月 5 日
i don't understand can you please help????
Natalia Wong
Natalia Wong 2015 年 12 月 5 日
編集済み: Walter Roberson 2015 年 12 月 5 日
%Written by:
% Student ID:
%ver 01, 8 Dec by JL
clc
clear
% Initiate Random Color Sequence
% r-red; b-blue; y-yellow; g-green; w-white; p-purple; o-orange;
all_colors = 'rbygwpo';
index = randperm(7);
hidden_seq = all_colors(index(1:5));
count = 0;
while 1
%Prompting user to enter input
result = input('Guess the 5 characters colour sequence: ');
count = count + 1;
%Validate user input
if numel(result) > 5 || numel(result) < 5
error('You have entered more or less than 5 characters')
end
%Check the user guesses
result == hidden_seq
end
i've placed count before the loop
and how do i use the result from result == hidden_seq
if ans = 1 0 0 0 0
means that there is one correct colour at the first position. how do i print that result since the input changes according to the user and the random sequence of characters generated????
Thanks
Walter Roberson
Walter Roberson 2015 年 12 月 5 日
find (result == hidden_seq)
gives you the list of matching positions.
What do you want to do with the count? Are you sure you want to change the count if the user does not enter exactly 5 characters? When the user gets everything right, shouldn't you exit the loop?
Natalia Wong
Natalia Wong 2015 年 12 月 5 日
pls look at my answer below i can't attach the file by commenting thanks I'm stuck at task 4
%Written by: % Student ID: %ver 01, 8 Dec by JL
clc clear
% Initiate Random Color Sequence % r-red; b-blue; y-yellow; g-green; w-white; p-purple; o-orange; all_colors = 'rbygwpo'; index = randperm(7); hidden_seq = all_colors(index(1:5));
count = 0;
while 1 %Prompting user to enter input result = input('Guess the 5 characters colour sequence: '); count = count + 1;
%Validate user input
if numel(result) > 5 || numel(result) < 5
error('You have entered more or less than 5 characters')
end
%Check the user guesses
% 1. How many guesses has correct colors and position?
% 2. How many guesses has correct colors?
result == hidden_seq
end
sorry for disturbing but I'm so so frustrated after trying for so long. Can you please correct my codes and tell me my mistake big thanks

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

Natalia Wong
Natalia Wong 2015 年 12 月 5 日

0 投票

i'm not changing the count. just leave it for now. i'll attach the question to give you a clearer picture of what i want to do.
Image Analyst
Image Analyst 2015 年 12 月 5 日

0 投票

It seems like this was an earlier question I did not see when I was answering your duplicate question. So now we have this going on in two discussion threads (not good). See my answer over in your more recent posting: http://www.mathworks.com/matlabcentral/answers/259020-creating-a-program-that-guesses-the-colours-not-a-colour#answer_202294

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

タグ

質問済み:

2015 年 12 月 5 日

回答済み:

2015 年 12 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by