How to make a bar graph using user input values
Mostra commenti meno recenti
I am trying to graph a bar graph from a number guessing game which asks users to input values. I want to make a bar graph of the numbers guessed but am only getting the bar graph to display the final number guessed and not all numbers guessed. Any feedback is great thank you! Code is below.

disp('Are you ready for your opponent to guess your number, begin playing.')
pause(1)
Player_1_Guess = input('Enter a number between 1 and 200:');
chance = 5;
clc
while chance <= 5 && chance >= 1;
chance = chance - 1;
for Player_2_Guess = input('Take your guesses on what the correct number is between 1 and 200\n');
if Player_2_Guess < 1;
disp('!NUMBER NOT IN RANGE!')
elseif Player_2_Guess > 200;
disp('!NUMBER NOT IN RANGE!')
end
if Player_1_Guess < Player_2_Guess
disp('!TOO HIGH!')
elseif Player_1_Guess > Player_2_Guess;
disp('!TOO LOW!')
end
fprintf('Chances remaining: %d, ', chance);
if Player_1_Guess == Player_2_Guess;
chance = 6;
end
end
end
if chance == 0;
disp('!NO MORE CHANCES!')
disp('!GAME OVER YOU LOSE!')
elseif chance == 6;
fprintf('!CONGRATS YOU WIN!')
end
ca = categorical({'P1 number','P2 guess1','P2 guess2','P2 guess3', 'P2 guess4', 'P2 guess5'});
bar(ca(1),Player_1_Guess)
hold on
bar(ca(2),Player_2_Guess)
hold on
bar(ca(3),Player_2_Guess)
hold on
bar(ca(4),Player_2_Guess)
hold on
bar(ca(5),Player_2_Guess)
hold on
bar(ca(6),Player_2_Guess)
grid on
xlabel('Player Guesses')
ylabel('Guessed Number Value')
title('Numbers Guessed by P2 & P1 Input')
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
