What is wrong with this code - if and else if statements

9 ビュー (過去 30 日間)
Mohammadamin Malek Pour
Mohammadamin Malek Pour 2017 年 3 月 9 日
編集済み: James Tursa 2017 年 3 月 9 日
Hi all, I'm new to Matlab and programming. I'm trying to solve a question but when I run the script it asks me for the values and when I enter them it doesn't print anything. Can anyone please tell me what is the problem?
clc
clear all
car = input('Enter the tybe of the, Sedan or SUV: ','s');
days = input('Enter the number of days: ');
miles = input('Enter the miles travelled: ');
cartype = char(car);
switch cartype
case char('Sedan')
if days>=1 && days<=6
if miles<=80
rent1 = (79*days)+((miles-80)*0.69);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
elseif days>=7 && days<=29
if miles<=100
rent2 = (69*days)+((miles-100)*0.59);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
elseif days>=30
if miles<=120
rent3 = (59*days)+((miles-120)*0.49);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
end
case char('SUV')
if days>=1 && days<=6
if miles<=80
rent4 = (84*days)+((miles-80)*0.74);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
elseif days>=7 && days<=29
if miles<=100
rent5 = (74*days)+((miles-100)*0.64);
fprintf('The cose of the rent is %6.2f $. \n',rent)
end
elseif days>=30
if miles<=120
rent6 = (64*days)+((miles-120)*0.54);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
end
end

採用された回答

James Tursa
James Tursa 2017 年 3 月 9 日
編集済み: James Tursa 2017 年 3 月 9 日
Change all of your rent1, rent2, etc variables to just rent, since that is what you print.
Also, to make things simpler on the user, maybe make your code case insensitive. E.g.,
switch upper(cartype)
case char('SEDAN')
In addition, your code doesn't print anything if the miles driven is too high. E.g., you should add else clauses like in the following:
if miles<=80
rent1 = (79*days)+((miles-80)*0.69);
fprintf('The cost of the rent is %6.2f $. \n',rent)
else
% ADD CODE HERE TO HANDLE HIGH MILEAGE CASES
end
  6 件のコメント
Mohammadamin Malek Pour
Mohammadamin Malek Pour 2017 年 3 月 9 日
I got the results. Thank you for your help. I appreciate it
James Tursa
James Tursa 2017 年 3 月 9 日
編集済み: James Tursa 2017 年 3 月 9 日
One other change you need to make is the location of your fprintf statements. The way you have it now, some of your cases still don't print anything since they don't have fprintf statements in them. You could put fprintf statement under each block, of course, but maybe a better approach is to just have one fprintf statement at the end of the code instead. That way if you decide to change the format of your output, you only have one code line to change instead of potentially dozens.
Also, it would be wise to include an "otherwise" case in your switch statement to catch car inputs that didn't match SEDAN or SUV.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by