How to make program keep asking input..

1 visualizzazione (ultimi 30 giorni)
serkan yassikaya
serkan yassikaya il 4 Giu 2015
Risposto: ag il 4 Dic 2024

This is a random number guessing program and asks input from user but asks once i want it to ask until correct answer given or end it when quit command typed.

display('Ödev-1 ile oluşturduğumuz oyuna hoşgeldiniz. ')

display('Tahmininizi yaptığınızda eğer doğru ise + yanlıs ise - alacaksınız!')

display('Eğer çok sıkılırsanız veya bulamaz ve sinirlenirseniz lütfen "game over" yazınız:)')

x = 0; while( x(1) == 0 ); x = randperm(10) - 1; end x = x(1:4);

a = x(1:1); b = x(2:2); c = x(3:3); d = x(4:4);

A = [a, b, c, d,]

prompt = 'Bir tahminde bulunun lütfen.';

H = input(prompt);

e = H/1000; e = floor(e); %binler basamağı buradan çıkar. r = H - e * 1000; r = r / 100; r = floor(r); % yüzler basamağı buradan çıkar. t = H-e*1000-r*100; t = t / 10; t = floor(t); y = H-e*1000-r*100-t*10; y = y / 1 ; y = floor(y); %onlar basamağı burdan çıkar.

J = [e,r,t,y,];

A == J; g = logical([J==A]); mp= '-+'; Dogrulukorani = mp(g+1) if A==J display('Tebrikler harikasın!') break; end while Dogrulukorani ~= 1111 do A == J; g = logical([J==A]); mp= '-+'; Dogrulukorani = mp(g+1)

H=input('Şansını bir kez daha denemek istermisin:');

if A==J display('Tebrikler harikasın!') break; else end

end

Risposte (1)

ag
ag il 4 Dic 2024
Hi Serkan,
To ensure that the program continues asking for input, until the correct number is guessed, you will need to keep the prompt inside a loop.
The below code demonstrates how to achieve this:
while true
% Prompt the user for input
prompt = 'Enter the number: ';
H = input(prompt, 's');
% Check if the user wants to quit
if strcmpi(H, 'quit')
break;
end
% Convert input to a number
H = str2double(H);
% rest of the logic
end
Hope this helps!

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by