Write an if statement inside of a while loop to check for a positive, negative, or zero number entered by the user. Stop if the user enters zero.
    18 次查看(过去 30 天)
  
       显示 更早的评论
    
• Start with an if statement: Write a script that first asks the user for a number (using the input command). Then, using an if statement, print out if this number is positive, negative or zero. Test your script a few times to make sure it does the right thing for all three cases.
• Now move your if statement and put it in a for loop that asks the user for input 10 times so you don’t have to keep calling the script by hand. Remember that control-c in the command window will stop the loop.
• Now change the for loop to a while loop that stops when the user inputs zero. What should your stopping condition be?
0 个评论
回答(1 个)
  bio lim
      
 2015-7-14
        1.
 prompt = 'Insert a number: ';
 x = input(prompt);
 if x < 0 
    fprintf('Your input is negative number %f.\n', x);
 end
 if x >= 0
    fprintf('Your input is positive number %f.\n', x);
    if x == 0
        fprintf('The number you inserted is zero.\n')
    end
 end
2. The user can input multiple values using [], such as [3 5 6 7].
3. I don't understand why you want to make your code more complicated.
0 个评论
另请参阅
类别
				在 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!

