Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
How can I repeat a switch case loop? (if something happens, I have to choose the case again)
    1 Ansicht (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
Hi,
I am trying to create a Stone, Paper, Scissors game in MATLAB, but I want that if there is a draw in any case, the game is repeated. Instead of break what should I put? (Return did not work too).
 T=0;
while T<=10
disp('Stone, Paper and Scissors')
disp('Stone      (1)')
disp('Paper      (2)')
disp('Scissors   (3)')
n=input('Which choice?: ')
pc=[1,2,3];
switch(n)
    case 1
        MATChoice=datasample(pc,1)
        if MATChoice==1
            *break*
        end
        if MATChoice==2
            disp('MATLAB win; Player LOSE')
        end
        if MATChoice==3
            disp('MATLAB lose; Player WIN')
        end
    case 2
        MATChoice=datasample(pc,1)
        if MATChoice==1
            disp('MATLAB lose; Player WIN')
        end
        if MATChoice==2
            *break*
        end
        if MATChoice==3
            disp('MATLAB win; Player LOSE')
        end
    case 3
        MATChoice=datasample(pc,1)
        if MATChoice==1
            disp('MATLAB win; Player LOSE')
        end
        if MATChoice==2
            disp('MATLAB lose; Player WIN')
        end
        if MATChoice==3
            *break*
        end
    otherwise
       error('Choose 1,2 or 3')
end
T=T+1;
end
0 Kommentare
Antworten (1)
  Image Analyst
      
      
 am 2 Dez. 2017
        I gave a solution in your duplicate question
numberOfTrials = 30; % Whatever you want...
for k = 1 : numberOfTrials
  choices = {'Rock', 'Paper', 'Scissors', 'Quit'};
  userChoice = menu('Which choice?: ', choices)
  if userChoice == 4
    break;
  end
  computerChoice =randi([1,3])
  message = sprintf('You chose %s and computer chose %s', ...
    choices{userChoice}, choices{computerChoice})
  switch userChoice
    case 1
      switch computerChoice
        case 1
          message = sprintf('%s\nTie', message);
        case 2
          message = sprintf('%s\nComputer wins.', message);
        case 3
          message = sprintf('%s\nYou win', message);
      end
    case 2
      switch computerChoice
        case 1
          message = sprintf('%s\nYou win!', message);
        case 2
          message = sprintf('%s\nTie', message);
        case 3
          message = sprintf('%s\nComputer wins', message);
      end
    case 3
      switch computerChoice
        case 1
          message = sprintf('%s\nComputer wins.', message);
        case 2
          message = sprintf('%s\nYou win!', message);
        case 3
          message = sprintf('%s\nTie', message);
      end
    case 4
      break;
  end
  uiwait(helpdlg(message));
end
0 Kommentare
Diese Frage ist geschlossen.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

