【无标题】matlab-为什么用户输入科目分数时,判断数值是否有效不能用if得用while?

由if引发的debug

对matlab学习过程中遇到的问题进行记录。2022-04-29

task:
Test statistics. Write a program, teststats.m , that prompts the user to enter 5 test scores. Then compute the mean, median, mode, and variance for the scores entered using the appropriate MATLAB functions. (Use help or doc if you need information on these functions.) Report the results for these test scores to the user.
即任务要求:编写一个matlab程序,用户从键盘输入5个分数,使用恰当的函数计算这些分数的均值、中位数、众数和方差。

1. 实现思路分析:基于matlab

(1)实现用户输入。
可以使用input函数从键盘(keyboard)获取用户输入。但是这值得注意的问题是,应该考虑用户误输入的情况,由于考试科目的分数不可能是负数,所以在输入的分数小于0时,我们需要让用户再一次重新输入正确的分数。

(2) 计算。
matlab有自带的函数可以满足这个题目的计算需求。如mean、mode、median、var。这些函数的具体使用方法可以在命令行窗口使用 help function 进行查询。
值得注意的是,我们首先需要知道方差是有两种的,一种是有效估计的方差,除以n,一种是无偏估计的方差,除以n-1,在matlab中使用的是无偏估计的方差,除以n-1。var()函数计算方差时,即默认计算无偏估计的方差。

(3) 打印。
使用disp函数将最终的结果打印在命令行窗口即可。

2. 实现代码
%% teststats.m
%    that prompts the user to enter 5 test scores.
%    Then compute the mean, median, mode, and variance for the scores
%    entered.
%    2022-04-29

clear
//%% set input parameters
N = 5;
score = zeros(1,N);

for i = 1:N
score(1,i) = input('please input your score: ');
    //%for score(1,i) < 0  改动的位置
    while score(1,i) < 0 
        disp('您输入的数字无效,请重新输入!');
        score(1,i) = input('please input your score again: ');
    end
end

//%% calculate mean、median、mode、variance
Mean = mean(score);
Median = median(score);
Mode = mode(score);
varUnbiased  = var(score); %计算的是无偏估计 除以(N-1)
var1 = sum((score(1,:)-Mean).^2)/(length(score)-1);  %unbiased 
var2 = sum((score(1,:)-Mean).^2)/length(score); % biased

//%% display the result
disp(['the mean of score: ', num2str(Mean)]);
disp(['the median of score: ', num2str(Median)]);
disp(['the mode of score: ', num2str(Mode)]);
disp(['the variance of score: ', num2str(varUnbiased)]);
disp(['the unbiased variance of score: ', num2str(var1)]);
disp(['the biased variance of score: ', num2str(var2)]);
3. 实现效果

以下是实现效果。确实是满足了我们一开始分析的功能要求。
在这里插入图片描述

4. 过程中遇到的问题以及解决办法

虽然代码实现比较简单,但是一开始也考虑了用户误操作的情况,如输入的score是负数。所以我在一开始使用for循环来获取用户输入的分数信息并把它存到score数组,然后获取的过程中用if判断它是否合格,不合格则会提示用户重新输入分数信息。代码如下:

for i = 1:N
score(1,i) = input('please input your score: ');
        if score(1,i) < 0  
        disp('您输入的数字无效,请重新输入!');
        score(1,i) = input('please input your score again: ');
        end
end

此时输出的结果看似正确,但其实是有陷阱的。
我们可以分析一下,假设此时的循环变量 i=1, score(1,1)的位置为输入的分数,假设为-10。ok, 由于-10<0,说明输入的分数不满足要求,disp发出警告。之后提示重新再输入一个数字,假设是-60吧,此时score(1,1)的位置就为-60了,然后直接结束了条件判断,进入到下一个循环(即 i = 2)的情况。效果就像这个样子:
在这里插入图片描述
此时的变量区:score里面存放的数据完全不对了。
在这里插入图片描述
究其原因,是循环一次, if 只判断一次,所以再次重新输入时就不再判断了,因此导致了错误。按照正常的逻辑来看,不满足就得一直判断直到满足为止。所以可以使用 while 来循环判断。修改后的代码:

for i = 1:N
score(1,i) = input('please input your score: ');
        while score(1,i) < 0
        disp('您输入的数字无效,请重新输入!');
        score(1,i) = input('please input your score again: ');
        end
end

此时就可以得到正确的结果了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值