Flipping Values (1 Line)
1 次查看(过去 30 天)
显示 更早的评论
I have to create this program which flips two values and both are inputted by the user. I have to flip the value in a single line of code in the for loop. I have figured it using three lines of code but one line in the for loop is confusing me.
user1=input('')
user2=input('')
for total=1:5
.........
end
Basic Output
user1=1
userr2=2
user1=2
userr2=1
user1=1
userr2=2
采纳的回答
Jos (10584)
2013-10-12
Like other, I totally fail to see why you need a for loop ... Anyways, this will work
for k=1,
[b,a] = deal(a,b)
end
更多回答(2 个)
sixwwwwww
2013-10-12
Dear Rooy, here is the desired code without for loop:
a = input('Input first value: ');
b = input('Input second value: ');
c = a;
a = b;
b = c;
disp(strcat('First value is : ', num2str(a)))
disp(strcat('Second value is: ', num2str(b)))
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!