Info

此问题已关闭。 请重新打开它进行编辑或回答。

How do i skip portions of a loop after getting my answer?

1 次查看(过去 30 天)
I am trying to make a loop that modifies a certain value in a matrix and skip to the end of the loop after the modification is made. Right now the value will be modified, but is modified again before the loop can end and I don't know how to fix it.
world = ['#' '.' '.' '.' '.' '.' '#' '.' 'v' '#'
'.' '#' '.' '#' '#' '.' '.' '.' '.' '.'
'.' '.' '#' '#' '.' '.' '#' '#' '.' '#'
'#' '.' '.' '.' '.' '#' '.' '.' '.' '.'
'.' '#' '.' '#' '#' '.' '.' '#' '.' '.'
'.' '#' '.' '.' '.' '#' '#' '#' '.' '#'
'#' '.' '#' '.' '.' '#' '#' '#' '.' '.'
'.' '#' '.' '#' '.' '.' '.' '#' '.' '.'
'.' '.' '.' '#' '#' '.' '#' '.' '#' '.'
'G' '#' '.' '.' '.' '.' '.' '.' '#' '#']
idx = 81; while idx ~= 10; move = input('Your move?(a,w,d,q)','s'); for idx = find(world == 'v'); switch(move) case 'a' world(idx) = '>'; case 'w' [r,c] = find(world == 'v'); world(r,c) = '.'; world(r+1,c) = 'v'; case 'd' world(idx) = '<'; end end for idx1 = find(world == '^'); switch(move) case 'a' world(idx1) = '<'; case 'w' [r,c] = find(world == '^'); world(r,c) = '.'; world(r-1,c) = '^'; case 'd' world(idx1) = '>'; end end for idx2 = find(world == '<'); switch(move) case 'a' world(idx2) = 'v'; case 'w' [r,c] = find(world == '<'); world(r,c) = '.'; world(r,c-1) = '<'; case 'd' world(idx2) = '^'; end end for idx3 = find(world == '>'); switch(move) case 'a' world(idx3) = '^'; case 'w' [r,c] = find(world == '>'); world(r,c) = '.'; world(r,c+1) = '>'; case 'd' world(idx3) = 'v'; end end clc; disp(world); counter = counter + 1

回答(1 个)

Image Analyst
Image Analyst 2014-10-15
I didn't really read/understand that code, but in general to skip portions of code you can use "if", break, or continue commands.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by